使用面板.单击按钮上的(动态面板)事件.单击“C# Windows 窗体”



>我的窗体上有动态生成的面板,每个面板都有多个控件,包括文本框,组合框和按钮。我想在不是动态生成的"保存"按钮上捕获它们的值(它在表单中)。我正在使用以下代码获取值:

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

这就是我生成面板及其事件的方式。

你可以尝试这样的东西

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

相关内容

  • 没有找到相关文章

最新更新