0

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!

1 Answer 1

4

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(" ", ""));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.