locked
Listbox SelectedIndex Changed Event RRS feed

  • Question

  • Hello,

    Depending on selection of listbox item i should display other two textbox values. For this i have written Listbox1_SelectedIndexChanged event. When i click on listbox item this event is firing but SelectedIndex value is showing -1, so i am not getting Listbox1.SelectedItem.ToString() value at alll..

    So how can i capture listbox selected item value??

    Can you please help me on this?

    Thank you


    Diddi

    • Moved by CoolDadTx Thursday, March 14, 2013 6:47 PM Winforms related
    Thursday, March 14, 2013 6:41 PM

Answers

  • hi Diddi

    unfortunately, along with not working in C# (even though I was able to muddle through the code posted above) I have never worked in ASP.NET.
    you can keep this thread open and wait (hope?) for someone with ASP.NET experience to come across it. While your code above to pull data from the SQL db looks good, ASP might have some little things.

    or what you might do is close this thread by marking it as answered and post your question in the ASP.NET Forum. The post from me that ob marked as proposed answer could be marked as Answer.

    The good thing is you now know it is not the SelectedIndexChanged event that is the problem. Sometimes you have to slowly rule things out to figure out what the problem is. It appears that the issue is pulling the data or how you are binding the ListBox.

    two thoughts to try.

    1. comment out the line lstMinorSystem.DataBind. In a WinForm app, you would set the DataSource and then the DisplayMember and ValueMember properties (these would be equivalent to the DataTextField and DataValueField probably), but you wouldn't call the DataBind method. Maybe it's different in ASP, but worth a try. Another thing to try is to move the DataSource line above where you set the Field properties (right after the da.Fill(ds) line).

    2. After the da.Fill(ds) line, put in a line like this:

    Debug.WriteLine(ds.Tables[0].Rows.Count)

    or whatever it is in C# to see the number of Rows in the table. The reason being if the Row count is correct, then you know it is filling the dataset with data, and the problem is narrowed down to how you are databinding the ListBox.

    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    • Marked as answer by diddi10 Friday, March 15, 2013 3:51 PM
    Friday, March 15, 2013 3:19 PM

All replies

  • please post your code for the IndexChanged event.

    without seeing that we can't do much to help

    Thanks


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    Thursday, March 14, 2013 6:55 PM
  • Here is my Listbox1_SelectedIndexChanged event.

    string strMinorSystem  ="";

    strMinorSystem  = lstMinorSystem.SelectedItem.ToString()

    If (strMinorSystem == "Summary Reports")

                    {

                        txtReportName.Text = "Summary_Reports_123012";

                    }

    else

    {

    txtReportName.Text = "Unknown_Reports_123012";

    }

    I am getting error at above bold line

    strMinorSystem  = lstMinorSystem.SelectedItem.ToString())

    saying that "Object reference not set to an instance of an object".

    Thank you

         

             

                   


                   


    Diddi

    Thursday, March 14, 2013 7:08 PM
  • well you've declared your String variable (strMinorSystem) so that's not it

    that leaves lstMinorSystem as the uninstantiated object

    are you sure you have a ListBox named lstMinorSystem?

    because you mentioned ListBox1_SelectedIndexChanged as the name of the event. Which makes it look like the ListBox has it's default name (ListBox1)


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    Thursday, March 14, 2013 8:07 PM
  • Yeah previously it was ListBox1 but i chnged it to lstMinorSystem  for verywhere.

    The problem the selected value is not coming at all.. while debugging i checked 

    lstMinorSystem.SelectedIndex it showing as -1

    Let me know if you need anything else? please help me on this..

    Thank you


    Diddi

    Thursday, March 14, 2013 8:30 PM
  • and when is this error ocurring? During Form_Load or when a selection is made in the ListBox?

    This code (a VB version of yours works fine):

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMinorSystem.SelectedIndexChanged
            Dim strMinorSystem As String = ""
            strMinorSystem = lstMinorSystem.SelectedItem.ToString
            Debug.WriteLine(lstMinorSystem.SelectedIndex)
            If strMinorSystem = "Summary Reports" Then
                txtReportName.Text = "Summary_Reports_123012"
            Else
                txtReportName.Text = "Unknown_Reports_123012"
            End If
        End Sub

    so can you post the entire SelectedIndexChanged event including the Private Sub... part?

    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    Thursday, March 14, 2013 8:44 PM
  • OK, I created a C# test app added the controls and put in this code. It works fine

    so create a new app. add a textbox and name it txtReportName, and add a ListBox and name it lstMinorSystem. Right click Form1.cs in Solution Explorer, click on view code and replace everything in there with the code below. See if it works

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                lstMinorSystem.Items.Add("foo");
                lstMinorSystem.Items.Add("more foo");
                lstMinorSystem.Items.Add("Summary Reports");
            }
            protected  void lstMinorSystem_SelectedIndexChanged(object sender,EventArgs e)
            {               
    string strMinorSystem ="";
                    strMinorSystem = lstMinorSystem.SelectedItem.ToString();          
    if(strMinorSystem =="Summary Reports")
                    {
                        txtReportName.Text = "Summary_Reports_123012";
                    }
                 else
                    {
                        txtReportName.Text = "Unknown_Reports_123012";
                  }
               }
        }
    }


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    • Proposed as answer by Bob Wu-MT Friday, March 15, 2013 7:14 AM
    Thursday, March 14, 2013 9:57 PM
  • hi Diddi

    unfortunately, along with not working in C# (even though I was able to muddle through the code posted above) I have never worked in ASP.NET.
    you can keep this thread open and wait (hope?) for someone with ASP.NET experience to come across it. While your code above to pull data from the SQL db looks good, ASP might have some little things.

    or what you might do is close this thread by marking it as answered and post your question in the ASP.NET Forum. The post from me that ob marked as proposed answer could be marked as Answer.

    The good thing is you now know it is not the SelectedIndexChanged event that is the problem. Sometimes you have to slowly rule things out to figure out what the problem is. It appears that the issue is pulling the data or how you are binding the ListBox.

    two thoughts to try.

    1. comment out the line lstMinorSystem.DataBind. In a WinForm app, you would set the DataSource and then the DisplayMember and ValueMember properties (these would be equivalent to the DataTextField and DataValueField probably), but you wouldn't call the DataBind method. Maybe it's different in ASP, but worth a try. Another thing to try is to move the DataSource line above where you set the Field properties (right after the da.Fill(ds) line).

    2. After the da.Fill(ds) line, put in a line like this:

    Debug.WriteLine(ds.Tables[0].Rows.Count)

    or whatever it is in C# to see the number of Rows in the table. The reason being if the Row count is correct, then you know it is filling the dataset with data, and the problem is narrowed down to how you are databinding the ListBox.

    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    • Marked as answer by diddi10 Friday, March 15, 2013 3:51 PM
    Friday, March 15, 2013 3:19 PM