articles

Home / DeveloperSection / Articles / ListBox events in C #.Net

ListBox events in C #.Net

Anonymous User12717 27-Aug-2011

ListBox and ComboBox are very useful control in developing window application. These two controls are very closely related to each other but they are quite different in some manner.  ListBox is collection of data item which can be selected single or multiple at a time and the data item is called listbox item where as ComboBox data item can be selected one at a time. Here I am focusing on ListBox events which are widely used in an application. ListBox have many events but in selected index change event are widely used. So let’s see the brief demonstration on this event.

Step 1: First open visual studio and select new project from file menu and then select project (if you want to create window application) or website (if you want to create web application). Here I am choosing window application.

Step 2:  After creating successful new project drag listbox from left pane toolbox and drop it into window form.

ListBox events in C #.Net

Here I am taking two LisBox one for select category and second to display appropriate information corresponding to select category name and one datagridview to explore information .

ListBox events in C #.Net

For creating this write the following code into select index change method. Let’s brief demonstration of ListBox.

privatevoidlist_CollectionItem_SelectedIndexChanged(objectsender, EventArgse)
        {
            list_ShowItem.Items.Clear();
            //// create the object of listbox select items collection
            ListBox.SelectedObjectCollectionlst_data=list_CollectionItem.SelectedItems;
            foreach (objectdatainlst_data)
            {
                /// pass items name to databind method to load bind item into second list box
               DataBind(data.ToString());
            }
          
        }
        publicvoidDataBind(stringcheck)
        {
            /// bind data if class item is selected
            if(check=="Class")
             {
                 list_ShowItem.Items.Insert(0, "--------Class--------");
                list_ShowItem.Items.Insert(1, "7");
                list_ShowItem.Items.Insert(2, "8");
                list_ShowItem.Items.Insert(3, "9");
                list_ShowItem.Items.Insert(4, "10");
                list_ShowItem.Items.Insert(5, "11");
                list_ShowItem.Items.Insert(6, "12");
              }
                //// bind data if subject is slelected
            elseif(check=="Subject")
              {
                    list_ShowItem.Items.Insert(0, "--------Subject--------");
                    list_ShowItem.Items.Insert(1, "Math");
                    list_ShowItem.Items.Insert(2, "English");
                    list_ShowItem.Items.Insert(3, "Science");
                    list_ShowItem.Items.Insert(4, "English");
                    list_ShowItem.Items.Insert(5, "History");
                    list_ShowItem.Items.Insert(6, "Civics");
               }
 
                //// bind data if stream is selected
            else
              {
                  list_ShowItem.Items.Insert(0, "--------Stream--------");
                list_ShowItem.Items.Insert(1, "Science");
                list_ShowItem.Items.Insert(2, "Art");
 
               }
        }
 
       
        privatevoid list_ShowItem_SelectedIndexChanged(objectsender, EventArgse)
        {
            dataGridView1.Visible= true;
            stringcheckinput=list_ShowItem.Text;
            try
            {
 
                intconvertval= Convert.ToInt32(checkinput); /// this line genrate exception exception if selected items not class item
                //// explore information of apprpriate class selected
                SqlConnectioncon= new SqlConnection(ConnectionString.ConnString());
                con.Open();
                SqlDataAdapterda= new SqlDataAdapter();
                da.SelectCommand=newSqlCommand("select * from tblLOGIN where userid in (select stu_id from tblSTUDENT where STU_CLASS ='"+list_ShowItem.Text+"')", con);
                DataSetds= newDataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count>0)
                {
                    ///// bind data set with datagridview
                    dataGridView1.DataSource=ds.Tables[0];
                }
                else
                {
                    //// if record not available then this notification is generated
                    dataGridView1.Visible=false;
                    MessageBox.Show("No Records found");
                }
 
 
            }
            catch (Exceptionae)
            {
                //// data bind for stream category item
                 if(checkinput=="Art"||checkinput=="Science")
                {
                    SqlConnectioncon= new SqlConnection(ConnectionString.ConnString());
                    con.Open();
                    SqlDataAdapterda= new SqlDataAdapter();
                    da.SelectCommand=newSqlCommand("select * from tblLOGIN where userid in (select stu_id from tblSTUDENT where stream ='"+list_ShowItem.Text+"')", con);
                    DataSetds= newDataSet();
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count>0)
                    {
                        dataGridView1.DataSource=ds.Tables[0];
                    }
                    else
                    {
                        /////
                        dataGridView1.Visible=false;
                        MessageBox.Show("No Records found");
                    }
                 }
                 elseif (checkinput== "--------Class--------"|| checkinput== "--------Subject--------"||checkinput== "--------Stream--------")
                 {
                     dataGridView1.Visible=false;
                 }
                     //// bind data item with gridview for subject category
                else
                {
                    SqlConnectioncon= new SqlConnection(ConnectionString.ConnString());
                    con.Open();
                    SqlDataAdapterda= new SqlDataAdapter();
                    da.SelectCommand=newSqlCommand("select * from tblSUBJECTT where SUB_NAME  ='"+ list_ShowItem.Text+"'", con);
                    DataSetds= newDataSet();
                    da.Fill(ds);
                    if (ds.Tables[0].Rows.Count>0)
                    {
                        //// bind dataset with datagridview
                        dataGridView1.DataSource=ds.Tables[0];
                    }
                    else
                    {
                        /// show notification if record not found
                        dataGridView1.Visible=false;
                        MessageBox.Show("No Records found");
                    }
                }
              
                }
            }
        }

If select multi- data item then all information are displayed into second listbox.

ListBox events in C #.Net

And if want to select any information from second listbox then its information displayed into datagridview.

ListBox events in C #.Net



Updated 15-May-2020
I am a content writter !

Leave Comment

Comments

Liked By