Nathan
Nathan

Reputation: 1143

Assign a string to each item in a Combo Box

I have a combo box in my application that has 17 different options. In my code, I have an if statement for all 17 of these options.

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...

I have been able to print what is selected in the combo box, i.e. Black or Dark Blue, however, I need what is printed to have no spaces or caps.

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

So the result should be black or darkblue. 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.

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.

Any help is appreciated. Thanks!

Upvotes: 0

Views: 125

Answers (1)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

Q1 however, 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?

Ans 2: The above code will not have any effect on your combobox.

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: Why don't you create an array of your items like

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

Then use it as

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

Upvotes: 4

Related Questions