将 DomainUpDown.Value 从以编程方式生成的表单传递到主表单
Passing DomainUpDown.Value from programmatically generated form to main form
我有一个带有 showForm
按钮的 form1,它以编程方式创建并打开一个包含 4 个 DomainUpDown
元素和一个 OKBtn
按钮的新 form2
。我需要使用我的 OKBtn
将 DomainUpDown
元素的值从 form2
传递到 form1
richtextbox.My 唯一困难的是末尾的问号。这是代码片段:
public void showForm_Click(object sender,EventArgs e)
{
Form frm = new Form();
frm.Size = new Size(264, 183);
frm.Name = "MarginSelector";
frm.Text = "Qiymət ver";
frm.ShowIcon = false;
frm.Show();
DomainUpDown marginRightVal = new DomainUpDown();
marginRightVal.Location = new Point(150, 100);
marginRightVal.Size = new Size(42, 40);
frm.Controls.Add(marginRightVal);
for (int i = 0; i < 100; i++)
{
marginRightVal.Items.Add(i + "%");
}
Button OKBtn = new Button();
OKBtn.Visible = true;
OKBtn.Text = "OK";
OKBtn.Size = new Size(30, 23);
OKBtn.Location = new Point(96, 109);
frm.Controls.Add(OKBtn);
OKBtn.Click += new System.EventHandler(this.OKBtn_Click);
}
public void OKBtn_Click(object sender, EventArgs e)
{
textArea.SelectionLength = 0;
textArea.SelectedText = string.Filter("margin-top: {0} ; \n, ? ");
}
您可以听从 Hans Passant 的建议,或者您可以将单击事件的 sender
转换为控件,获取对表单的引用并遍历 Controls
集合以找到控件您正在寻找。找到后,将其分配给一个变量并在您的逻辑中使用它。实现可能如下所示:
public void OKBtn_Click(object sender, EventArgs e)
{
// assume a Control is the sender
var ctrl = (Control)sender;
// on which form is the control?
var frm = ctrl.FindForm();
// iterate over all controls
DomainUpDown domainUpDown = null;
foreach(var ctr in frm.Controls)
{
// check if this is the correct control
if (ctr is DomainUpDown)
{
// store it's reference
domainUpDown = (DomainUpDown)ctr;
break;
}
}
// if we have found the control
if (domainUpDown != null)
{
textArea.SelectionLength = 0;
Debug.WriteLine(domainUpDown.SelectedIndex);
Debug.WriteLine(domainUpDown.SelectedItem);
// use the SelectedItem
textArea.SelectedText = string.Format("margin-top: {0} ; \n,", domainUpDown.SelectedItem );
}
}
如果您的表单上有多个控件,您最好为每个控件添加一个唯一的名称:
DomainUpDown marginRightVal = new DomainUpDown();
marginRightVal.Location = new Point(150, 100);
marginRightVal.Size = new Size(42, 40);
marginRightVal.Name = "right";
frm.Controls.Add(marginRightVal);
并且当您遍历控件集合时,您可以检查该名称:
foreach(var ctr in frm.Controls)
{
// check if this is the correct control
if (ctr is DomainUpDown)
{
// store it's reference
domainUpDown = (DomainUpDown)ctr;
if (domainUpDown.Name == "right")
{
// do logic for that value
}
}
}
或者您可以使用 Find method:
var found = frm.Controls.Find("right", false);
if (found.Length>0)
{
var rightDomain = (DomainUpDown)found[0];
// do logic here
}
我有一个带有 showForm
按钮的 form1,它以编程方式创建并打开一个包含 4 个 DomainUpDown
元素和一个 OKBtn
按钮的新 form2
。我需要使用我的 OKBtn
将 DomainUpDown
元素的值从 form2
传递到 form1
richtextbox.My 唯一困难的是末尾的问号。这是代码片段:
public void showForm_Click(object sender,EventArgs e)
{
Form frm = new Form();
frm.Size = new Size(264, 183);
frm.Name = "MarginSelector";
frm.Text = "Qiymət ver";
frm.ShowIcon = false;
frm.Show();
DomainUpDown marginRightVal = new DomainUpDown();
marginRightVal.Location = new Point(150, 100);
marginRightVal.Size = new Size(42, 40);
frm.Controls.Add(marginRightVal);
for (int i = 0; i < 100; i++)
{
marginRightVal.Items.Add(i + "%");
}
Button OKBtn = new Button();
OKBtn.Visible = true;
OKBtn.Text = "OK";
OKBtn.Size = new Size(30, 23);
OKBtn.Location = new Point(96, 109);
frm.Controls.Add(OKBtn);
OKBtn.Click += new System.EventHandler(this.OKBtn_Click);
}
public void OKBtn_Click(object sender, EventArgs e)
{
textArea.SelectionLength = 0;
textArea.SelectedText = string.Filter("margin-top: {0} ; \n, ? ");
}
您可以听从 Hans Passant 的建议,或者您可以将单击事件的 sender
转换为控件,获取对表单的引用并遍历 Controls
集合以找到控件您正在寻找。找到后,将其分配给一个变量并在您的逻辑中使用它。实现可能如下所示:
public void OKBtn_Click(object sender, EventArgs e)
{
// assume a Control is the sender
var ctrl = (Control)sender;
// on which form is the control?
var frm = ctrl.FindForm();
// iterate over all controls
DomainUpDown domainUpDown = null;
foreach(var ctr in frm.Controls)
{
// check if this is the correct control
if (ctr is DomainUpDown)
{
// store it's reference
domainUpDown = (DomainUpDown)ctr;
break;
}
}
// if we have found the control
if (domainUpDown != null)
{
textArea.SelectionLength = 0;
Debug.WriteLine(domainUpDown.SelectedIndex);
Debug.WriteLine(domainUpDown.SelectedItem);
// use the SelectedItem
textArea.SelectedText = string.Format("margin-top: {0} ; \n,", domainUpDown.SelectedItem );
}
}
如果您的表单上有多个控件,您最好为每个控件添加一个唯一的名称:
DomainUpDown marginRightVal = new DomainUpDown();
marginRightVal.Location = new Point(150, 100);
marginRightVal.Size = new Size(42, 40);
marginRightVal.Name = "right";
frm.Controls.Add(marginRightVal);
并且当您遍历控件集合时,您可以检查该名称:
foreach(var ctr in frm.Controls)
{
// check if this is the correct control
if (ctr is DomainUpDown)
{
// store it's reference
domainUpDown = (DomainUpDown)ctr;
if (domainUpDown.Name == "right")
{
// do logic for that value
}
}
}
或者您可以使用 Find method:
var found = frm.Controls.Find("right", false);
if (found.Length>0)
{
var rightDomain = (DomainUpDown)found[0];
// do logic here
}