PC Review


Reply
Thread Tools Rate Thread

A Combobox Question (Windows Forms)

 
 
Ing. Rajesh Kumar
Guest
Posts: n/a
 
      3rd Aug 2004
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


 
Reply With Quote
 
 
 
 
SStory
Guest
Posts: n/a
 
      3rd Aug 2004
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:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      4th Aug 2004
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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ComboBox Item (Windows Forms) Jerad Rose Microsoft Dot NET 4 27th Apr 2007 10:59 PM
Tooltip on Combobox disappears - Windows Forms bug? Jake Montgomery Microsoft Dot NET Framework Forms 0 26th Feb 2007 08:40 PM
Tooltip on Combobox disappears - Windows Forms bug? Jake Montgomery Microsoft C# .NET 0 6th Feb 2007 04:12 PM
Windows Forms, CheckBox, RadioButton, ComboBox and database :( rozrabiak Microsoft C# .NET 1 26th Aug 2004 03:19 PM
System.Windows.Forms.ComboBox genc ymeri Microsoft C# .NET 4 24th Mar 2004 08:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:48 AM.