ComboBox setting a value - Help!!
Folks,
After using VB 6.0 for the last 10 years, I am rather embarrassed to post this question because I could not find answer to this question.
Here's my situation
1) I have a dropdown combobox. It displays a the list of projects. The "Code" property is mapped to the "Project ID" and the "Value" property is my "project description"
2) From another Win Form, I pass the "Project ID" [code value] to my current form that has this combobox. I have to set proper "SelectedIndex" property based on the value passed to to this form. How do I do it?
I see methods like "FindExactString", "FindString" etc. I can use them if I passed the "Description" value from the other form. However, I am afraid of duplicate descriptions.
There got to be some way to set the "SelectedIndex" property if I have the "Code" and I just could not find it.
Any help will be appreciated
-Thanks again
[1007 byte] By [
Ragas] at [2007-12-24]
Sorry,
I am currently using "VB.NET" 2005 and I need help on how to do this in VB.NET [not VB6.0]
I smiled a bit at your reply because, if I did not figure out how to do this in VB6.0 in the last 10 years, I should probably quit programming :)
hehe - no worries, I Wasnt sure either - it's just you referred to VB6...
so doing it in .NET, let's give it a bash for you
So, I understand (please feel free to correct me) that you wish to set the property of the combobox to show the value being passed into your form (from another form/class)?
Is the Code Value (project ID?) added in the collection of items in the combobox?
if this is the case, you can either:
loop through each item in the combobox, check if the current item matches the string passed in, if so, set the selectedindex to this index founduse the "SelectedItem" property to select the object you want to display in the combobox:Me.theComboBox.SelectedItem = "My Item"
this would select it if the item existed in the combobox, otherwise it won't.
Does this help? is this what you are after?
Sorry, your question doesn't make a lot of sense to me. "Code" and "Value" properties? A ComboBox has an Items property...
Thanks for your reply.
Yes, I do understand I have to loop. Let me explain in detail as where I am stuck.
Just to clarify the terms I used,
Values [in VB6.0 terms] is what gets displayed in the "DropDown"
Code [in VB.6.0 terms] is what that gets saved in the "Database" when you select an item
In my E.g)
ProjectID is the Code
Project Description is the value displayed
I am binding the combobox to a database table and it automatically populates it for me
Now, I have a "project ID" say 103. Let's say 103 corresponds to a Project called "Dont Mess With Texas". How do I loop through the dropdown and compare each item and see if it matches with 103 and then set the index to display "Dont Mess with Texas" ? I need the "Code"
The selectedValue, SelectedItem returns Object. How do you parse this object to get the "Project ID" ? Makes Sense?
I understand what you mean. You want the correct item to display in the drop down list when given a project ID correct?
The combobox has to have a datasource, as you have done.
The combobox has to have a displaymember, project name in this case
The combobox has to have a ValueMember, ProjectID in this case.
so now, when you get the project ID, you want to display the correct project Name.
Set the "SelectedValue" property of the combobox to the value passed in.
So....
The combobox has to have these properties set:
DataSource (you've done this)displayMember (set this to the project name field name in the datasource)ValueMember (set this to the project ID field name in the datasource)then when you get the project ID, simply set the "SelectedValue" property of the combobox to the project ID given:
Me.theComboBox.SelectedValue = projectID
does this help?
There are a few ways to do this. Here is one example.
Create a ComboBoxItem class:
Public Class ComboBoxItem
Private baseText, baseValue As String
Public Sub New()
Me.baseText = ""
Me.baseValue = ""
End Sub
Public Sub New(ByVal text As String)
Me.baseText = text
Me.baseValue = ""
End Sub
Public Sub New(ByVal text As String, ByVal value As String)
Me.baseText = text
Me.baseValue = value
End Sub
Public Property Text() As String
Get
Return Me.baseText
End Get
Set(ByVal value As String)
Me.baseText = value
End Set
End Property
Public Property Value() As String
Get
Return Me.baseValue
End Get
Set(ByVal value As String)
Me.baseValue = value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.baseText
End Function
End Class
Then create a simple windows application to test this, one form, one combobox:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add(New ComboBoxItem("test", "test value"))
ComboBox1.Items.Add(New ComboBoxItem("test2", "test2 value"))
ComboBox1.Items.Add(New ComboBoxItem("test3", "test3 value"))
ComboBox1.Items.Add(New ComboBoxItem("test4", "test4 value"))
ComboBox1.SelectedIndex = 0
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim item As ComboBoxItem = DirectCast(ComboBox1.SelectedItem, ComboBoxItem)
MessageBox.Show(item.Text + " - " + item.Value)
End Sub
End Class
Since a combobox accepts an item as input, you can place any item youd like in there. Like I said though, this is just one simple solution.To answer your other question, you would use the ToString function to get the text value of the object. For example, SelectedValue.ToString. I hope I understood your question. Good luck.
-Joe
Folks,
You guys are just awesome. Thanks for the reply. Here's how I fixed it
First I did this,
For each obj as Object in ProjectIDComboBox.Items
'At run time I added obj to "Quick Watch"
'It returned an object of type "DataRowView"
'DataRowView object again has a property called "Row" which was of the type of my data table row
'So I simply did this
if obj.Row.ProjectID = searchProjectID then
ProjectIDComboBox.SelectedItem = obj
Exit For
End if
Next
The catch here is that there will be no "Intellisense" support [you kinda get used to it and keep digging your answers within the intellisense options]. You can type cast the object as your data table row. But to do that, you will have to first run the application and figure that out at runtime.
So there you go...and thanks a lot friends for your help and support!!
a better way would have been to use the displaymember/valuemember and selecteditem properties - best practice and saves time ;-)
or going with your example above, if your trying to set the project ID/show it in the combobox with the value given, then just use the SelectedItem property as this will automatically select the projectID if found in the collection rather than going through the loop
Can we provide the "Project Id" value to selecteditem property? I dont think we can use that property to actually
- Find out the project id
- and then set it.
can you please give example?
Thanks
Here's a modified MS example of what Ahmed Ilyas was suggesting:
Public
Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "ComboBox Sample3"
ComboBox1.Location =
New Point(24, 16)ComboBox1.Name =
"ComboBox1"ComboBox1.Size =
New Size(232, 130)Dim USStates As New ArrayList()USStates.Add(
New USState("Alabama", "AL"))USStates.Add(
New USState("Washington", "WA"))USStates.Add(
New USState("West Virginia", "WV"))USStates.Add(
New USState("Wisconsin", "WI"))USStates.Add(
New USState("Wyoming", "WY"))'AddHandler ComboBox1.SelectedValueChanged, AddressOf ComboBox1_SelectedValueChangedComboBox1.DataSource = USStates
ComboBox1.DisplayMember =
"LongName"ComboBox1.ValueMember =
"ShortName"End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickWith Me.ComboBox1.SelectedValue=
"WY"End WithEnd SubEnd
ClassPublic
Class USStatePrivate myShortName As StringPrivate myLongName As StringPublic Sub New(ByVal strLongName As String, ByVal strShortName As String)Me.myShortName = strShortNameMe.myLongName = strLongNameEnd Sub 'NewPublic ReadOnly Property ShortName() As StringGetReturn myShortNameEnd GetEnd PropertyPublic ReadOnly Property LongName() As StringGetReturn myLongNameEnd GetEnd PropertyPublic Overrides Function ToString() As StringReturn Me.ShortName + " - " + Me.LongNameEnd Function 'ToStringEnd
Class 'USState