comboBox_SelectedIndexChanged event did not activate when click on combo box

Lin100

Well-known member
Joined
Dec 12, 2022
Messages
69
Programming Experience
10+
comboBox_Name_2_SelectedIndexChanged event did not activate when I click on the combo box.

I clicked the left margin of the line of code below and I see a big red dot on that line. Next, I clicked F5 to enter into
debug mode. I then I selected a value in a combo box, but I did not see the procedure below run line by line in debug mode.


C#:
        private void comboBox_Name_2_SelectedIndexChanged(Object Sender, EventArgs e)
        {
          display_datagrid();
        }
 
Last edited by a moderator:
Did you actually select an item, or did you just press the down arrow icon on the side of the combobox? The code will only fire when an item is selected.

Did you hook up the event to the combo box?
 
Did you actually select an item, or did you just press the down arrow icon on the side of the combobox? The code will only fire when an item is selected.

Did you hook up the event to the combo box?
Did you actually select an item, or did you just press the down arrow icon on the side of the combobox?
Answer: I actually selected an item, and when I did the above code did not get activated.

Did you hook up the event to the combo box?
Answer: How do you hookup the event to the combo box ?
I thought that when I created a combo box, Visual Studio
did all of that hookup, and all I have to do is use the
procedure comboBox_Name_2_SelectedIndexChanged.


The code below does work. When I run it, the dataGridView2 did load
records into it, but when I selected an item in the combo box, no
event was activated and so the dataGridView2 did not load
new records into it.
C#:
///////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
//using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Apartment_Management
{
    public partial class Unit_2 : Form
    {
        public Unit_2()
        {
           InitializeComponent();
        }

        private void comboBox_property_name()
        {
           OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AMS_2007.accdb");
           OleDbDataAdapter da = new OleDbDataAdapter("SELECT Property.Property_Name, Property.Property_ID, Property.Property_Phone, Property.Property_Manager FROM Property", con);
           con.Open();
           DataSet dt = new DataSet();
           da.Fill(dt, "Property_Name");

           comboBox_Name_2.DataSource = dt.Tables["Property_Name"];
           comboBox_Name_2.DisplayMember = "Property_Name";
           comboBox_Name_2.ValueMember = "Property_ID";
           con.Close();
        }

        private void display_datagrid()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Apartment_Management.Properties.Settings.AMS_2007ConnectionString"].ToString();
            con.Open();
            MessageBox.Show("Connection Sucess");
            OleDbCommand cmd = new OleDbCommand();

            if (comboBox_Name_2.Text == "Select All Properties")
               cmd.CommandText = "SELECT DISTINCTROW Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, " +
                                  "Unit.Rental_Cost, Unit.Security_Deposit, Unit.Bathroom, Unit.Square_Feet, " +
                                  "Unit.Status, Tenant.Date_Lease_Expired, Tenant.Late_On_Payment, " +
                                  "Tenant.Days_Delinquent FROM Unit LEFT JOIN " +
                                  "Tenant ON(Unit.Unit_Number = Tenant.Unit_Number) " +
                                  "AND(Unit.Property_Name = Tenant.Property_Name) " +
                                  "ORDER BY Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, Unit.Rental_Cost";
            else
            {
               MessageBox.Show("else condition");
               cmd.CommandText = "SELECT DISTINCTROW Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, " +
                                 "Unit.Rental_Cost, Unit.Security_Deposit, Unit.Bathroom, Unit.Square_Feet, " +
                                 "Unit.Status, Tenant.Date_Lease_Expired, Tenant.Late_On_Payment, " +
                                 "Tenant.Days_Delinquent FROM Unit LEFT JOIN " +
                                 "Tenant ON(Unit.Unit_Number = Tenant.Unit_Number) " +
                                 "AND(Unit.Property_Name = Tenant.Property_Name) " +
                                 "WHERE Unit.Property_Name = '" + comboBox_Name_2.Text + "'" +
                                 "ORDER BY Unit.Property_Name, Unit.Unit_Number, Unit.Bedroom, Unit.Rental_Cost";
            }
            cmd.Connection = con;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView2.DataSource = ds.Tables[0];
        }

        private void Unit_2_Load(Object Sender, EventArgs e)
        {
           comboBox_property_name();
           display_datagrid();
        }

        private void comboBox_Name_2_SelectedIndexChanged(Object Sender, EventArgs e)
        {
           display_datagrid();
        }

    }
}
 
