简体   繁体   中英

Change text value after item is selected in Combobox

I have a combobox with value in and extra value in the brackets "()", I want to find out if there is a way, that when I select a value it only shows the first part of the string, not the whole selected value.

I just want the first part of the description to be shown in the combobox text.

我只想显示说明的第一部分

You can extract the desired value using Substring() . If you want the first 3 characters you can do it like this:

string.Substring(0,3);

If you want to change the text of the item, after it is selected, you will have to use an event of the ComboBox :

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox.SelectedIndex > -1)
            {
                string s = comboBox.GetItemText(this.comboBox.SelectedItem).Substring(0, 3);
                this.BeginInvoke((MethodInvoker)delegate { this.comboBox1.Text = s; });
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM