364,033 Members | 4804 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

listbox to textbox

faha
P: 3
hi
this is faha here im new to c#
I was actually trying to load different textboxes
by multiple selecting a listbox..
I added items to the listbox and then used a for loop to iterate the listbox and accordingly enter the value in the textbox but it isnt working...Any help greatly appreciated..

lb1.Items.AddRange(new string[] {"Engine", "Water", "Air", "Fire" });
LB1 IS THE LIST BOX. This code is in the load method.

In the listbox method i have this code

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lb1 = listb1;
for (int i = 0; i < lb1.SelectedItems.Count; i++)

{


tb.Text = lb1.SelectedItems[0].ToString();

wt.Text = lb1.SelectedItems[1].ToString();

ft.Text = lb1.SelectedItems[2].ToString();

at.Text = lb1.SelectedItems[3].ToString();
}
This code isnt't working and when it reaches
wt.Text = lb1.SelectedItems[1].ToString();
it throws an indexoutofbound exception
however there are 4 items in da listbox.
Feb 6 '12 #1

✓ answered by GaryTexmo

You're performing your switch on valida, which is going to be one of "Engine", "Water", etc... but your cases are "1", "2", etc...

Why would you not go..
Expand|Select|Wrap|Line Numbers
  1. switch (valida)
  2. {
  3.   case "Engine":
  4.     ...
  5. }
...?
Share this Question
Share on Google+
4 Replies


GaryTexmo
Expert 100+
P: 1,225
That's because you're assuming multiple items are selected, which may not be the case. SelectedItems is populated as a list of the items that are selected, so it's not guaranteed to match the contents of the actual ListBox. For example... your list box contains four items, "Engine", "Water", "Air", and "Fire". If the first two are selected, SelectedItems will only contain those two items. If the 2nd and 4th are selected, SelectedItems will contain only those items. If nothing is selected, SelectedItems will be empty (or potentially null).

Hopefully you get the idea. So with your code there, you can't assume that SelectedItems[0 - 3] exists or even that it will contain the appropriate item. Also note that you're doing that assignment for the number of times as there are selected items.

Instead, you need a way to map the selected item to an actual control, then populate that control with whatever data you like... which is likely the selected item itself.

There are a number of ways you could do this... one you might consider is setting up a Hashtable object where you map a string key to a textbox value, then you can can loop over your selected items and retrieve the appropriate textbox by the string name of the selected item.

Give this a try, or try a solution of your own devising. If you have troubles with the code, post back here with what you've tried and we'll see what we can do to help :)
Feb 6 '12 #2

faha
P: 3
Hi…
The output form lets me select multiple entries however if the index is either 1 or i. It works by displaying the selected value in the text boxes.
The real problem here is im not able to tell the program to update a particular
Textbox upon a particular selection in the listbox.

I however tried using a switch case to achieve the same but then the problem here is
It executes only the default case.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lb1 = listb1;
for (int i = 0; i < lb1.SelectedItems.Count; i++)

{
string valida = lb1.SelectedItems[i].ToString();

switch (valida)
{
case "1":// if (valida == "Engine")
if(lb1.SelectedItems[i].ToString() == "Engine")
tb.Text = lb1.SelectedItems[i].ToString();
break;

case "2": if (lb1.SelectedItems[i].ToString() == "Water")
wt.Text = lb1.SelectedItems[i].ToString();
break;
default: if (lb1.SelectedItems[i].ToString() == "Air")
Ab.Text = lb1.SelectedItems[i].ToString();
break;

}
}
I’m totally new to c# and this is my first project, it would be great if you could suggest a few websites from were I could get help as Im afraid that i do not understand what you mean by hashtable object mapping.
Feb 6 '12 #3

GaryTexmo
Expert 100+
P: 1,225
You're performing your switch on valida, which is going to be one of "Engine", "Water", etc... but your cases are "1", "2", etc...

Why would you not go..
Expand|Select|Wrap|Line Numbers
  1. switch (valida)
  2. {
  3.   case "Engine":
  4.     ...
  5. }
...?
Feb 6 '12 #4

faha
P: 3
Thanks that's what i needed to do...It works as expected now.
Feb 7 '12 #5

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp