|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Combobox and Values - Help PleaseI'm pretty new to programming with vb.net. I'm stuck on a problem: I need to populate a combobox with items and a value for each item. For example if it were for countries: Item - Value Canada - 1 France - 2 Egypt - 3 Brazil - 1 Germany - 2 Spain - 2 and such... I want the user to see the Item (Country name) but when he selects the country, I want another event to happen based on the Value. I was able to do such things with old ASP and VBScript code, but with ..net it doesn't seem the same. I tried: Combobox1.Items.Add("Canada", "U"), but it didn't work. Any help would be appreciated. Thanks...
Show quote
Hide quote
"TheGanjaMan" <theganjaman@somplace.invalid> wrote in message news:Xns976A8BE526517TheGanjaMan@207.46.248.16... The Add method expects a single object to be passed to it. If you want to add a combo item and item data like you could in vb6> Hi all, > > I'm pretty new to programming with vb.net. I'm stuck on a problem: > I need to populate a combobox with items and a value for each item. > > For example if it were for countries: > Item - Value > Canada - 1 > France - 2 > Egypt - 3 > Brazil - 1 > Germany - 2 > Spain - 2 > and such... > > I want the user to see the Item (Country name) but when he selects the > country, I want another event to happen based on the Value. > I was able to do such things with old ASP and VBScript code, but with > .net it doesn't seem the same. > I tried: Combobox1.Items.Add("Canada", "U"), but it didn't work. > > Any help would be appreciated. > > Thanks... create a ComboIItem class and pass an instance to the Add method. ======================= Public Class ComboItem ' Declare the variable the property uses. Private strItemData As String Private strItem As String Public Property ItemData() As String Get Return strItemData End Get Set(ByVal ItemData As String) strItemData = ItemData End Set End Property Public Property Item() As String Get Return strItem End Get Set(ByVal Item As String) strItem = Item End Set End Property Public Overrides Function ToString() As String Return strItem End Function Public Sub New() End Sub Public Sub New(ByVal Item As String, ByVal ItemData As String) strItem = Item strItemData = ItemData End Sub End Class ====================== Then you can add the Items and ItemData to the ComboBox using Combobox1.Items.Add(New ComboItem("Canada", "1") I hope this helps. -- Al Reid
Show quote
Hide quote
"Al Reid" <arei***@reidDASHhome.com> wrote in Thanks... I'm pretty new to OOP so I think I need time for the code in the news:#opNZFWMGHA.4052@TK2MSFTNGP15.phx.gbl: > "TheGanjaMan" <theganjaman@somplace.invalid> wrote in message > news:Xns976A8BE526517TheGanjaMan@207.46.248.16... >> Hi all, >> >> I'm pretty new to programming with vb.net. I'm stuck on a problem: .... >> >> Any help would be appreciated. >> >> Thanks... > > The Add method expects a single object to be passed to it. If you > want to add a combo item and item data like you could in vb6 create a > ComboIItem class and pass an instance to the Add method. > > ======================= > Public Class ComboItem > ' Declare the variable the property uses. .... > End Class > ====================== > > Then you can add the Items and ItemData to the ComboBox using > > Combobox1.Items.Add(New ComboItem("Canada", "1") > > I hope this helps. > > -- > Al Reid > > class to sink in. I did put your code into a new class in my project and now my combobox accepts the values I assign to it. but when I try to read the combobox1.selectedvalue I still get nothing. What am I doing wrong? I have the following code to fill the combobox: Public Sub FillMenu() Dim conn = DBConn() Dim sqlstr As String = "select * from Form_Table" Dim rs = DBrs(conn, sqlstr) Do While rs.read ComboBox1.Items.Add(New ComboItem(rs("form_name"), rs("T_id"))) Loop conn.close() End Sub The above code works as it fills the combobox... but when I do this: Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox2.Text = ComboBox1.SelectedValue & " - " & ComboBox1.SelectedItem End Sub I get en error and when I do this: Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox2.Text = ComboBox1.SelectedValue End Sub I get nothing... any ideas? (Thanks very much for the example code...)
Show quote
Hide quote
"TheGanjaMan" <theganjaman@somplace.invalid> wrote in message news:Xns976A95E3821BETheGanjaMan@207.46.248.16... SelectedItem returns an object as well. In this case it's an instance of> Thanks... I'm pretty new to OOP so I think I need time for the code in the > class to sink in. > I did put your code into a new class in my project and now my combobox > accepts the values I assign to it. but when I try to read the > combobox1.selectedvalue I still get nothing. What am I doing wrong? > I have the following code to fill the combobox: > Public Sub FillMenu() > Dim conn = DBConn() > Dim sqlstr As String = "select * from Form_Table" > Dim rs = DBrs(conn, sqlstr) > Do While rs.read > ComboBox1.Items.Add(New ComboItem(rs("form_name"), rs("T_id"))) > Loop > conn.close() > End Sub > The above code works as it fills the combobox... but when I do this: > Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As > System.Object, ByVal e As System.EventArgs) Handles > ComboBox1.SelectedIndexChanged > TextBox2.Text = ComboBox1.SelectedValue & " - " & > ComboBox1.SelectedItem > End Sub > I get en error and when I do this: > Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As > System.Object, ByVal e As System.EventArgs) Handles > ComboBox1.SelectedIndexChanged > TextBox2.Text = ComboBox1.SelectedValue > End Sub > > I get nothing... any ideas? > (Thanks very much for the example code...) > the ComboItem class. Therefore, you need to access the properties of the object in order to use them. TextBox2.Text = CType(ComboBox1.SelectedItem, ComboItem).Item <- to get the country name OR TextBox2.Text = CType(ComboBox1.SelectedItem, ComboItem).ItemData <- to get the country code -- Al Reid "Al Reid" <arei***@reidDASHhome.com> wrote in [snip...]news:uSSqOXWMGHA.2472@TK2MSFTNGP11.phx.gbl: Show quoteHide quote > Thank you ... it worked like a charm...> SelectedItem returns an object as well. In this case it's an instance > of the ComboItem class. Therefore, you need to access the properties > of the object in order to use them. > > TextBox2.Text = CType(ComboBox1.SelectedItem, ComboItem).Item <- to > get the country name OR TextBox2.Text = CType(ComboBox1.SelectedItem, > ComboItem).ItemData <- to get the country code > > -- > Al Reid > > > and saved me hours of frustration and pulling off whats left of the hair on my head. TGM "TheGanjaMan" <theganjaman@somplace.invalid> schrieb: <URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/029eaf619366a274>> I'm pretty new to programming with vb.net. I'm stuck on a problem: > I need to populate a combobox with items and a value for each item. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> |
|||||||||||||||||||||||