listbox to textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • faha
    New Member
    • Feb 2012
    • 3

    #1

    listbox to textbox

    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.AddRa nge(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_Select edIndexChanged( object sender, EventArgs e)
    {
    lb1 = listb1;
    for (int i = 0; i < lb1.SelectedIte ms.Count; i++)

    {


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

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

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

    at.Text = lb1.SelectedIte ms[3].ToString();
    }
    This code isnt't working and when it reaches
    wt.Text = lb1.SelectedIte ms[1].ToString();
    it throws an indexoutofbound exception
    however there are 4 items in da listbox.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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 :)

    Comment

    • faha
      New Member
      • Feb 2012
      • 3

      #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_Select edIndexChanged( object sender, EventArgs e)
      {
      lb1 = listb1;
      for (int i = 0; i < lb1.SelectedIte ms.Count; i++)

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

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

      case "2": if (lb1.SelectedIt ems[i].ToString() == "Water")
      wt.Text = lb1.SelectedIte ms[i].ToString();
      break;
      default: if (lb1.SelectedIt ems[i].ToString() == "Air")
      Ab.Text = lb1.SelectedIte ms[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.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        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..
        Code:
        switch (valida)
        {
          case "Engine":
            ...
        }
        ...?

        Comment

        • faha
          New Member
          • Feb 2012
          • 3

          #5
          Thanks that's what i needed to do...It works as expected now.

          Comment

          Working...