|
-
Nov 13th, 2014, 05:21 PM
#31
Hyperactive Member
Re: [RESOLVED] refresh combobox values after updating its datatable and adapter
 Originally Posted by larrycav
To me, selecting the combobox is the only logical answer. You click it and it should present the desired data.
The event for what you are describing is Enter (or possibly DropDown) not SelectedIndexChanged. The Enter event happens when a control gains focus such as when the user clicks the ComboBox. SelectedIndexChanged event occurs when the user selects one of the items in the ComboBox. But you want all of the items already in the ComboBox before the user makes a selection (otherwise they may complain that the new item isn't listed; they'd have to select an item to get all of the items refreshed and then make their actual selection a second time after the ComboBox has refreshed).
I have created this small program to show the difference. It has a Button (named btnTest1) and a ComboBox (named cboTest) and has a List(Of String) that the ComboBox's DataSource is connected to:
VB.Net Code:
Public Class Start
Public lstItems As New List(Of String)
Private Sub btnTest1_Click(sender As System.Object, e As System.EventArgs) Handles btnTest1.Click
With Me.lstItems
.Add("A")
.Add("B")
.Add("C")
End With
End Sub
Private Sub cboTest1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboTest1.SelectedIndexChanged
Me.cboTest1.DataSource = Nothing
Me.cboTest1.DataSource = Me.lstItems
End Sub
'Private Sub cboTest1_Enter(sender As Object, e As System.EventArgs) Handles cboTest1.Enter
' Me.cboTest1.DataSource = Nothing
' Me.cboTest1.DataSource = Me.lstItems
'End Sub
Private Sub Start_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.lstItems.Add("Hello")
Me.cboTest1.DataSource = Me.lstItems
End Sub
End Class
When the program runs, the ComboBox will initially just contain the word "Hello". When the button is clicked, the letters "A", "B", and "C" get added to the List. Then one of the ComboBox's events will trigger the List to be reset as its DataSource. Currently I have the SelectedIndexChanged handling the DataSource reset, but to see how the Enter event works, just comment out the SelectedIndexChanged handler and uncomment the Enter handler. Then do these steps to see the difference:
- Run the program and drop-down the ComboBox to see its initial items (eg the word "Hello")
- Click on the Button to add some more items to the List
- Drop-down the ComboBox again. If the Enter event handler is enabled, then you'll see all of the items listed (eg "Hello", "A", "B", and "C"); at this point you can quit. If the SelectedIndexChanged handler is enabled, then it will look like nothing happened and only the word "Hello" will be listed; you'll need to continue with the remaining steps to see the new items in the List for the ComboBox.
- While the ComboBox is in its drop-down state, click on the word "Hello". The ComboBox should close at this point (showing only the editable portion with the drop-down list hidden).
- Drop-down the ComboBox again to see that the DataSource was reset when you selected the word "Hello" in the previous step.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|