有两个窗体Form1 ,Form2
Form1中有一个TextBox txtText 和 一个Button btnShowForm2
Form2中有一个TextBox txtValue 和 一个Button btnOK
当启动Form1的时候,点击按钮btnShowForm2时,调出Form2对话框,在txtValue 中输入一个字符串,
后点击按钮btnOK时,就把txtValue 中的值传到Form1中的txtText 中,条件是:Form2是不能关闭的,希望高手指点,谢谢!
Form1中有一个TextBox txtText 和 一个Button btnShowForm2
Form2中有一个TextBox txtValue 和 一个Button btnOK
当启动Form1的时候,点击按钮btnShowForm2时,调出Form2对话框,在txtValue 中输入一个字符串,
后点击按钮btnOK时,就把txtValue 中的值传到Form1中的txtText 中,条件是:Form2是不能关闭的,希望高手指点,谢谢!
……
Form2 form2=new Form2();
……
form2.txtText.Text=form1.txtValue.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
public delegate void GetString(string str);
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnOpen = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(144, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// btnOpen
//
this.btnOpen.Location = new System.Drawing.Point(200, 88);
this.btnOpen.Name = "btnOpen";
this.btnOpen.Size = new System.Drawing.Size(72, 24);
this.btnOpen.TabIndex = 2;
this.btnOpen.Text = "显示窗体2";
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 32);
this.label1.Name = "label1";
this.label1.TabIndex = 3;
this.label1.Text = "显示Form2值:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 134);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnOpen);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void btnOpen_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2();
frm.myEvent+=new GetString(getStr);
frm.Show();
} public void getStr(string str)
{
this.textBox1.Text=str;
}
}
}
Form1全部代码:
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{ public event GetString myEvent; private System.Windows.Forms.Button btnOk;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnOk = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnOk
//
this.btnOk.Location = new System.Drawing.Point(152, 72);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(64, 23);
this.btnOk.TabIndex = 0;
this.btnOk.Text = "确定";
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 40);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "输入值:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 118);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnOk);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void btnOk_Click(object sender, System.EventArgs e)
{
myEvent(this.textBox1.Text);
}
}
}
Form2全部代码
public static strng textBoxtext
{
get { TextBox1.Text; } set { TextBox1.Text = value}}
-----------------Form1----------------------------
this.textbox1.Text = From2.txtBoxtext
网络上有几种方法,先总结如下:
调用窗体(父):Form1,被调用窗体(子):Form2
方法1: 所有权法
//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() ;
方法2:自身传递法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
Form2 f2 = new Form2() ;
f2.ShowDialog(this) ;
//Form2:
private Form1 p_f1;
public Form2(Form1 f1)
{
InitializeComponent();
p_f1 = f1;
}
//刷新时
p_f1.Refresh_Method() ;
方法3:属性法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.P_F1 = this;
f2.Show() ;//Form2:
private Form1 p_f1;
public Form1 P_F1
{
get{return p_f1;}
set{p_f1 = value;}
}
//刷新时
p_f1.Refresh_Method() ;
方法4:委托法
//声明一个委托
public delegate void DisplayUpdate();
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
Form2 f2 = new Form2() ;
f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;
f2.Show() ;
//Form2:
//声明事件
public event DisplayUpdate ShowUpdate;
//刷新时,放在需要执行刷新的事件里
ShowUpdate();
只是你在打开form2的时候,要把DataGrid的选中记录取到,传到form2中以便处理。
同时,你还要在form1中增加一个刷新方法,该刷新方法用来根据form2中传来的值对DataGrid进行处理。
Application.Run(窗体名称);
可以这样(窗体名称.txtText.Text=txtValue.Text)改变Form1中txtText了,绝对可以的
public static strng textBoxtext
{
get { TextBox1.Text; } set { TextBox1.Text = value}}
-----------------Form1----------------------------
this.textbox1.Text = From2.txtBoxtext------------------------------------------------------------------------------------
这里是不是有问题啊 static 不能实例化,怎么可能会有 TextBox1 让你操作啊? 我的意思是找不到 TextBox1 的
是 不是这样啊》??