在 Button.Click C# Windows 表单上使用 Panel.Click(属于动态面板)事件
Use Panel.Click (of Dynamic Panels) event on Button.Click C# Windows Forms
我的表单上有动态生成的面板,每个面板都有多个控件,包括文本框、组合框和按钮。我想在 "Save" 按钮上捕获它们的值,该按钮不是动态生成的(在表单中)。我正在使用此代码获取值:
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);
}
}
}
}
}
}
}
我的表单上有动态生成的面板,每个面板都有多个控件,包括文本框、组合框和按钮。我想在 "Save" 按钮上捕获它们的值,该按钮不是动态生成的(在表单中)。我正在使用此代码获取值:
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);
}
}
}
}
}
}
}