Click here to Skip to main content
14,735,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have an Listbox Each Time when i select the listbox item the value should display in diffrent textbox i used my code bt fr each click it takes the value as 1 what should i do here pls Help...

try
            {
                int i = ListBox1.GetSelectedIndices().Count();
             //   int i = ListBox1.SelectedItems.Count;
               
                    if (i == 1)
                    {
                ;
                        TextBox2.Text = ListBox1.SelectedItem.ToString();
                    }
                    if (i == 2)
                    {

                       
                        TextBox3.Text = ListBox1.SelectedItem.ToString();
                    }
                 if (i == 3)
                 {

                     TextBox4.Text = ListBox1.SelectedItem.ToString();
                 }
                 if (i == 4)
                 {

                     TextBox5.Text = ListBox1.SelectedItem.ToString();
                 }
                 if (i == 5)
                 {

                     TextBox6.Text = ListBox1.SelectedItem.ToString();
                 }
                 if (i == 6)
                 {

                 
                     TextBox7.Text = ListBox1.SelectedItem.ToString();
                 }
                 if (i == 7)
                 {

                     TextBox8.Text = ListBox1.SelectedItem.ToString();
                 }
                 if (i > 8)
                 {

                     ListBox1.Enabled = false;
                     throw new Exception();
                 }
            }
            catch (Exception ex) {  }
        }
Posted
Comments
Samresh.ss 24-Jul-13 9:12am
   
GetSelectedIndices() returns An array of integers, each representing the index of a selected item in the list box.
ref - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.getselectedindices.aspx
So when you select an item and have a count() on getselectedindices(), it will return you 1, as you selected 1 item.

You can use SelectedIndex property instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900