我在这里尝试做的是从另一个表单上的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,但我不知道如何将其取回,任何帮助都将不胜感激。
发布于 2019-12-14 05:12:20
这里有几点要说明。第一个问题是,您是否希望输入表单是模态的(等到它关闭后再继续)?如果是,则需要初始化表单并在需要输入时调用.ShowDialog()。否则,您需要在调用fill list方法之前使用.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属性)中恰好存在的任何值。
发布于 2019-12-14 04:16:20
我更喜欢通过在第二个表单中添加一个公共属性来完成此操作。此属性获取可以返回文本框中的字符串,也可以返回一个变量(如果已设置)。
这是一种更面向对象的方法,因为第二个表单控制返回的内容。
https://stackoverflow.com/questions/59328050
复制相似问题