C# 修剪SelectedItem组合框

C# 修剪SelectedItem组合框,c#,winforms,combobox,trim,C#,Winforms,Combobox,Trim,我有以下组合框项目: A6 - Tiger A79 - Eagle B6789- Elephant B69679 - Monkey C67 - Whale D - Dragon 我如何将selectedItem显示到文本框中,只显示字符串Tiger、Eagle和Elephant。。。没有A6、A79、B6789 我在处理固定数量的字符时使用了此选项: string temp = comboBox1.Text; char[] array1 = temp.ToCharArray(); textBo

我有以下组合框项目:

A6 - Tiger
A79 - Eagle
B6789- Elephant
B69679 - Monkey
C67 - Whale
D - Dragon
我如何将selectedItem显示到文本框中,只显示字符串Tiger、Eagle和Elephant。。。没有A6、A79、B6789

我在处理固定数量的字符时使用了此选项:

string temp = comboBox1.Text;
char[] array1 = temp.ToCharArray();
textBox1.Text = "" + array1[0] + array1[1];

假设您有
SelectedItem

textBox1.Text = theSelectedItem.Split('-')[1].Trim()

假设您有
SelectedItem

textBox1.Text = theSelectedItem.Split('-')[1].Trim()

在我看来,您的代码似乎只想显示A6、A79、B6789,。。。 因此,我发布了这两种方法的解决方案

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{//Last
    string s = (string)listBox1.SelectedItem;

    string last = s.Substring(s.LastIndexOf(' ') + 1);

    textBox1.Text = last;

    listBox1_SelectedIndexChanged_first(sender, e);
}


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{//First
    string s = (string)listBox1.SelectedItem;
    string first = s.Substring(0, s.IndexOf(' '));

    textBox1.Text = first;
}

在我看来,您的代码似乎只想显示A6、A79、B6789,。。。 因此,我发布了这两种方法的解决方案

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{//Last
    string s = (string)listBox1.SelectedItem;

    string last = s.Substring(s.LastIndexOf(' ') + 1);

    textBox1.Text = last;

    listBox1_SelectedIndexChanged_first(sender, e);
}


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{//First
    string s = (string)listBox1.SelectedItem;
    string first = s.Substring(0, s.IndexOf(' '));

    textBox1.Text = first;
}
这应该行得通

  string[] splittedValues =  comboBox1.Text.Trim().Split('-');
  if(splittedValues.Length==2)
      textBox1.Text = splittedValues[1].Trim();
这应该行得通

  string[] splittedValues =  comboBox1.Text.Trim().Split('-');
  if(splittedValues.Length==2)
      textBox1.Text = splittedValues[1].Trim();
我假设“A6-Tiger”是你们文本的格式。
你可以试试这个:

            if (comboBox1.SelectedIndex > 0)
        {
            textBox1.Text = comboBox1.Text.Substring(comboBox1.Text.IndexOf('-') + 1).Trim();
        }
我假设“A6-Tiger”是你们文本的格式。
你可以试试这个:

            if (comboBox1.SelectedIndex > 0)
        {
            textBox1.Text = comboBox1.Text.Substring(comboBox1.Text.IndexOf('-') + 1).Trim();
        }

temp.Split(“-”[1]。Trim()
temp.Split(“-”[1]。Trim()
?我假设他的
temp
是他想要打印的物品:)+1我假设他的
temp
是他想要打印的物品:)+1