Share via


Need to change Text in combobox when user changes selection

Question

Friday, August 19, 2011 8:28 AM

I have a combobox on a form with the dropdownstyle set to drop down. Users can either salect a value from the list, or type in any value they wish. This all works fine. However, when one particular item in the list is selected I wish to set a different value. Unfortunately I don't appear to be able to set the combobox Text property from within any of the events that fire when the selected item is changed. Any suggestions how to do this?

Here is some example code that demonstrates the problem:

Public Class Form1

  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    With ComboBox1
      ' dropdown style allows text to be set to what is not in the list
      .DropDownStyle = ComboBoxStyle.DropDown
      ' populate the combo list
      Dim L = New List(Of KeyValuePair(Of String, String))
      L.Add(New KeyValuePair(Of String, String)("Key0", "Value 0"))
      L.Add(New KeyValuePair(Of String, String)("Key1", "Value 1"))
      .DisplayMember = "Value"
      .ValueMember = "Key"
      .DataSource = L

    End With
  End Sub

  Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' This works fine
    ComboBox1.Text = "Text set from button click"
  End Sub

  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If ComboBox1.SelectedIndex = 0 Then
      ' This doesn't work!!
      ComboBox1.SelectedIndex = -1
      ComboBox1.Text = "Text set from combo list selection change"
    End If
  End Sub
End Class

All replies (6)

Friday, August 19, 2011 12:34 PM ✅Answered | 1 vote

I expect to see the text displayed on the screen change to "Text set from combo list selection change" when I select the first item in the list, but it does not.

Yep, i 'hope' now noticed your exact problem. You want to see the text property applied on combobox "eventhough" just after selectedindex is set to -1?

Though it's weird, here is a workaround by using delegates...

I'm still sampling If ComboBox1.SelectedIndex = 0 Then..for the sake of your original example, so:

** Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged**
**        If ComboBox1.SelectedIndex = 0 Then**
**             ComboBox1.SelectedIndex = -1**
**            ' set text to "Text set from combo list selection change"**
**            Me.BeginInvoke(New ChangeTextDelegete(AddressOf ChangeText))**
**        End If**
**    End Sub**

**    Private Delegate Sub ChangeTextDelegete()**

**    Public Sub ChangeText()**
**        ComboBox1.Text = "Text set from combo list selection change"**
**    End Sub**

...seems working fine. The selectedindex will be -1 and the corresponding text value will be applied at the same time if combobox.selectedindex = 0 (you can omit this condition as you wish). Forget timer for this purpose.

HTH.

Best regards, Saygılarımla, Onur Güzel

[Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

](http://forum.yazgelistir.com/User_Profile.aspx?userId=878&siteId=0)Microsoft Haber Grupları Profilim (VB.NET)


Friday, August 19, 2011 9:19 AM

Pay attention to the statement in SelectedIndexChanged event:

If ComboBox1.SelectedIndex = 0 Then..

Are you sure that user (or you) is always choosing first item (item index zero)? If first item isn't selected, the selectedindex won't be set to -1 (unselected-empty). Otherwise, it seems working fine.

HTH.

Best regards, Saygılarımla, Onur Güzel

[Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

](http://forum.yazgelistir.com/User_Profile.aspx?userId=878&siteId=0)Microsoft Haber Grupları Profilim (VB.NET)


Friday, August 19, 2011 9:40 AM

Thanks for your reply.

When one particular item in the list is selected I wish to set a different value. In this example, I have used the first item, i.e. SelectedIndex=0. Sorry if this was not clear from my original message.

You said "Otherwise, it seems working fine". It certainly doesn't work for me (VS2010). I expect to see the text displayed on the screen change to "Text set from combo list selection change" when I select the first item in the list, but it does not.


Friday, August 19, 2011 9:49 AM

Just to be absolutely clear, what is happening: The code within the if is being executed, and the Text property is being updated. However, it is then being changed again after the event handler completes, so the value that I set is not being displayed. I have tried the SelectedValueChanged and the SelectionChangeCommitted events too, and none of them allow me to change the Text displayed in the combobox.

 


Friday, August 19, 2011 9:50 AM

I have managed to workaround the problem by starting a timer in the SelectedIndexChanged Event, and updating the Text property in the Timer Tick event, but this seems a bit of an ugly and cumbersome solution. Is there a better way to do this?


Friday, August 19, 2011 1:48 PM

Thanks. I'll try this. I guess the BeginInvoke is doing something similar to the timer, in that it is delaying execution until after whatever code has raised the event has finished.

Note that the problem is nothing to do with me setting SelectedInde = -1. That was just something that I added to try and get it to work, the same problem occurs with and without it.