关于窗体传值问题 我有两个windowform窗体f1和f2,现想要将f1的一个全局变量string str传到f2的 textbox1中去,在f1事件代码怎么写,在f2的代码又怎么写。因为我传的只是一个值,所以不想再写一个类那么复杂,有没有更简单的方式来实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 'f1调用f2代码dim f2 as new form2f2.textbox1.text=me.strf2.showdialog Form1 中private string str="This is a param";private btnShow_Click(object sender,EventArgs e){ Form2 form2 = new Form2(); form2.Show();}Form2 中重写构造方法public Form2(string str){ InitializeComponent(); this.textBox1.Text=str;} 楼上的不错,就是漏了在FROM1中声明FORM2的时候传入的STR了Form1 中private string str="This is a param";private btnShow_Click(object sender,EventArgs e){ Form2 form2 = new Form2(str); form2.Show();}Form2 中public Form2(string str){ InitializeComponent(); this.textBox1.Text=str;} f1的一个全局变量string str 是什么样的啊,public还是private?如果是public 就可以直接访问的。如果是private,你可以根据你的具体需求在其中一个Form中设置一个public的static变量。通过它来传递值。 public delegate void mydelegate(string info);//定义一个委托类型//Form1的代码public partial class Form1 : Form { . .. ....//省略系统代码 public static event mydelegate myevent;//定义事件 //当按钮被点,就传递数据 private void button2_Click(object sender, EventArgs e) { string s = "aa";//要传递的数据,这样的数据不需要全局的变量 myevent(s);//触发事件 } //创建Form2窗体,其实可以在任何地方创建都无所谓 private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.Show(); }}//Form2中的代码 private void Form2_Load(object sender, EventArgs e) { Form1.myevent += new mydelegate(Form1_myevent);//实例化事件对象 } void Form1_myevent(string info)//定义事件调用的方法 { this.textBox1.Text = info; } 这样的好处是不需要将Form1和Form2关联起来比如:Form1的button1_Click创建的是Form3Form3的button1_Click创建的是Form2当我们点Form1的button1时候仍然能将数据传递到Form2用其他方法的灵活性不强 为自己的前一个Form增加一个属性:private string changeMessage="";///<summary>///交换信息的属性///</summary>public string ChangeMessage{ get{return this.changeMessage;} //只保留一个读的属性} Form1 中private string str="This is a param";private btnShow_Click(object sender,EventArgs e){ Form2 form2 = new Form2(str); form2.Show();}Form2 中public Form2(string str){ InitializeComponent(); this.textBox1.Text=str;} Form1 中 public static string str="This is a param"; private btnShow_Click(object sender,EventArgs e) { Form2 form2 = new Form2(str); form2.Show(); } Form2 中 this.textBox1.Text=Form1.str; form1public string aa = "ssssssxs";Form2 form = new Form2(aa);form2InitializeComponent(); this.textBox.Text = aa; FORM1中 public string str = " this is form1"; private void button1_Click(object sender, EventArgs e) { //string str = "-1-2-3-4-5"; //foreach (string s in str.Split('-')) //{ // listBox1.Items.Add(s); //} ////textBox1.Text = textBox1.Text + "-"; //bind("tablea"); Form2 form2 = new Form2(str); form2.Show(); }FORM2中public Form2(string str) { InitializeComponent(); textBox1.Text = str; } f1中定义一个class Global { public static string n; } 赋值:Global.n="zifuchuan";f2中 textBox1.Text = Global.n.ToString(); FORM1中public string str = " this is form1";private void button1_Click(object sender, EventArgs e){ Form2 form2 = new Form2(str); form2.Show();}FORM2中public Form2(string str){ InitializeComponent(); textBox1.Text = str;} 用构造函数呀,3楼的就很正确的。当Show窗体的时候在构造函数里面把那个变量传过去。你试一下。 不知道为什么都喜欢构造函数传值,如果两个窗体已经存在数据是在Form用户操作后再传递到Form2,难道要重新New一个新的 Form2窗体出来么!!!在Windows应用程序中,消息处理机制才是基础,一个窗体发送消息,一个窗体接收消息处理数据才是王道,请在代码设计过程中注意代码的灵活性,维护性和开展性的问题,不能是把代码写成死的!!! 求大神解决下代码问题 位数不够补零 VS2005经常在Debug一次后输入时失去焦点 根据计算机名称如何获取IP? Janus gridEX 怎么样进行数据绑定 vs2005是否提供自动生成所有重载 关于用itextsharp填写pdf表单的问题 请教 如何通过操作数据库实现用户身份验证?小弟是菜鸟,各位大哥帮帮忙!高分求教 数据库中的内容变了以后,怎样刷新DataGrid里面的显示,急,在线等候 xml反序列化的问题 C# 三层开发连接数据库(sql server2005)问题?非常感谢帮助一下?
dim f2 as new form2
f2.textbox1.text=me.str
f2.showdialog
Form1 中
private string str="This is a param";
private btnShow_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
Form2 中
重写构造方法
public Form2(string str)
{
InitializeComponent();
this.textBox1.Text=str;
}
楼上的不错,就是漏了在FROM1中声明FORM2的时候传入的STR了
Form1 中
private string str="This is a param";
private btnShow_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2(str);
form2.Show();
}Form2 中
public Form2(string str)
{
InitializeComponent();
this.textBox1.Text=str;
}
public partial class Form1 : Form
{
. .. ....//省略系统代码
public static event mydelegate myevent;//定义事件
//当按钮被点,就传递数据
private void button2_Click(object sender, EventArgs e)
{
string s = "aa";//要传递的数据,这样的数据不需要全局的变量
myevent(s);//触发事件
}
//创建Form2窗体,其实可以在任何地方创建都无所谓
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
}//Form2中的代码
private void Form2_Load(object sender, EventArgs e)
{
Form1.myevent += new mydelegate(Form1_myevent);//实例化事件对象
} void Form1_myevent(string info)//定义事件调用的方法
{
this.textBox1.Text = info;
}
比如:
Form1的button1_Click创建的是Form3
Form3的button1_Click创建的是Form2
当我们点Form1的button1时候仍然能将数据传递到Form2用其他方法的灵活性不强
///<summary>
///交换信息的属性
///</summary>
public string ChangeMessage
{
get{return this.changeMessage;} //只保留一个读的属性
}
private string str="This is a param";
private btnShow_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2(str);
form2.Show();
}Form2 中
public Form2(string str)
{
InitializeComponent();
this.textBox1.Text=str;
}
public static string str="This is a param";
private btnShow_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2(str);
form2.Show();
} Form2 中 this.textBox1.Text=Form1.str;
public string aa = "ssssssxs";
Form2 form = new Form2(aa);form2
InitializeComponent();
this.textBox.Text = aa;
public string str = " this is form1";
private void button1_Click(object sender, EventArgs e)
{
//string str = "-1-2-3-4-5";
//foreach (string s in str.Split('-'))
//{
// listBox1.Items.Add(s);
//}
////textBox1.Text = textBox1.Text + "-";
//bind("tablea");
Form2 form2 = new Form2(str);
form2.Show();
}
FORM2中
public Form2(string str)
{
InitializeComponent();
textBox1.Text = str;
}
class Global
{
public static string n;
}
赋值:
Global.n="zifuchuan";
f2中 textBox1.Text = Global.n.ToString();
public string str = " this is form1";
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(str);
form2.Show();
}FORM2中
public Form2(string str)
{
InitializeComponent();
textBox1.Text = str;
}
数据是在Form用户操作后再传递到Form2,难道要重新New一
个新的 Form2窗体出来么!!!
在Windows应用程序中,消息处理机制才是基础,一个窗体发送
消息,一个窗体接收消息处理数据才是王道,请在代码设计过程
中注意代码的灵活性,维护性和开展性的问题,不能是把代码写
成死的!!!