List Info

Thread: read combobox text




read combobox text
user name
2006-12-05 01:36:09
Steve, I am not sure in .Net. ; Hopefully someone will chime in.  I have included a VB6 example.  .Net should be similar.

Private Sub Combo1_Click()
Dim ComboData As String
Dim ComboListIndex As Integer
If Combo1.ListIndex = -1 Then Exit Sub
 ;  ComboListIndex = Combo1.ListIndex
   ComboData = Combo1.List(ComboListIndex)
   MsgBox "Item selected was " & ComboData
End Sub

Private Sub Form_Load()
Combo1.AddItem ("dog")
Combo1.AddItem ("cat")

End Sub



Steve wrote:
eGroups.com" type="cite">

For VB.Net 2005, How do you read the text of a combobox item?

combobox1.items.Add("Tokyo")

How do I read the string "Tokyo"?

P.S. I'm still looking for a Reference manual that will explain this
kind of stuff, if anyone has any suggestions. I have four VB.Net
reference books and not one can answer this basic question. How
pathetic is that?


__._,_.___ __,_._,___
read combobox text
user name
2006-12-05 15:25:44
Air code, and If I mis-state things, I hope someone will correct me, but this is my understanding.
 
1) In .NET everything is an object.  So, if you "add something to a combobox", you are adding an object.
 
2) Comboboxes (and likewise listboxes) have an items property, which is an object that implements ICollection, so you can use the methods of the Collection interface, like Add, remove, Insert, Clear, etc.
 
3) When you add an object to a cbo, you are  adding the object to the control's items collection (Or more abstractly, the items member, which behaves like a collection -- I believe that's the right way to think of it in .NET).
 
4) This means you can enumerate the items collection like any other -- bearing in mind that the collection comprises untyped objects (that has Option Strict, CType, and DirectCast implciations for the data type), f'rex
    With cbo1.items
        .Clear()   
        .Add("One")    ' add a string object whose value is "One"
        .Add("Two")    ' add a string object whose value is "Two"
        .Insert(0,"Zero")    'Insert a string object whose value is "Zero" at ordinal position 0
    End With
 
5) If you add an object, the displaywill normally execute the object's ToString method... since the ToString method (which all objects have) of a string is it's value, you see the string in the combo.  You also get the sting's value in output.  F'rex
 
    Dim o as object
    For Each o in cbo1.items
        Debug.Writeln(o) ;      
    Next
 
