none
Change text of combobox on selection change event

    Question

  • Hi,

    I have problem where I've a "Browse more" item in combobox. I am opening a form containing few options, on selection of those items I want it to be Text of combobox.

    I am getting the value on the previous form but unable to display it on the combobox.

    Thanks.

    Tuesday, April 26, 2011 11:37 AM

Answers

  • So you could use the other control instead of the ComboBox. You could show one Popup below the TextBox, and show the items in the Popup. Then you could change the text property of the TextBox or select the item in the Popup and show it in the TextBox.
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 28, 2011 2:01 PM

All replies

  • There are several different ways to do this. If you are using MVVM or something similar, you just need to set the bound property to the new value (and by bound property I mean a property that's two-way bound to the ComboBox's SelectedItem).
    http://blog.voidnish.com
    Tuesday, April 26, 2011 11:56 AM
  • Another thing to note is that if the new item is not a part of the combobox's collection, you probably need to add it there as well.
    http://blog.voidnish.com
    Tuesday, April 26, 2011 11:57 AM
  • Hi...

    This is the code

    This is Form1 where there is combobox


    <ComboBox Height="23" Name="cmbName" IsEditable="True" SelectionChanged="cmbName_SelectionChanged">           
               <ComboBoxItem Content="&lt;Browse for more...&gt;" Name="BrowseMore" Selected="BrowseMore_Selected" Selector.IsSelected="False" />
            </ComboBox>

    BrowseMore frmBrowse = new BrowseMore();
    frmBrowse.ShowDialog();
    if (frmBrowse.DialogResult.HasValue && frmBrowse.DialogResult.Value)
    {
    cmbServerName.Text = frmBrowse.Selected;               
    }


    BrowseMore.cs (Form which is opened as dialog)
    public string Selected
    {
    get { return ListBox1.SelectedValue.ToString(); }
    }

    I am getting the value in Form1 but unable to set it to combobox text.

    Tuesday, April 26, 2011 12:14 PM
  • Hello,

    If the item that has been selected using the dialog is not in the ComboBox items, you need to add it first.

    Try adding the item in the combobox items:

    cmbServerName.Items.Add(frmBrowse.Selected);

    Last, select the item.

     

    Good Luck.


    Tuesday, April 26, 2011 12:24 PM
  • Hi.......

    Thanks for your quick reply.

    But I dont want to add items in combobox, I just want to change the text of combobox.

    Thanks.

    Tuesday, April 26, 2011 1:56 PM
  • So you could use the other control instead of the ComboBox. You could show one Popup below the TextBox, and show the items in the Popup. Then you could change the text property of the TextBox or select the item in the Popup and show it in the TextBox.
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 28, 2011 2:01 PM