我在A 窗体里 打开了一个新窗体B 想在B 的某些时间触发后给A 的两他 TextBox 赋值,该怎么做(100)在线等马上给分 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最简单的方法:在B中保存一个A的指针m_AForm,在A中打开B时,给该变量赋值bform.m_AForm = this; bform.Show();然后在B的相应代码中写上m_AForm.textBox1.text='aaa';m_AForm.textBox2.text='bbb'; 或者你在打开B窗体的时候把A窗体上面的TextBox传给B窗体,然后在B窗体里面赋值。 哦,不好意思,我忘记说了 是 .net 环境下的,我用B调用 A 页面的公共方法,老报错说没将A的TextBox引用到实例。 A窗口using System;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组件显示出来 } }} B窗口 记的给我分不要骗我哈using System;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 { ; } //} } }} 1。在实例化B窗体的时候,在构造函数中传入A窗体中的TextBox的引用。在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 kelvin1981 (菜菜菜) 还没给分呢? kelvin1981 (菜菜菜) 太不厚道了,居然不给分 其实还有一个办法可以进行窗口传递思路是比如:A主窗口,B从窗口A窗口里面写一个函数(函数就是你要做的操作)然后在B窗口调用这个函数,比刚才那样传递要方便的多呵呵 我也是菜鸟哈 C#仿WINDOWS可折叠导航栏 如何将数据文件中的连续多个空格替换成1个空格 winform 提取txt文件 在线等 提取排名 错误 3 当前上下文中不存在名称“printPreviewDialog1” 请问:如何在c#中获得游戏中八方向键的效果? 请问如何实现输出精度. 主从关系 怎样在C#中使用消息处理? 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窗口调用这个函数,比刚才那样传递要方便的多
呵呵 我也是菜鸟哈