Home > other >  Multiple dynamically created comboboxes not triggering event handlers C#
Multiple dynamically created comboboxes not triggering event handlers C#

Time:02-12

The following code is for a desktop application, not a web app. I have essentially created a dynamic row of controls to collect phone information for a contact. The user can push a button at the end of each row if they want to add an additional row for contact info.

Problem 1: Each row has two comboboxes (cmbo_PhoneType & cmbo_BestCalltime) with eventhandlers attached. The event handlers, cmbo_PhoneType.SelectedIndexChanged && cmbo_BestCallTime.SelectedIndexChanged are not firing for the individual comboBoxes, for each row the user creates.

Problem 2: The selected list item visible to the user never changes. For example they select "Home"...the combobox will find it and append, but once the user leaves the combobox the selection (for all comboboxes) are defaulting back to the first item in the list.

If this were a web app, I would think it would be related to postback and viewstate (which I have not yet created the view state, but will once I get past these issues), but since this is a desktop app, I'm not sure. What have I missed?

    public partial class frmTestPhoneRows : Form
    {
        
        int leftControl = 1;
        int StartPos = 88;
       

        ComboBox cmbo_phoneType = new ComboBox();
        MaskedTextBox msk_phone = new MaskedTextBox();
        TextBox txt_ext = new TextBox();
        ComboBox cmbo_BestCallTime = new ComboBox();
        CheckBox isMobile = new CheckBox();
        CheckBox verified = new CheckBox();
        CheckBox SubscribeToTexts = new CheckBox();
        Button btn_addNew = new Button();


        public frmTestPhoneRows()
        {
            InitializeComponent();
            createNewRow();

        }

        private void frmTestPhoneRows_Load(object sender, EventArgs e)
        {
           

        }

        void createNewRow()
        {
            //connection string redacted
            MySqlConnection reConn = new MySqlConnection(sConnection);
            reConn.Open();

            MySqlCommand phoneTypeCommand = new MySqlCommand("Select * from lutable_phonetype order by Type", reConn);
            MySqlDataAdapter phoneTypeAdapter = new MySqlDataAdapter();
            DataTable phoneTypeTable = new DataTable();
            phoneTypeAdapter.SelectCommand = phoneTypeCommand;
            phoneTypeAdapter.Fill(phoneTypeTable);

            MySqlCommand bestCallTimeCommand = new MySqlCommand("Select * from lutble_bestcalltime ORDER by CallTime", reConn);
            MySqlDataAdapter bestCallTimeAdapter = new MySqlDataAdapter();
            DataTable bestCallTimeTable = new DataTable();
            bestCallTimeAdapter.SelectCommand = bestCallTimeCommand;
            bestCallTimeAdapter.Fill(bestCallTimeTable);


            ComboBox cmbo_newPhoneType = new ComboBox();
            this.Controls.Add(cmbo_newPhoneType);
            cmbo_newPhoneType.Size = new Size(121, 20);
            cmbo_newPhoneType.DropDownStyle = ComboBoxStyle.DropDown;
            cmbo_newPhoneType.AutoCompleteMode = AutoCompleteMode.Append;
            cmbo_newPhoneType.AutoCompleteSource = AutoCompleteSource.ListItems;
            cmbo_newPhoneType.Top = leftControl * StartPos;
            cmbo_newPhoneType.Left = 18;
            cmbo_newPhoneType.Font = new Font(this.Font.FontFamily, 9);
            cmbo_newPhoneType.BackColor = Color.LightBlue;
            cmbo_newPhoneType.ForeColor = Color.DarkBlue;
            cmbo_newPhoneType.DataSource = phoneTypeTable;
            cmbo_newPhoneType.DisplayMember = "Type";
            cmbo_newPhoneType.ValueMember = "TypeID";
            cmbo_newPhoneType.DataBindings.Add("Selectedvalue", phoneTypeTable, "TypeID");
            cmbo_newPhoneType.SelectedIndexChanged  = new System.EventHandler(phoneType_SelectedIndexChanged);

            MaskedTextBox msk_NewPhone = new MaskedTextBox();
            this.Controls.Add(msk_NewPhone);
            msk_NewPhone.Size = new Size(100, 20);
            msk_NewPhone.Top = leftControl * StartPos;
            msk_NewPhone.Left = 143;
            msk_NewPhone.Font = new Font(this.Font.FontFamily, 9);
            msk_NewPhone.BackColor = Color.LightBlue;
            msk_NewPhone.ForeColor = Color.DarkBlue;
            msk_NewPhone.Mask = "(000) 000-0000";

            TextBox txt_newExt = new TextBox();
            this.Controls.Add(txt_newExt);
            txt_newExt.Size = new Size(61, 20);
            txt_newExt.Top = leftControl * StartPos;
            txt_newExt.Left = 247;
            txt_newExt.Font = new Font(this.Font.FontFamily, 9);
            txt_newExt.BackColor = Color.LightBlue;
            txt_newExt.ForeColor = Color.DarkBlue;

            ComboBox cmbo_newBestCallTime = new ComboBox();
            this.Controls.Add(cmbo_newBestCallTime);
            cmbo_newBestCallTime.Size = new Size(231, 20);
            cmbo_newBestCallTime.DropDownStyle = ComboBoxStyle.DropDown;
            cmbo_newBestCallTime.AutoCompleteMode = AutoCompleteMode.Append;
            cmbo_newBestCallTime.AutoCompleteSource = AutoCompleteSource.ListItems;
            cmbo_newBestCallTime.Top = leftControl * StartPos;
            cmbo_newBestCallTime.Left = 312;
            cmbo_newBestCallTime.Font = new Font(this.Font.FontFamily, 9);
            cmbo_newBestCallTime.BackColor = Color.LightBlue;
            cmbo_newBestCallTime.ForeColor = Color.DarkBlue;
            cmbo_newBestCallTime.DataSource = bestCallTimeTable;
            cmbo_newBestCallTime.DisplayMember = "CallTime";
            cmbo_newBestCallTime.ValueMember = "CallTimeId";
            cmbo_newBestCallTime.DataBindings.Add("Selectedvalue", bestCallTimeTable, "CallTimeId");
            cmbo_newBestCallTime.SelectedIndexChanged  = new System.EventHandler(cmboBestCallTime_SelectedIndexChanged);

            CheckBox chkbx_newVerified = new CheckBox();
            this.Controls.Add(chkbx_newVerified);
            chkbx_newVerified.CheckState = CheckState.Unchecked;
            chkbx_newVerified.Text = "Verified";
            chkbx_newVerified.Size = new Size(75, 20);
            chkbx_newVerified.Top = leftControl * StartPos;
            chkbx_newVerified.Left = 547;
            chkbx_newVerified.Font = new Font(this.Font.FontFamily, 9);
            chkbx_newVerified.BackColor = Color.LightBlue;
            chkbx_newVerified.ForeColor = Color.DarkBlue;

            CheckBox chkbx_newIsMobile = new CheckBox();
            this.Controls.Add(chkbx_newIsMobile);
            chkbx_newIsMobile.CheckState = CheckState.Unchecked;
            chkbx_newIsMobile.Text = "Mobile";
            chkbx_newIsMobile.Size = new Size(70, 20);
            chkbx_newIsMobile.Top = leftControl * StartPos;
            chkbx_newIsMobile.Left = 626;
            chkbx_newIsMobile.Font = new Font(this.Font.FontFamily, 9);
            chkbx_newIsMobile.BackColor = Color.LightBlue;
            chkbx_newIsMobile.ForeColor = Color.DarkBlue;

            CheckBox chkbx_newSubscribeToTexts = new CheckBox();
            this.Controls.Add(chkbx_newSubscribeToTexts);
            chkbx_newSubscribeToTexts.CheckState = CheckState.Unchecked;
            chkbx_newSubscribeToTexts.Text = "Subscribe To Texts";
            chkbx_newSubscribeToTexts.Size = new Size(156, 20);
            chkbx_newSubscribeToTexts.Top = leftControl * StartPos;
            chkbx_newSubscribeToTexts.Left = 700;
            chkbx_newSubscribeToTexts.Font = new Font(Font.FontFamily, 9);
            chkbx_newSubscribeToTexts.BackColor = Color.LightBlue;
            chkbx_newSubscribeToTexts.ForeColor = Color.DarkBlue;

            Button btn_newAddNew = new Button();
            this.Controls.Add(btn_newAddNew);
            btn_newAddNew.Size = new Size(75, 20);
            btn_newAddNew.BackColor = Color.DarkBlue;
            btn_newAddNew.ForeColor = Color.LightBlue;
            btn_newAddNew.Text = "Add Another";
            btn_newAddNew.Top = leftControl * StartPos;
            btn_newAddNew.Left = 860;
            btn_newAddNew.Click  = new System.EventHandler(btn_addNew_Click);



        }

        private void phoneType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbo_phoneType.DataBindings.Clear();
        }

        private void cmboBestCallTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbo_BestCallTime.DataBindings.Clear();
        }

        private void btn_addNew_Click(object sender, EventArgs e)
        {
            StartPos = StartPos   28;
            createNewRow();

          

        }

CodePudding user response:

When you wire up the dynamic controls, the SOURCE of the event will be passed to the event handler via the SENDER parameter.

Cast "sender" to your appropriate control type and then act upon it:

private void phoneType_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    cb.DataBindings.Clear();
}

private void cmboBestCallTime_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    cb.DataBindings.Clear();
}
  • Related