多个动态创建的组合框不触发事件处理程序 C#

Multiple dynamically created comboboxes not triggering event handlers C#

以下代码适用于桌面应用程序,而非网络应用程序。我基本上创建了一行动态控件来收集联系人的 phone 信息。如果用户想要为联系信息添加额外的一行,可以在每行的末尾按下一个按钮。

问题 1:每一行都有两个组合框(cmbo_PhoneType 和 cmbo_BestCalltime),并附有事件处理程序。对于用户创建的每一行,事件处理程序 cmbo_PhoneType.SelectedIndexChanged && cmbo_BestCallTime.SelectedIndexChanged 不会触发单个组合框。

问题 2:用户可见的 selected 列表项永远不会改变。例如,他们 select“Home”...组合框会找到它并追加,但是一旦用户离开组合框,selection(对于所有组合框)默认返回到列表。

如果这是一个网络应用程序,我认为它会与回发和视图状态相关(我还没有创建视图状态,但一旦我解决了这些问题就会),但因为这是一个桌面应用程序,我不确定。我错过了什么?

    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();

          

        }

连接动态控件时,事件的 SOURCE 将通过 SENDER 参数传递给事件处理程序。

将“发件人”转换为适当的控件类型,然后对其进行操作:

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();
}