我在这里要做的是从另一张表单上的Textbox
取值回到主表单。
在FormMain
中,我具有以下功能:
private void FillList(string type, HtmlNode form)
{
try {
var nodes = form.SelectNodes("//form" + type);
if (nodes != null)
{
foreach (HtmlNode elem in nodes)
{
var eleTY = elem.Attributes["type"] == null ? elem.Name.ToString() : elem.Attributes["type"].Value;
var eleNM = elem.Attributes["id"] == null ?
elem.Attributes["name"] == null ? "" : "name"
: "id";
var eleVU = elem.Attributes["id"] == null ?
elem.Attributes["name"] == null ? "" : elem.Attributes["name"].Value
: elem.Attributes["id"].Value;
var elePR = Helpers.PredictValue(eleVU);
var eleSL = "";
// check for select ...
if (eleTY == "select") {
FormInput fi = new FormInput(this, eleTY, eleVU);
fi.Show();
}
// first checked id then name ...
listViewMain.Items.Add(new ListViewItem(new string[] {
elem.Attributes["type"]==null? elem.Name.ToString():elem.Attributes["type"].Value
,
elem.Attributes["id"]==null?
elem.Attributes["name"]==null? "":"name"
:"id"
,
elem.Attributes["id"]==null?
elem.Attributes["name"]==null?"": elem.Attributes["name"].Value
: elem.Attributes["id"].Value
,
eleNM + "|" + eleVU + "|" + eleSL + "|" + elePR
}));
// check the mode and append to it ...
if (comboBoxMode.Text == "mode_register") {
txtBoxUploadRegisterMacro.AppendText(eleNM + "|" + eleVU + "|" + eleSL + "|" + elePR + Environment.NewLine);
}
// check the mode and append to it ...
if (comboBoxMode.Text == "mode_login_and_post")
{
txtBoxUploadLoginAndPostMacro.AppendText(eleNM + "|" + eleVU + "|" + eleSL + "|" + elePR + Environment.NewLine);
}
}
}
} catch (Exception) {
// handle ...
}
}
Once i have a ```select``` attribute another form will popup ```FormInput``` here i will input a value and hit a button, once the button is pressed i am trying to get the value of ```txtBoxInput.Text``` back to the ```FormMain``` i will hopefully store the returned value in the ```eleSL``` variable.
My ```FormInput``` working:
public partial class FormInput : Form
{
FormMain _formMain;
public FormInput(FormMain formMain, string eleType, string eleName)
{
_formMain = formMain;
InitializeComponent();
lblTypeInput.Text = eleType;
lblNameInput.Text = eleName;
}
private void BtnInput_Click(object sender, EventArgs e)
{
// pass the value of txtBoxInput.Text back to FormMain here ...
this.Close();
}
}
我可以将FormMain
的实例传递给FormInput
,但是我对如何找回它一无所知,我们将不胜感激。
答案 0 :(得分:2)
这里有几点要说明。一种是您是否希望输入表单具有模式(要等到它关闭后才能继续)?如果是,那么您需要初始化表单并在需要输入时调用.ShowDialog()
。否则,在调用填充列表方法之前,您需要使用.Show()
显示表单,并使用属性将引用中的值提取到输入表单中。
public partial class InputForm : Form
{
public InputForm()
{
InitializeComponent();
}
public string Value
{
get => textBox1.Text;
set => textBox1.Text = value;
}
}
无论哪种方式,输入表单都需要显示数据的属性。可以是单个值,多个值或自定义类。
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void FillList()
{
var fi = new InputForm();
fi.Value = textBox1.Text; // set initial value from main form
if (fi.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fi.Value; // get input value back to main form
}
}
}
为此,您需要相应地设置输入表单中每个按钮的.DialogResult
属性,并设置输入表单的.AcceptButton
和.CancelButton
属性。完成后,这将用于关闭表单,并将DialogResult
返回设置为.ShowDialog()
以便知道用户是按[OK]还是[Cancel]。
public partial class MainForm : Form
{
InputForm fi = new InputForm() { Value = "Default" };
public MainForm()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
fi.Show(this);
}
private void FillList()
{
textBox1.Text = fi.Value; // grab whatever value the input form has
}
}
在这种方法中,输入表单不会阻止主表单的流程,但是您不知道用户何时更改了值。该方法运行时,它只会提取输入表单文本框中的任何值(以及.Value
属性)。
答案 1 :(得分:0)
我希望通过在第二个表单中添加一个公共属性来做到这一点。此属性获取可以从文本框中返回字符串,也可以返回变量(如果已设置)。
这是一种面向对象的方法,因为第二种形式控制返回的内容。