两个窗体之间,在一个窗体代码中对另一个窗体中的控件赋值该怎么写?

suinx 2002-12-09 02:17:36
例如:form1,form2两个窗体,from2中有几个文本框,
现在form1的类代码中要将几个值赋给form2中的那几个文本框.
那么在form1中的代码该怎样写?
...全文
319 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sy_programmer 2002-12-09
  • 打赏
  • 举报
回复
同意 xumahua 说的,但还可以用vb6升级到.net时自动创建的代码,用完后操作就与vb6相同!很方便,但不知有没有更好的办法
Dugu_Niu 2002-12-09
  • 打赏
  • 举报
回复
to hongshun(红顺):
请看清楚它的问题是发在那个版块的,是VB.net呀,你却用C#的代码来回答,FT!
hongshun 2002-12-09
  • 打赏
  • 举报
回复
//
// form1 主
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;

private Form2 pf2;

private Form3 pf3;
/// 必需的设计器变量。
/// </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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 136);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 64);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(304, 128);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 64);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 461);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

public void a1()
{
this.pf3.textBox1.Text ="sdfsdfsdf";

}

private void button1_Click(object sender, System.EventArgs e)
{
this.pf2= new Form2(this);
this.pf2.Show ();

}

private void button2_Click(object sender, System.EventArgs e)
{
this.pf3= new Form3(this);
this.pf3.Show ();

}

}
}





//
// form 2 可以访问 form3
//



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication2
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private Form1 pf;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2(Form1 f1)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.pf=f1;

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 152);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(184, 72);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

private void Form2_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
this.pf.a1();
}
}
}


//
// form3 被form 更改上边的文本
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication2
{
/// <summary>
/// Form3 的摘要说明。
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
public System.Windows.Forms.TextBox textBox1;
private Form1 pf;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form3(Form1 f3)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 120);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 357);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1});
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false);

}
#endregion

private void Form3_Load(object sender, System.EventArgs e)
{

}
}
}



//一个我方法的小例子 .

xumahua 2002-12-09
  • 打赏
  • 举报
回复
建立一个标准模块(moudle),在其中为想要操作的窗体定义一个公共实例:
Public frm2 as new frim2()
然后
在窗体form2的Load事件中写:frm2=me
接下来你就可以用frm2这个公共实例来做你要做的事了:)
如在form1中写:frm2.textbox1.text=me.textbox1.text
suinx 2002-12-09
  • 打赏
  • 举报
回复
我的假设是form1,form2都是父窗体创建的.
而且两窗体都是已调用.show()方法显示
Montaque 2002-12-09
  • 打赏
  • 举报
回复
form2:
Public Shared txtDemo As String
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = txtDemo
End Sub
form1调用:
Form2.txtDemo = "123"
Dim frmNew As New Form2()
frmNew.Show()


hongshun 2002-12-09
  • 打赏
  • 举报
回复
如果 form2 是form1 创建的 . 问题很简单.

如果两个都是 一个父窗口创建的. 可以通过在父窗口中写一个公共的方法

通过这个方法来些你需要的方法. 我用这个笨方法 这个讲起来好像很费劲.
来实现.不只有什么好的方法.学习 学习

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