Question

How can I use switch statement in my case, if I'm using listBox?

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.GetSelected(0))
                richTextBox1.Text = "0";
            else if (listBox2.GetSelected(1))
                richTextBox1.Text = "1";

        }
Was it helpful?

Solution

Try the SelectedItem property.

switch (listBox2.FindString(listBox2.SelectedItem.ToString()))//find the index of the selected string
{
    case 0:
        richTextBox1.Text = "0";
        break;
    ...
}

Although, it would be more effective then to just call

//Do the same as above, then convert to string
richTextBox1.Text = listBox2.FindString(listBox2.SelectedItem.ToString()).ToString();

As this gets the selected item, finds its index, then convert the index to a string.

OTHER TIPS

switch (listBox2.SelectedIndex)
{
    case 0:
        richTextBox1.Text = "0";
        break;
    case 1:
        richTextBox1.Text = "1";
        break;
}

Or:

richTextBox1.Text = listBox2.SelectedIndex.ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top