我试过这个传递信息:
Form1 frm1 = new Form1();
textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text;
Run Code Online (Sandbox Code Playgroud)
这是获取信息的表单的形式加载.但没有文字.我该如何解决?Form2抓住Form1文字并显示它.
Her*_*man 12
使用属性公开文本框的内容:
class Form1 {
public string MyValue {
get { return textBox1.Text; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Form2中执行以下操作:
var frm1 = new Form1();
frm1.ShowDialog(this); // make sure this instance of Form1 is visible
textBox1.Text = frm1.MyValue;
Run Code Online (Sandbox Code Playgroud)
如果你想要frm1经常可见,那么创建frm1一个类变量Form2,并.Show()在Form2例如构造函数中调用.
小智 5
我发现在 Windows 应用程序中将文本值一个文本框传递给另一个文本框的简单而合乎逻辑的方法。
以第二种形式编写代码:-
Create a Parameter of *Form2* Constructor.
namespace PassingValue
{
public partial class Form2 : Form
{
public Form2(string message)
{
InitializeComponent();
Textbox1.Text= message;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以第一种形式编写代码:-
Use the Parameter of Second Form in *First Form*:-
namespace PassValue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2(Textbox.Text);
f2.Show();
}
}
}
Run Code Online (Sandbox Code Playgroud)