2012-07-06 27 views
0

我的應用程序中有一個組合框,有17個不同的選項。在我的代碼中,我對這17個選項都有if語句。爲組合框中的每個項目分配一個字符串

int colorchoice = colorSelect.SelectedIndex; 
if (colorchoice == 0) 
{ 
textBox1.AppendText("the color is black"); 
} 
if (colorchoice == 1) 
{ 
textBox1.AppendText("the color is dark blue"); 
} 
and so on... 

我已經能夠打印的內容在組合框中選擇,即黑色或深藍色,但是,我需要的是印有沒有空格或蓋。

if (colorchoice == 0) 
{ 
textBox1.AppendText("the color is " + colorSelect.SelectedText); 
} 

所以結果應該是黑色或暗藍色。我怎麼能做到這一點,而不需要改變我的組合框中的大寫字母和空格,所以它看起來不錯,但會打印出我需要的東西。

我的想法是爲17個選項中的每一個分配一個字符串,但我一直無法弄清楚如何做到這一點。

任何幫助表示讚賞。謝謝!

回答

4

Q1however, I need what is printed to have no spaces or caps.

ANS1

textBox1.AppendText("the color is " 
     + colorSelect.SelectedText.ToLower().Replace(" ", "")); 

Q2:How could I do this without changing the caps and spaces in my combo box so it still looks nice but will print out what I need?

答2:上面的代碼不會對您的組合框任何影響。

Q3:My idea was to assign a string to each of the 17 options, but I have not been able to figure out how to do this.

ANS3:你爲什麼不創建項目的數組一樣

string[] myarray = new string[]{ "Black", "Dark Blue" , ....}; 

然後用它作爲

textBox1.AppendText("the color is " + 
    myarr[colorSelect.SelectedIndex].ToLower().Replace(" ", "")); 
相關問題