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] "Jason Barnett" <jwb@wbai.com> wrote in message
news:eNF1vDM2FHA.3120@TK2MSFTNGP10.phx.gbl...
> 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...".
>
>