all groups > vb.net controls > october 2005 >
You're in the

vb.net controls

group:

I can't assign a value to a ComboBox's Text property during its SelectedIndexChanged event.


I can't assign a value to a ComboBox's Text property during its SelectedIndexChanged event. Jason Barnett
10/24/2005 12:32:38 PM
vb.net controls:
Could someone shed some light on this problem I've been having with the
ComboBox.

I would like to change the text property of a ComboBox during its
SelectedIndexChanged event. I have a "Browse..." item as the first item in
the ComboBox and I open a dialog box when it is selected. Then, based on
the options selected in the dialog box, I would like to replace the selected
item ("Browse...") with a string representation of the options selected.
The problem is that the gui displays the selected item, regardless of having
changed the Text property during the SelectedIndexChanged event.

Here is a snippet of code that demonstrates the fundamental idea:

---------------------------------------------------------------------------------------
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If ComboBox1.SelectedIndex = 0 Then ComboBox1.Text = "options string"

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

ComboBox1.Items.Add("Browse...")

End Sub

--------------------------------------------------------------------------------------

I've tried other events (such as SelectedValueChanged, and
SelectionChangeCommitted), but they all produce the same results. When
stepping through the code it appears that the options string is assigned to
the text property, but the gui shows "Browse...".

Re: I can't assign a value to a ComboBox's Text property during its SelectedIndexChanged event. Jason Barnett
11/4/2005 10:26:22 AM
I believe I may have come up with a workable solution, following these
steps:

- I subclassed the ComboBox and added implemented MessageFilter.
- I set a flag when SelectionChangeCommitted fires.
- Then I filter for a Paint event and fire a custom event.

If anyone has a better solution, please let me know. In the meantime, here
is an example:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Other...")
ComboBox1.Text = "Select an item"
End Sub

Private Sub ComboBox1_SelectionChangeComplete(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeComplete
ComboBox1.Text = "A Value"
End Sub

End Class

Public Class MyComboBox
Inherits ComboBox
Implements IMessageFilter

Private Const WM_PAINT As Integer = &HF

Private iSelectChangeCommit As Boolean = False

''' <summary>
''' Fires when a selection change has completed and before the control
is
''' painted.
''' </summary>
Public Event SelectionChangeComplete(ByVal sender As Object, ByVal e As
System.EventArgs)

Public Sub New()
Application.AddMessageFilter(Me)
End Sub

Private Sub MyComboBox_SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.SelectionChangeCommitted
iSelectChangeCommit = True
End Sub

Private Function PreFilterMessage(ByRef m As
System.Windows.Forms.Message) As Boolean Implements
System.Windows.Forms.IMessageFilter.PreFilterMessage
If m.Msg = WM_PAINT AndAlso iSelectChangeCommit Then
RaiseEvent SelectionChangeComplete(Me, Nothing)
Me.SelectionStart = 0
Me.SelectionLength = Len(Me.Text)
iSelectChangeCommit = False
End If
End Function

End Class


[quoted text, click to view]

Re: I can't assign a value to a ComboBox's Text property during its SelectedIndexChanged event. Jason Barnett
11/11/2005 1:45:24 PM
I found a bug in my code...

With a recent project, I received multiple WM_PAINT messages before the
iSelectChangeCommit variable was reset. This caused an infinite loop,
raising SelectionChangeComplete continuously. The following code reflects
the correct order of operations (Note that I reset the variable as soon as I
enter the If block).

If m.Msg = WM_PAINT AndAlso iSelectChangeCommit Then
iSelectChangeCommit = False
RaiseEvent SelectionChangeComplete(Me, Nothing)
Me.SelectionStart = 0
Me.SelectionLength = Len(Me.Text)
End If

Hope this hasn't caused anyone grief when trying to implement my solution.


[quoted text, click to view]

AddThis Social Bookmark Button