【问题标题】:Use Panel.Click (of Dynamic Panels) event on Button.Click C# Windows Forms在 Button.Click C# Windows 窗体上使用 Panel.Click(动态面板)事件
【发布时间】:2016-09-22 13:34:53
【问题描述】:

我的表单上有动态生成的面板,每个面板都有多个控件,包括文本框、组合框和按钮。我想在不是动态生成的“保存”按钮上捕获它们的值(它的形式)。我正在使用此代码获取值:

private void GetPanelControls(object sender, EventArgs e)
        {
            Panel allpanels = sender as Panel;
            panelname = ItemsIDSelected[panelnamecounter] + "p";
            //"p" identifies Panel and there is a counter with a list
            if (allpanels.Name == panelname)
            {
                foreach (Control item in allpanels.Controls)
                {
                    if (item.Name == (ItemsIDSelected[panelcontrolcounter] + "t"))  //"t" identifies TextBox
                    {
                        ItemsNameListforInsert.Add(item.Text);
                        panelcontrolcounter++;     //Panel has multiple controls
                    }
                    panelnamecounter++;             //There are multiple Panels
                }

            }
        }

如何在我的 Button_Click 事件中调用此事件?

Panel panelGroup = new System.Windows.Forms.Panel();
panelGroup.Click += new EventHandler(GetPanelControls);

这就是我如何生成面板及其事件。

【问题讨论】:

    标签: c# forms winforms dynamic panels


    【解决方案1】:

    你可以试试这样的

    private void Button_Click(object sender, EventArgs e)
    {
        GetPanelControls(this, new EventArgs());
    }
    

    编辑

    如果我们在不使用面板点击事件的情况下使用一个方法,如果你需要你可以在面板点击事件中调用这个方法

        private void GetPanelControls()
        {
            foreach (Control formControl in this.Controls)
            {
                if (formControl is Panel)
                {
                    string panelName = ItemsIDSelected[panelnamecounter] + "p";
    
                    if (formControl.Name == panelName)
                    {
                        foreach (Control item in formControl.Controls)
                        {
                            // Your Code
                        }
                    }
                }
            }
        }
    

    【讨论】:

    • 感谢您的回答,兄弟。但它不能以这种方式识别面板。它说:对象引用未设置为对象的实例。
    【解决方案2】:
            //Control create button   
              private void button1_Click(object sender, EventArgs e)
                        {
    
                            Panel pnl = new Panel();
                            pnl.Name = "pnltest";
                            pnl.Location = new Point(500, 200);
                            TextBox txt1 = new TextBox();
                            txt1.Name = "txttest";
                            txt1.Location = new Point(0 ,10);
                            pnl.Controls.Add(txt1);
                            ComboBox cmb = new ComboBox();
                            cmb.Location = new Point(0, 50);
                            cmb.Name = "cmbtest";
                            cmb.Items.Add("one");
                            cmb.Items.Add("two");
                            cmb.Items.Add("three");
                            pnl.Controls.Add(cmb);
                            Button btn = new Button();
                            btn.Name = "btntest";
                            btn.Text = "submit";
                            btn.Location = new Point(0, 75);
                            btn.Click += btn_Click;
                            pnl.Controls.Add(btn);
                            this.Controls.Add(pnl);
    
    
                        }
    //control button click event
         void btn_Click(object sender, EventArgs e)
                {
                    foreach (Control frmcntrl in this.Controls)
                    {
                        if (frmcntrl is Panel)
                        {
                            if (frmcntrl.Name == "pnltest")
                            {
                                foreach (Control item in frmcntrl.Controls)
                                {
                                    if (item is TextBox)
                                    {
                                        if (item.Name == "txttest")
                                        {
                                            MessageBox.Show(item.Text .ToString());
                                        }
                                    }
                                    else if (item is ComboBox)
                                    {
                                        if (item.Name == "cmbtest")
                                        {
                                            MessageBox.Show(item.Text);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    

    【讨论】:

    • 谢谢先生。我想到了这个问题是别的东西..现在解决了..谢谢
    猜你喜欢
    • 2020-08-04
    • 2012-01-27
    • 2013-10-31
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2017-06-15
    • 1970-01-01
    相关资源
    最近更新 更多