我的目标是能够在form1的文本框中输入一个值然后按回车键(当按下回车键时,该值将传递给一个名为setvalue的方法。当按下切换按钮时,表单将隐藏Form2有两个按钮,show和exit。当点击show时,我需要显示一个消息框,通过调用getvalue方法在form1的文本框中显示数据。
请接受我的想法。
这是一个
public partial class Form1 : Form
{
private int secretValue;
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Visible = true;
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
这个表格两个
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
答案 0 :(得分:0)
在表单2上添加构造函数值。它应如下所示:
public Form2(int secValue)
然后你可以通过传递值
在表单1上调用它Form2 frm2 = new Form2(secretValue);
也许您可以将它分配给表单2中的全局变量,以便您的代码可以在所有表单中引用它。您的表单2现在可以是:
public partial class Form2 : Form
{
int passedValue = 0;
public Form2(int secValue)
{
InitializeComponent();
passedValue = secValue;
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
}
}
在我看来,您不需要创建的属性。永远记住,如果从表单更改为表单,则分配给类的值将为null。
答案 1 :(得分:0)
以下代码无法正常工作,因为您正在创建form1的新实例并尝试从那里获取值,它只有默认的int值。
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
}
您可以做的就是在public property
内定义Form2
。并在显示Form2,
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
frm2.SecrectValueOfForm2 = ValueInForm1;
frm2.Visible = true;
this.Hide();
}
答案 2 :(得分:0)
Plz尝试代码如下所示:
public partial class Form1 : Form
{
private int secretValue;
Form2 frm2 = new Form2();
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frm2 .Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
frm2.Owner = this;
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
在Form2上:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = ((Form1 )this.Owner).GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
答案 3 :(得分:0)
窗体2
public Int32 _txtval { get; set; }
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(_txtval.ToString());
}
Form1中
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._txtval = Convert.ToInt32(textBox1.Text);
frm2.Visible = true;
this.Hide();
}