值的传递! 我用C#做了2个Form1(父),Form2。现在我要把Form1里面的textbox内容传递到Form2的一个textbox里面。 请问:怎样传递过去?要写一个方法吗?请给你的思想和代码?谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 建一个类类中用一个静态字段存储把Form1里面的textbox的值 Form1 frn = new Form1 (你要的传的值)然后在Form2的窗体构造函数中 值的类型 strs; public Form1(值的类型 str) { InitializeComponent(); this.strs = str; } Form2 frn = new Form2 (你要的传的值)frn.show();然后在Form2的窗体构造函数中 值的类型 strs; public Form1(值的类型 str) { InitializeComponent(); this.strs = str; }上面的错了! 窗体间传值的问题方法很多,如你在Form1 中设置一个全局的属性,再From2中访问。或者你直接封装From1 的那个TextBox,在From2中用From1 的对象访问它的Text值如:Public MyTextBox{ get {retrun textBox1;}}在from2中用From1的对象(可以通过构造函数传)frm.MyTextBox.Text赋值给From2的文本框 form间传值通过公共静态类进行传值; 通过绑定事件进行传值; 使用Attribute public partial class Form1 : Form { private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(this); } } public partial class Form2 : Form { private void button1_Click(object sender, EventArgs e) { Form1 frm1 = (Form1)this.Owner; ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text; this.Close(); } } 定义一个public变量,存放 Form1里面的textbox(public string str)在form2中 声明form1的对象,调用刚才的变量,赋值给form2中的textboxfrom1 obj=new from1();textbox.Text=obj.str; 方法有很多!一:把form1和form2中的textbox访问级别都由private 该为publict直接用控件传值textbox.modifiers=public;//Form1中:Form f2=new Form2();f2.owner=this;//Form2中Form1 f1=(Form1)this.owner;this.textbox.Text=f1.textbox.Text;二:利用属性传值//在Form1中设置一个全局静态的字段例如:public static string content;content=textbox.Text;//直接在Form2中写this.textbox.Text=Form1.Content;三:利用事件该方法比较麻烦,一般情况下建议不要使用//声明事件所用到的类 声明在Form1中Class EventArgesGet:EventArges{ private string content; public EventArgesGet(string s) { content=s; } public string Contnt { get{return content;} } set{content=value; };}//声明事件所需的委托 声明在Form1中public void delete myGetEventHandler(object sender,EventArgesGet e);//事件声明 声明在Form1中public event mydelete myGet;//可在在Form1中的某个方法或事件中写上事件的激发:myget(this,new EventArgesGet(this,textbox.Text));//在Form2中声明事件所代理的方法pub void getValue(object sender,EventArgesGet e){ this.text.Text=e.Content;}//然后追加事件Form1 f1=new Form1();f1.myGet+=new myGetEventHandler(getValue);public voi set(object sender,EventArgesGet e){ this.textbox.Text=e.Content;} 不好意思上述事件声明关键字有问题:应为://声明事件所需的委托 声明在Form1中public delegate myGetEventHandler(object sender,EventArgesGet e); 不好意思上述事件声明关键字有问题:应为://声明事件所需的委托 声明在Form1中public delegate void myGetEventHandler(object sender,EventArgesGet e); iTextSharp.text.pdf如何获取图片的位置 WindowsMoblie安装程序制作问题 关于c#连接oracle 请大家帮忙啊,谢谢啊! C#2005无法弹出错误列表?求救!!!!!!!!!!!!!! 数据邦定问题。。菜鸟求教 SQL SERVER的datetime属性读取求救 Adapter设计模式 C#做一个防止网站服务器遭受攻击的程序,请教思路. WPF安装部署项目 .NET的功能如何在其他语言中运用? webBrowser1_DocumentCompleted 开启一个线程奇怪的问题。 如何将Datagirdview中的数据写到数据库中
类中用一个静态字段存储把Form1里面的textbox的值
值的类型 strs;
public Form1(值的类型 str)
{
InitializeComponent();
this.strs = str;
}
frn.show();然后在Form2的窗体构造函数中
值的类型 strs;
public Form1(值的类型 str)
{
InitializeComponent();
this.strs = str;
}
上面的错了!
方法很多,如你在Form1 中设置一个全局的属性,再From2中访问。
或者你直接封装From1 的那个TextBox,在From2中用From1 的对象访问它的Text值
如:
Public MyTextBox
{
get {retrun textBox1;}
}
在from2中
用From1的对象(可以通过构造函数传)frm.MyTextBox.Text赋值给From2的文本框
通过公共静态类进行传值;
通过绑定事件进行传值;
使用Attribute
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);
}
} public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
this.Close();
}
}
textbox.Text=obj.str;
一:把form1和form2中的textbox访问级别都由private 该为publict直接用控件传值
textbox.modifiers=public;
//Form1中:
Form f2=new Form2();
f2.owner=this;//Form2中
Form1 f1=(Form1)this.owner;
this.textbox.Text=f1.textbox.Text;
二:利用属性传值
//在Form1中设置一个全局静态的字段
例如:
public static string content;
content=textbox.Text;
//直接在Form2中写
this.textbox.Text=Form1.Content;
三:利用事件
该方法比较麻烦,一般情况下建议不要使用
//声明事件所用到的类 声明在Form1中
Class EventArgesGet:EventArges
{
private string content;
public EventArgesGet(string s)
{
content=s;
}
public string Contnt
{
get{return content;}
} set{content=value; };
}
//声明事件所需的委托 声明在Form1中
public void delete myGetEventHandler(object sender,EventArgesGet e);
//事件声明 声明在Form1中
public event mydelete myGet;
//可在在Form1中的某个方法或事件中写上事件的激发:myget(this,new EventArgesGet(this,textbox.Text));
//在Form2中声明事件所代理的方法
pub void getValue(object sender,EventArgesGet e)
{
this.text.Text=e.Content;
}
//然后追加事件
Form1 f1=new Form1();
f1.myGet+=new myGetEventHandler(getValue);public voi set(object sender,EventArgesGet e)
{
this.textbox.Text=e.Content;
}
应为:
//声明事件所需的委托 声明在Form1中
public delegate myGetEventHandler(object sender,EventArgesGet e);
应为:
//声明事件所需的委托 声明在Form1中
public delegate void myGetEventHandler(object sender,EventArgesGet e);