Omarrrio
Omarrrio

Reputation: 1142

use a variable index without using a loop

i was wondering if there is a way to use an index variable, without using the for or foreach loop, like in this example (what i've made but doesn't work): i have 3 integer arrays, one goes for the listbox, and when i select an item on the listbox, it would take the index of the selected item, and look for that index inside the other arrays, and put the values of the 2nd and 3rd arrays inside textboxs, here is my code:

if (ListBox1.SelectedItems.Count > 0)
        {
            TextBox1.Text = jnames[ListBox1.SelectedIndex];
            TextBox2.Text = enames[ListBox1.SelectedIndex];
        }

no errors or exceptions are given, just does nothing.

Upvotes: 0

Views: 133

Answers (1)

Ken White
Ken White

Reputation: 125698

SelectedItems is more suited for a multi-select ListBox. You should just useSelectedIndex directly:

if (ListBox1.SelectedIndex > -1)
    {
        TextBox1.Text = jnames[ListBox1.SelectedIndex];
        TextBox2.Text = enames[ListBox1.SelectedIndex];
    }

You should learn to use the debugger. A breakpoint set on the if statement will show you if the condition is being met, and whether the code inside the if will ever execute.

Upvotes: 3

Related Questions