Allowing user to add to/edit combobox
-
I populate a combobox as follows:
ComboBox2.Items.Clear() ComboBox2.Items.AddRange(ScanHistoryList) ComboBox2.SelectedIndex = 0
ScanHistoryList is a string array. I want to be able to allow the user to add to/edit the combobox . How do I do this? Any help much appreciated - I can't find a property to set:^)
You always pass failure on the way to success.
-
I populate a combobox as follows:
ComboBox2.Items.Clear() ComboBox2.Items.AddRange(ScanHistoryList) ComboBox2.SelectedIndex = 0
ScanHistoryList is a string array. I want to be able to allow the user to add to/edit the combobox . How do I do this? Any help much appreciated - I can't find a property to set:^)
You always pass failure on the way to success.
-
I think you need to achive this prgamatilally by traping the events and if the text is changed while drop down event replace the older text with new one and if older text is a blank then add the new Item inthe list.Just try it:-D
Thanks I'll give that a go :)
You always pass failure on the way to success.
-
I populate a combobox as follows:
ComboBox2.Items.Clear() ComboBox2.Items.AddRange(ScanHistoryList) ComboBox2.SelectedIndex = 0
ScanHistoryList is a string array. I want to be able to allow the user to add to/edit the combobox . How do I do this? Any help much appreciated - I can't find a property to set:^)
You always pass failure on the way to success.
Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks Dave and Ciacia, I've solved it. First thing I did was turn it into a DropDown rather than DropDownList. This allows the users to edit the data:doh: Then I created the following event handlers:
Private Sub ComboBox2_SelectedTextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.TextChanged textChanged = True End Sub Private Sub ComboBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.Leave If textChanged Then ComboBox2.Items.Insert(ComboBox2.Items.Count, ComboBox2.Text) ComboBox2.SelectedIndex = ComboBox2.Items.Count - 1 End If End Sub
Note: textChanged is declared as a boolean variable. What happens then is that when the user leaves the combobox if they have changed any text: this is added to the end of the combobox array collection and this is then made the current selection. Thanks for your help guys. It's a shame that something which seems so simple can be so tricky!:-D
You always pass failure on the way to success.
-
Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007I don't see anything wrong with this approach. Quick and easy :-D
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
-
Thanks Dave and Ciacia, I've solved it. First thing I did was turn it into a DropDown rather than DropDownList. This allows the users to edit the data:doh: Then I created the following event handlers:
Private Sub ComboBox2_SelectedTextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.TextChanged textChanged = True End Sub Private Sub ComboBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.Leave If textChanged Then ComboBox2.Items.Insert(ComboBox2.Items.Count, ComboBox2.Text) ComboBox2.SelectedIndex = ComboBox2.Items.Count - 1 End If End Sub
Note: textChanged is declared as a boolean variable. What happens then is that when the user leaves the combobox if they have changed any text: this is added to the end of the combobox array collection and this is then made the current selection. Thanks for your help guys. It's a shame that something which seems so simple can be so tricky!:-D
You always pass failure on the way to success.