如题
解决方案 »
- c# 画图如何选择性的显示图案
- C#如何动态创建对象
- string 的问题
- 一个函数通过事件不断的向外发送事件,在另一个函数中则需要点一次按钮,才接受此次数据。 如何才能实现不丢数据呢????
- “ASP.NET配置”的访问规则是写在哪里的?
- 求助﹕ 使用 DataList的 SelectedItemTemplate時﹐提示保護級別出錯
- VS2003自带的打包程序可以在安装过程中运行bat文件吗?
- 关于datagridview的问题 求大神帮忙啊
- 线程的同步与互斥
- datagridview控件一行显示内容太多,拖动显示不全怎么办
- asp.net 生成静态页面,求救!!
- 关于Mail发送的问题,紧急求助,郁闷好久了!
在B中保存一个A的指针m_AForm,在A中打开B时,给该变量赋值
bform.m_AForm = this;
bform.Show();然后在B的相应代码中写上
m_AForm.textBox1.text='aaa';
m_AForm.textBox2.text='bbb';
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 窗体间传输数据3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private Form2 myForm = new Form2 ( );
private System.Windows.Forms.RadioButton radioButton1;
/// <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.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 80);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "传递来的数据";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(136, 80);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 144);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "显示Form2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(368, 144);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 3;
this.radioButton1.Text = "radioButton1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 273);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
myForm.Send += new Form2.SendMess ( Send ) ;
}
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
myForm.ShowDialog ( ) ;
//显示从窗体
} private void Send(string str)
{
radioButton1.Text = str ;
//把接收来的字符串通过radioButton1组件显示出来
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace 窗体间传输数据3
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RadioButton radioButton1;
public delegate void SendMess (string str);//定义委托类型
public event SendMess Send;//定义一个事件类型
//public delegate void SendMess (bool str);//定义委托类型
//public event SendMess Send;//定义一个事件类型 /// <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.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 23);
this.label1.TabIndex = 0;
this.label1.Text = "传递到主窗体的字符串";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(144, 48);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 168);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "传递";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// radioButton1
//
this.radioButton1.Enabled = false;
this.radioButton1.Location = new System.Drawing.Point(72, 112);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 3;
this.radioButton1.Text = "radioButton1";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
//if (Send != null)
//{
try
{
//Send(this.textBox1.Text);
Send(this.button1.Text);
}
catch
{
;
}
//}
}
}
}
在B窗体的一些事件激发时设置TextBox引用中的相关属性,比如Text.2.对B中要激发的事件在A窗体的类中进行注册,比如private void OpenBForm()
{
FormB b = new FormB();
b.Load += new System.EventHandler(this.Form1_Load);
}
//
private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Text = "数据1";
this.TextBox2.Text = "数据2";
}//第二种方法,在具体使用时可能需要修改,比如这里不一定是Form.Load事件,有可能是自己写一个事件,对于参数,这里的System.EventArgs e可能需要更具体的类型来写,然后通过e.Value1,e.Value2来设置textBox1或textBox2.
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
A窗口里面写一个函数(函数就是你要做的操作)
然后在B窗口调用这个函数,比刚才那样传递要方便的多
呵呵 我也是菜鸟哈