Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to refer to one part of multi-value combobox

Status
Not open for further replies.

lunaclover

Programmer
Jun 22, 2005
54
Hi all - I'm having a problem with syntax, I think. I just made my first class (called MyList) and am trying to work with it. I learned on VB6 and even that was a while ago, but .. well anyway, classes are new to me, so I may have something wrong.

I created the class so that I could have two values in my combobox - the first is called 'desc' and the second is called 'value'. It looks like this when populating.
Code:
Dim sBrand As String = BRAND.Text
   Select Case sBrand
      Case "Elm"
         With CAT540
             .Items.Add(New MyList("10-11 EER A/C", "H"))
             .Items.Add(New MyList("11+ EER A/C", "J"))
             .Items.Add(New MyList("9-10 EER A/C", "M"))

where CAT540 and BRAND are comboboxes. This works okay.

I am trying to use one of the fields (desc) as reference when telling it how to populate the next combobox, based off the selection in the previous combobox, like this.

Code:
If BRAND.Text <> "" Then
   If CAT540.Items.Item(CAT540.SelectedText).Desc = "11+ EER A/C" Then
        ID540.Items.Add("J")
    End If

where ID540 is the next combobox.

It gives me a

ERROR
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from string "" to type 'Integer' is not valid.
ERROR

Here's my class I made, if that helps explain why the error is being produced.

Code:
Public Class MyList
        Private _sDesc As String
        Private _sValue As String

        ' Default empty constructor. 
        Public Sub New()
            _sDesc = ""
            _sValue = ""
        End Sub

      Public Sub New(ByVal Desc As String, ByVal Value As String)
            _sDesc = Desc
            _sValue = Value
        End Sub
      Public ReadOnly Property Desc() As String
            Get
                Return _sDesc
            End Get
      End Property
        ' This is the property that holds the extra data. 
      Public ReadOnly Property Value() As String
            Get
                Return _sValue
            End Get
      End Property

        ' This is necessary because the ListBox and ComboBox rely 
        ' on this method when determining the text to display. 
      Public Overrides Function ToString() As String
            Return Desc
            Return Value
      End Function
   End Class

How am I supposed to reword this to make it work? I could just say

CAT540.text = "10-11 EER A/C"

but that would defeat the purpose of my multivalue combobox and my class and everything. It is erroring out because I am saying

CAT540.Items.Item(CAT540.SelectedText).Desc "blah"
Do I even have to use the description again, shouldn't it know? I don't get it.

Thanks in advance,
Luna
 
At the second block of code:

CAT540.SelectedText should be
CAT540.SelectedIndex
 
Sorry, should have told you, that was how it was originally, it still produces the error.
I have tried selectedvalue, selectedtext and selectedindex.

Thanks,
Luna
 
Okay, let me make it more clear.

Here is what I am attempting to do. I just had a better idea. I want to populate a combobox dynamically with a description, while populating a textbox with the value of that description.

I created the class MyList above to hold two 'fields'(is it right?) desc and value. Desc will go into the combobox. When it is selected, I will have code that will write the value matched to that desc into a textbox.

Here is how I'm populating the desc and value fields, respectively.

Code:
.Items.Add(New MyList("10-11 EER A/C", "H"))

But, I will still need to refer to that desc for other reasons in the code, which is why I am trying to figure out the correct way to say

Code:
If 
CAT540.Items.Item(CAT540.SelectedText).Desc Then
blah blah blah
End If

and also how to refer to the value so it will write that segment into a textbox. I'm assuming it will be similiar.

Can anyone help me? Am I not making sense? Is there a better way to do this - I'm thinking a sql table, but pretend there's a way to do it this way.. is there?

Thanks in advance..
Luna
 
I don't think that this is the right way to do that. First i must say the the CAT540.Items.Item(.....) accepts an integer, from 0 to CAT540.Items.Count-1. This you are tring to do is amazingly easy with database. The combo has to be binded to a datasource-a binding source. If you create a table (SQL) and add two columns "desc" and "value" with types "nvarChar(MAX)" you will then have to fill even from the design time the:
1. Datasource
2. DisplayMember (set to col "desc")
3. ValueMember (set to col "value")
If you do something like msgbox(CAT540.SelectedValue) you will get the "value" from the "desc".
To display that in a textbox, just bind it to the "value" column.

I said before that it is very easy. To do that without the database, add a new datasource. There, will be listed: Database, webservice, object... etc. Select the object and follow the wizard.


Hope these help
 
Thanks, tipgiver, I decided yesterday I was going to use a sql db, even though my boss didn't recommend it. We'll hope you and I are right!

Luna
 
How many entries will be in the combobox? (I mean the "desc" (which has a "value")). Also, are they static, or you will want to add some more desc-value pairs ?
 
There will be about 20 tables with three or less columns, ranging from 5 to 100 rows.
They will always be changing.
 
SQL then is what you need. Not only the data input will be easier, but also the binding to the controls. If you are using 2005 version you wont need a single line of code. I think the same for e.g. 2003 version of visual studio. A quick thought of your saying "They will always be changing."... you can have for example a column to keep the date that the record was added, and with one (simple) query to delete those records that are e.g. 10 days or more older.
Last, this extra column wont be a problem because you can exclude it from viewing it by a SELECT statement, or by creating a view.


Hope these help.
:)
 
Thanks so much for the insight. I will definitely keep that in mind.

Right now, I am trying to do what you spoke of earlier. I have displaymember = desc and valuemember = value, and then I am coding:
Code:
Private Sub txtModelN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _CLASS.SelectedValueChanged, txtModelN.TextChanged

        If _CLASS.DisplayMember = "15-25 TON" Then
            txtModelN.Text = CAT540.ValueMember()
        End If
    End Sub

How is this wrong?

Thanks TipGiver!
L
 
I suppose that the CAT540 datasource is a table, and displaymember set to desc and valuemember to value (columns).

Private Sub RefreshComboCAT540(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CAT540.SelectedIndexChanged
txtModelN.Text = CAT540.ValueMember()
End Sub


Is the above working?
What version of VB do you have?
 
Oupss,

Change this : txtModelN.Text = CAT540.ValueMember()
to : txtModelN.Text = CAT540.SelectedValue()

But you can also do:
Goto the textbox properties pane, find the databindings and expand it (click the +). At the Text property select you table binding sourse and select the "value". Without any code, the "value" will be displayed in the textbox, when ever you choose something else from the combobox.
 
Yes, your assumptions are right, sorry about that. It just said VALUE in the textbox when I tried it. But, that's okay, because I got it to work with

Private Sub ModelNumber(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtModelN.TextChanged

If _CLASS.SelectedIndex <> -1 Then
txtModelN.Text = CAT540.SelectedValue
End If
End Sub

I just wonder why _CLASS.DisplayMember and _CLASS.ValueMember don't work like I think they should. :)

I have Visual Studio 2003. There are a lot of exceptions and rules I have in this project, where I will have to write code behind it to 'catch' these exceptions/rules and not display certain data dependent on previous selections in comboboxes that a user has selected.

Thanks for your help!

Luna

 
I just wonder why _CLASS.DisplayMember and _CLASS.ValueMember don't work like I think they should.

They work fine. You set a column to be displayed by DisplayMember. You set the ValueMember to a column too. It is like a hidden field to keep some information. To get it use the .SelectedValue(). To get the actuall text that is displayed in the combo box, do : ComboBoxName.Text()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top