2013-07-23 484 views
0

我有这样的组合框的项目:修剪组合框的SelectedItem

A6 - Tiger 
A79 - Eagle 
B6789- Elephant 
B69679 - Monkey 
C67 - Whale 
D - Dragon 

我怎么能显示selectedItem属性为TextBox控件以及只有串虎,鹰大象......没有A6,A79,B6789?

我在一个固定数量的焦炭的工作时,用这个:

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

'temp.Split( “ - ”)[1] .Trim()'? – Sayse

回答

1

我假设“A6 - 老虎”是你的文本的格式。
你可以试试这个:

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

...谢谢:) –

2

假设你有SelectedItem:像你想只显示A6

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

我认为他的'temp'是他想要打印的项目:) +1 – Sayse

0

你的代码,在我看来, 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; 
} 
0

这应该工作

string[] splittedValues = comboBox1.Text.Trim().Split('-'); 
    if(splittedValues.Length==2) 
     textBox1.Text = splittedValues[1].Trim();