C#设置其他窗体的控件属性

来源:百度知道 编辑:UC知道 时间:2024/07/04 16:39:04
我有两个窗体a和b,要在窗体b中设置a的text1.Text="123",请问如何实现(两个窗体不是MDI关系)
请大家把回答改一改,是a用ShowDialog方法打开b,所以,不能用new函数定义新的a

两个办法
1、把a窗体的text1设置为public
在b窗体中:((FormA)(a)).text1.Text="123";
其中,FormA是a窗体的类名,a是窗体变量

2、在a窗体中添加一个public方法
public void SetTextBox(string text)
{
text1.Text=text;
}
在b窗体中调用:((FormA)(a)).SetTextBox("123");

假如窗体a为 Form2
在构造函数中传值:
public Form2(string s)
{
InitializeComponent();
this.textBox1.Text = s;
}

在窗体B中的按钮事件中写如下代码:
Form2 f2=new Form2("123");
f2.ShowDialog();

在方法shou()的时候将参数传过去,
我给你个小例子。。。

a窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private vo