By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

A Combobox Question (Windows Forms)

P: n/a
Hi everybody
I am a web developer now trying to do something in WinForms using VB.NET and
have a small problem with ComboBox. I just wanted to ask what is the
equivalent to the following web code :

Dim MyString as String
Do While DR.Read()
Select Case DR("Column_1")
Case "X" : MyString = "XXXX"
Case Else : MyString = DR("Column_1")
End Select
DropDownList1.Items.Add(New ListItem(MyString, DR("Column_2")))
Loop

'Could also be : For i = 0 To 5 and then DropDownList1.Items.Add(New
ListItem("Item " & i, i))

In WinForms i can add only one item as :
DropDownList1.Items.Add("XXXX")
but i need to add KEY and VALUE.
I read a lot in the MSDN and found a way to do this using dataset,
DisplayMember, ValueMember and a helper class but not an easy way.

Thanks in advace
Raja
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Raja, the basic idea in winforms is to

create a class for your list items.
put whatever you want into it.
Override the ToString method and that is what the list will display but the
selected item will be an instance of the class you defined and contain
whatever you wanted.

HTH,

Shane
"Ing. Rajesh Kumar" <iambad(Without This)@post.cz> wrote in message
news:eX**************@TK2MSFTNGP11.phx.gbl...
Hi everybody
I am a web developer now trying to do something in WinForms using VB.NET and have a small problem with ComboBox. I just wanted to ask what is the
equivalent to the following web code :

Dim MyString as String
Do While DR.Read()
Select Case DR("Column_1")
Case "X" : MyString = "XXXX"
Case Else : MyString = DR("Column_1")
End Select
DropDownList1.Items.Add(New ListItem(MyString, DR("Column_2")))
Loop

'Could also be : For i = 0 To 5 and then DropDownList1.Items.Add(New
ListItem("Item " & i, i))

In WinForms i can add only one item as :
DropDownList1.Items.Add("XXXX")
but i need to add KEY and VALUE.
I read a lot in the MSDN and found a way to do this using dataset,
DisplayMember, ValueMember and a helper class but not an easy way.

Thanks in advace
Raja

Nov 20 '05 #2

P: n/a
Raja,

You can do it in almost the same way you wrote.

DropDownList.items.add(mystring)
should be enough in that case
and than set the combobox style to dropdownlist

with a dataset it would be because you have already your connection and
selectstring
\\\
myLoadedSwitch = false
dim ds as new dataset
dim da as new xxxDataadapter(sqlstring,conn)
da.fill(ds)
ds.tables(0).columns.add(see links)
DropDownList1.datasource = ds.tables()
DropDownList1.displaymember = "MynewColumn"
DropDownList1.valuemember = "Myvaluecolumn"
///

http://msdn.microsoft.com/library/de...ctortopic4.asp

http://msdn.microsoft.com/library/de...ssionTopic.asp

The advantage from the last is that you have extra a value column and pass a
bug with the selection change commitet event.

When you use it you have to set in the the indexchange event (there are
alternatives too)
If myLoadedSwitch = true
' This because some anoying load efects of the combobox.

As last use as control name someting as cmb not dropdownlist, these are of
the few controls where the webcontrols and the winforms have a well distinct
in there name.

(All is typed in this message and not checked on typos)

I hope this helps?

Cor
Hi everybody
I am a web developer now trying to do something in WinForms using VB.NET and have a small problem with ComboBox. I just wanted to ask what is the
equivalent to the following web code :

Dim MyString as String
Do While DR.Read()
Select Case DR("Column_1")
Case "X" : MyString = "XXXX"
Case Else : MyString = DR("Column_1")
End Select
DropDownList1.Items.Add(New ListItem(MyString, DR("Column_2")))
Loop

'Could also be : For i = 0 To 5 and then DropDownList1.Items.Add(New
ListItem("Item " & i, i))

In WinForms i can add only one item as :
DropDownList1.Items.Add("XXXX")
but i need to add KEY and VALUE.
I read a lot in the MSDN and found a way to do this using dataset,
DisplayMember, ValueMember and a helper class but not an easy way.

Thanks in advace
Raja

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.