Last edited by a moderator:
Please put your code in code tags when pasting them in. The code tags button on that toolbar looks like: </>. I'll update your post above.
 
Did you hook up the event to the combo box?
Answer: How do you hookup the event to the combo box ?
I thought that when I created a combo box, Visual Studio
did all of that hookup, and all I have to do is use the
procedure comboBox_Name_2_SelectedIndexChanged.
Normally that WinForms Designer does that, but there are some developers who come from a VB background where there just start typing in what looks like an event handler and then expect the VB behavior where it automatically hooks up that event. They often post a question similar to yours where they are wondering why their event handler is not being called.

Anyway, you can verify if the event hookup happened by clicking on the combobox in the designer, then look in the Properties pane to see what events have methods assigned to them. Double check that that event assigned is the one you have in your code.
 
Normally that WinForms Designer does that, but there are some developers who come from a VB background where there just start typing in what looks like an event handler and then expect the VB behavior where it automatically hooks up that event. They often post a question similar to yours where they are wondering why their event handler is not being called.

Anyway, you can verify if the event hookup happened by clicking on the combobox in the designer, then look in the Properties pane to see what events have methods assigned to them. Double check that that event assigned is the one you have in your code.

Answer: Please be specify in detail because I was in the property of the
combobox and I cannot find any section or subsection that
said "Events Method" or "Events"
 
Sorry I can't provide more details. I don't use the WinForms Designer day-to-day. (Got burned by it badly back in the VS2005-2008 days so I just started hand coding my forms since then.) I'm not anywhere close to a PC right now to fire it up and take screenshots to post and show you where to find the events. Look for a button that looks like a lightning bolt in the properties pane?
 
Sorry I can't provide more details. I don't use the WinForms Designer day-to-day. (Got burned by it badly back in the VS2005-2008 days so I just started hand coding my forms since then.) I'm not anywhere close to a PC right now to fire it up and take screenshots to post and show you where to find the events. Look for a button that looks like a lightning bolt in the properties pane?
Thank you SkyDiver for your help. I found it and got it to work.
Properties TAB --> Lightning Bolt --> SelectedIndexChanged -->
Select from drop down box: comboBox_Name_2_SelectedIndexChanged
 
In VB, there are two ways to register an event handler. You can add a Handles clause to the method itself if the field is declared WithEvents, or you can use an AddHandler statement. Most of the time, it is the former that is used. If, for instance, you double-click a ComboBox in the VB designer, an event handler will be generated and you can see the Handles clause. If you set the GenerateMember (I think its callled) property to False then there is no field declared WithEvents so there is nothing to put in the Handles clause. In that case, an AddHandler statement is generated to register the event handler.

In C#, there is no equivalent to WithEvents and Handles. There's only one way to register an event handler and that is the += operator, which is equivalent to AddHandler. If you look at the designer code file for your form, you'll see a line that uses that operator to register the event handler. If you just paste a method into your form then it's just a method, with no connection to the control. If you use the Properties window and click the Events button, then select an existing method as an event handler, the designer will add that line with the += operator.
 
1671039150068.png


If nothing at 3, no event is wired up


Please name your controls well. No pro dev you seek help from will want to read code that is stuffed full of "DataGridView2", "ComboBox6", "Button37". In six months time you won't want to either. To save having to remember whether it's "textBox72" or "textBox27" that is the first name box, and to save having to go into the designer to remind yourself, just call it "_firstNameTextBox" after you add it to the form
 
Back
Top Bottom