If you know it's a string, you can avoid late binding with
 
    Dim o as object
    For Each o in cbo1.items
        Debug.Writeln(DirectCast(o, String).ToString
        Debug.Writeln(DirectCast(o, String)
        Debug.Writeln(CType(o, String)    'etc
    Next
 
If it's a user-defined or other complex object, it will execute ToString, so make sure you write that overridable method.  In that case you really should use DirectCast or CType to access the object's properties, though.
 
That out of the way....  Here's the answer to what you actually asked.
 
6) The two properties that test for the selected values are
SelectedIndex, or -1 if nothing is selected.  Conversely you can set the selection to Nothing by setting it to -1.  This propoerty replaces ListIndex in VB6.
SelectedItem, wichihc returns a reference to the item, or the value Nothing if nothing is selected.  (I think you can also set that property to Nothing to blank the combo -- not sure).   
You can then use the return value as you see fit. 
F'rex,
For strings these should work:
cbo1.selectedItem
cbo1.selectedItem.ToString
cbo1.items(SelectedIndex)
 
for a combo that store a multi-field Employee object, you could get the ID # field of that record object with
 
Dim i as Integer = DirectCast(cbo1.SelectedItem, EmployeeRecord).ID
Dim i as Integer = CType(cbo1.SelectedItem, EmployeeRecord).ID
 
It also means that you can store a complex object, show it's ToString method, and then work with another other property of the object once you retrieve it.  When you get used to it, it beats the VB6 way all to hell. ; F'rex, there's no VB6-style ItemData property, but you could define one for the object... then show the ToStrng method int he comboi, and then later work with ItemData of the retrieved object.
 
For multi-select listboxes there are analogous SelecteItems and SelectedIndexes Collections
 
HTH
-BDN


From: helpwithvb@yahoogroups.com [mailto:helpwithvb@yahoogroups.com] On Behalf Of Tim Lewis
Sent: Monday, December 04, 2006 8:36 PM
To: helpwithvb@yahoogroups.com
Subject: Re: [helpwithvb] read combobox text

Steve, I am not sure in .Net. ; Hopefully someone will chime in.  I have included a VB6 example.  .Net should be similar.

Private Sub Combo1_Click()
Dim ComboData As String
Dim ComboListIndex As Integer
If Combo1.ListIndex = -1 Then Exit Sub
   ComboListIndex = Combo1.ListIndex
   ComboData = Combo1.List(ComboListIndex)
   MsgBox "Item selected was " & ComboData
End Sub

Private Sub Form_Load()
Combo1.AddItem ("dog")
Combo1.AddItem ("cat")

End Sub



Steve wrote:

eGroups.com type="cite">

For VB.Net 2005, How do you read the text of a combobox item?

combobox1.items.Add("Tokyo")

How do I read the string "Tokyo"?

P.S. I'm still looking for a Reference manual that will explain this
kind of stuff, if anyone has any suggestions. I have four VB.Net
reference books and not one can answer this basic question. How
pathetic is that?


__._,_.___ __,_._,___
read combobox text
user name
2006-12-05 13:57:18
Good Morning Steve,

>>  How do I read the string "Tokyo"?

Do you mean 
" How Do I Know What The User Selected ?"    ( ? )

From This Sample / Example:

htt
p://vb-helper.com/howto_net_listbox_class.html

...you can use the ListBox's   SelectedItem   method to see
which item is
selected.

< other teaching points >

Finally, this code shows how the program ( will )
display information about the item you click (upon ). 	
 	
' Display the selected item's information:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    ListBox1.SelectedIndexChanged

    Dim food_item As FoodItem = ListBox1.SelectedItem()

    lblInfo.Text = food_item.ToString()
End Sub	



All The Best,
Steve

08:56  AM  EST  Tuesday, December 05, 2006


From: helpwithvb@yahoogroups.com 
[mailto:helpwithvb@yahoogroups.com] On Behalf Of Tim Lewis
Sent: Monday, December 04, 2006 8:36 PM
To: helpwithvb@yahoogroups.com
Subject: Re: [helpwithvb] read combobox text

Steve, I am not sure in .Net.  Hopefully someone will chime
in.  
I have included a VB6 example.  .Net should be similar.

Private Sub Combo1_Click()
Dim ComboData As String
Dim ComboListIndex As Integer
If Combo1.ListIndex = -1 Then Exit Sub
   ComboListIndex = Combo1.ListIndex
   ComboData = Combo1.List(ComboListIndex)
   MsgBox "Item selected was " & ComboData
End Sub

Private Sub Form_Load()
Combo1.AddItem ("dog")
Combo1.AddItem ("cat")

End Sub



Steve wrote: 
For VB.Net 2005, How do you read the text of a combobox
item?

combobox1.items.Add("Tokyo")

How do I read the string "Tokyo"?

P.S. I'm still looking for a Reference manual that will
explain this
kind of stuff, if anyone has any suggestions. I have four
VB.Net
reference books and not one can answer this basic question.
How
pathetic is that?
.
_ 


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://grou
ps.yahoo.com/group/helpwithvb/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://
groups.yahoo.com/group/helpwithvb/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:helpwithvb-digest@yahoogroups.com 
    mailto:helpwithvb-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    helpwithvb-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
read combobox text
user name
2006-12-06 11:34:54

ComboBox1.SelectedItem should give you what you are looking for.

 

 

 


From: helpwithvb@yahoogroups.com [mailto:helpwithvb@yahoogroups.com] On Behalf Of Tim Lewis
Sent: Monday, December 04, 2006 8:36 PM
To: helpwithvb@yahoogroups.com
Subject: Re: [helpwithvb] read combobox text

 

Steve, I am not sure in .Net. ; Hopefully someone will chime in.  I have included a VB6 example.&nbsp; .Net should be similar.

Private Sub Combo1_Click()
Dim ComboData As String
Dim ComboListIndex As Integer
If Combo1.ListIndex = -1 Then Exit Sub
 ;  ComboListIndex = Combo1.ListIndex
&nbsp;  ComboData = Combo1.List(ComboListIndex)
&nbsp;  MsgBox "Item selected was " & ComboData
End Sub

Private Sub Form_Load()
Combo1.AddItem ("dog")
Combo1.AddItem ("cat")

End Sub



Steve wrote:

eGroups.com" type=cite>

For VB.Net 2005, How do you read the text of a combobox item?

combobox1.items.Add(&quot;Tokyo&quot;)

How do I read the string "Tokyo&quot;?

P.S. I'm still looking for a Reference manual that will explain this
kind of stuff, if anyone has any suggestions. I have four VB.Net
reference books and not one can answer this basic question. How
pathetic is that?

 

__._,_.___ __,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )