form1.cs代码:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
form2.cs代码: 怎么得到form1中textBox1的值?
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1(); MessageBox.Show(frm1.textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
form2.cs代码: 怎么得到form1中textBox1的值?
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1(); MessageBox.Show(frm1.textBox1.Text);
}
解决方案 »
- (难)怎样用 Winform 或者 WPF 实现 QQ好友可以创建桌面快捷方式
- 如何用C# 获取网页源码中<div id="result">之间的内容</div>?
- 关于正则表达式
- System.InvalidCastException异常 FormatException
- 控制datagridview行颜色的问题
- xml解析
- textbox输入数值后相加然后显示出来的问题
- 输入数字 打印对应数字英文
- 小弟请教DSACryptoServiceProvider的用法,我怎么才能验证正确??
- 哇哈哈哈哈哈!双喜临门!!散分散分!!!瓦哈哈哈哈
- richTextBox1设置的字体为宋体,但是为什么输入英文字母时,显示的不是宋体呢?请问怎么解决呢?
- 为什么调试运行时正常,发布运行就主线程卡死?
public string test//
{
get { return this.textBox1.Text; }
}
public string TextBoxValue
{
get
{
return textBox1.Text;
}
}在Form2里面去这么操作
MessageBox.Show(Form1.TextBoxValue);
textBox1.modifiers =public
MessageBox.Show(frm1.textBox1.Text); 但是显示为空 为啥?
1、主窗体往子窗体传值
有两种方法,一种是在子窗体提供重载构造函数,利用重载构造函数传递值,适用于传值数量比较少;第二种是,在子窗体中定义一个主窗体对象,然后就可以接收到主窗体的属性值了,适用于传值数量大。
主窗体代码如下:
public partial class frmParent : Form
{
private string strValueA = "";
public string StrValueA
{
get
{
return this.strValueA;
}
set { this.strValueA = value; }
}
public frmParent()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.strValueA = textBox1.Text;
frmChild frmchild = new frmChild();
frmchild.Owner = this;
frmchild.ShowDialog();
frmchild.Dispose();
} private void button2_Click(object sender, EventArgs e)
{
frmChild frmchild = new frmChild(this.textBox1.Text);
string returnValue = "";
if (frmchild.ShowDialog() == DialogResult.OK)
{
returnValue = frmchild.Str;
this.textBox1.Text = returnValue;
}
}
}
子窗体代码如下:
public partial class frmChild : Form
{
private string str;
public string Str
{
get { return this.str; }
set { this.str = value; }
}
private frmParent frmparent; public frmChild()
{
InitializeComponent();
}
public frmChild(string str)
{
this.str = str;
InitializeComponent();
this.textBox1.Text = str;
}
private void frmChild_Load(object sender, EventArgs e)
{
frmparent = (frmParent)this.Owner;
//this.textBox1.Text = frmparent.StrValueA;
} private void button1_Click(object sender, EventArgs e)
{
//frmparent = (frmParent)this.Owner;
this.Str = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
2、从子窗体返回值到主窗体中
利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
主窗体代码如下:
public partial class frmParent : Form
{
private string strValueA = "";
public string StrValueA
{
get
{
return this.strValueA;
}
set { this.strValueA = value; }
}
public frmParent()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frmChild frmchild = new frmChild(this.textBox1.Text);
string returnValue = "";
if (frmchild.ShowDialog() == DialogResult.OK)
{
returnValue = frmchild.Str;
this.textBox1.Text = returnValue;
}
}
}
子窗体代码如下:
public partial class frmChild : Form
{
private string str;
public string Str
{
get { return this.str; }
set { this.str = value; }
}
private frmParent frmparent; public frmChild()
{
InitializeComponent();
}
private void frmChild_Load(object sender, EventArgs e)
{
frmparent = (frmParent)this.Owner;
//this.textBox1.Text = frmparent.StrValueA;
} private void button1_Click(object sender, EventArgs e)
{
//frmparent = (frmParent)this.Owner;
this.Str = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
有时需要动态通知另一个窗体则发布事件,外面爱用就用,不用拉到~~~
如 frm1.textBox1.Text frm1.textBox1.name frm1.textBox1.width,,,等等
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//在调用Form2时,要把Form2的所有者设为Form1
Form2 f2 = new Form2() ;
f2.Owner = this;
f2.ShowDialog() ;
//Form2:
//在需要对其调用者(父)刷新时
Form1 f1 ;
f1 = (Form1)this.Owner;
f1.Refresh_Method() ;eg:Form1中的函数: public void DiaoYong(string str)
{
this.textBox1.Text =str;
}private void button2_Click(object sender, EventArgs e)
{ string str = this.textBox1.Text;
Form2 f2 = new Form2(str);//在构造函数中,向子窗体传值。
f2.Owner = this;
f2.ShowDialog();
}Form2中的函数: public Form2(string ss)
{
InitializeComponent();
this.textBox1.Text = ss; } private void button1_Click(object sender, EventArgs e)
{
string st = textBox1.Text; Form1 f1;
f1 = (Form1)this.Owner;
f1.DiaoYong(st); this.Close();
}
http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx#sec5
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
public void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(2, 3);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(183, 123);
this.textBox1.TabIndex = 0;
this.textBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDoubleClick);
//
// toolTip1
//
this.toolTip1.ToolTipTitle = "我的tooltiptitle标题!";
//
// button1
//
this.button1.Location = new System.Drawing.Point(110, 132);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "获得驱动器";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(2, 132);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(102, 20);
this.comboBox1.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(110, 190);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "打开Form2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 270);
this.Controls.Add(this.button2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "main";
this.Text = "main";
this.Load += new System.EventHandler(this.main_Load);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion public System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.ToolTip toolTip1;
public System.Windows.Forms.Button button1;
public System.Windows.Forms.ComboBox comboBox1;
public System.Windows.Forms.Button button2;
注意其中的所有的public,这样的话,我们在form2界面中就可以这样使用了:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
main m = new main();
this.label1.Text = m.button2.Text ;
} private void Form2_Load(object sender, EventArgs e)
{
}
}
}
注意看其中的button1_Click的代码.测试过了,完全可以的。
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
窗体的参数传递
http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx#sec5
form1.cs代码:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
}
form2.cs代码:
添加一个变量string str;
public Form2(string text)
{
str=text;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(str);
}