c#不同窗体中的传值 c#,不同的窗体怎么实现传值?比如说我想将form1的一个字符串传给form2,在form2中可以使用form1的赋值字符串. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Form2窗体中 声明 form1窗体public Form1 form1;Form1窗体 中Form2 form2=new Form2();form2.form1=this;这样Form2窗体就能调用form1里面的方法和属性了。 比如给form1添加个静态字段,在比如添加构造函数Form2 form2=new Form2(“你要传的参数”); 一楼form2.form1=this;总是报错:类,接口或成员声明中的标记“=”无效 3楼我要怎么在form2中调用它呢??? 在form2中创建构造函数,获取传入的值。 form1:string info="内容";form2 f=new from2(info);form2:priva string info;public form2(string info){//窗体初始化this.info=info;} public form2(string info){//窗体初始化this.info=info;}/// 就是建立一个构造函数 form1:string info="内容";form2 f=new from2(info);就是你要传的内容放在string里 class Form2{public int i{set{}get{}}}class Form1{public void Show(){Form2 frm =new Form2();frm.i=1;frm.Show();}}或者说使用一个静态变量作为全局变量来使用 在form2中定义公开的属性在form1实例化form2时给属性赋值这个属性的值在form2中就能使用了 给力的7楼比如,我在form1中写 else if (cbStatus.Text == "学生") { string sqlString = "select count(*) from Student where stuName='" + this.tbName.Text.Trim() + "' and stuPwd='" + this.tbPwd.Text.Trim() + "'"; SqlCommand command = new SqlCommand(sqlString, connection); int num = (int)command.ExecuteScalar(); if (num == 1) { Stu name = new Stu(tbName.Text.Trim()); stu.Show(); this.Visible = false; } }在form2中写: private string name; public Stu(String name) { InitializeComponent(); this.name = name; } private void 基本信息ToolStripMenuItem_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(connString); string sqlString = "select * from Student Where stuName='"+name; SqlCommand comm = new SqlCommand(sqlString,connection); }可是结果并没有成功的读取啊求解 建议加断点看看name是否传过来 sql语句对吗?怎么感觉"select * from Student Where stuName='"+name+"'"好呢 添加断点发现form1中的 stu.name = tbName.Text;tbName.Text有值,正确;stu.name 为null。求解。 因为那个要读取textbox中写入的值我觉得好像要这么写 form2中的name有值,值还是正确的 这个问题咋个这么出现得平凡啊。http://topic.csdn.net/u/20110422/10/1322f22f-1d30-497f-b68f-4811d491d0af.html详见 自己写一个构造函数在传递时的 new from1(ddd,ddd);另一个窗体的 初始化里面接收参数 看下这个就明白了http://blog.csdn.net/jvbss/archive/2011/03/10/6237276.aspx 自己给form增加个属性,随时可以赋值和取值。使用最方便。 form2 frm = new form2(可以在这里提供要传递的参数); 哎,.net果然很火啊!这么一个幼稚的问题都有一大堆人来解决,我在C/C++板块提一个问题,大半个月也没人理的 在Form1中添加公共属性,把Form2中内容赋值于该属性。 C# 主从表 操作 字符中含有某字符的判断条件怎么写? 将整个页面作为邮件发送时,该页面中的图片在邮箱中无法显示 数据库操作类 请问.netPNG图片显示不正常,如何处理 问个菜鸟的存储过程问题!!! [请教] .net C/S 架构框架 C#winform中放大缩小拖动picturebox中的图片 C#中的图标让我好无语!!!!! 关于时间界限判断的问题。 程序运行后当前控件的选定如何选定 C# 播放mp3怎么获取当前时间 总时间,音量,定位
public Form1 form1;Form1窗体 中
Form2 form2=new Form2();
form2.form1=this;这样Form2窗体就能调用form1里面的方法和属性了。
string info="内容";
form2 f=new from2(info);form2:
priva string info;public form2(string info)
{
//窗体初始化
this.info=info;
}
{
//窗体初始化
this.info=info;
}
/// 就是建立一个构造函数 form1:
string info="内容";
form2 f=new from2(info);就是你要传的内容放在string里
public int i{
set{}
get{}
}
}class Form1{
public void Show(){
Form2 frm =new Form2();
frm.i=1;
frm.Show();}}
或者说使用一个静态变量作为全局变量来使用
比如,我在form1中写
else if (cbStatus.Text == "学生")
{
string sqlString = "select count(*) from Student where stuName='" + this.tbName.Text.Trim() + "' and stuPwd='" + this.tbPwd.Text.Trim() + "'";
SqlCommand command = new SqlCommand(sqlString, connection);
int num = (int)command.ExecuteScalar();
if (num == 1)
{
Stu name = new Stu(tbName.Text.Trim()); stu.Show();
this.Visible = false;
}
}
在form2中写:
private string name;
public Stu(String name)
{
InitializeComponent();
this.name = name;
}
private void 基本信息ToolStripMenuItem_Click(object sender, EventArgs e)
{ SqlConnection connection = new SqlConnection(connString);
string sqlString = "select * from Student Where stuName='"+name;
SqlCommand comm = new SqlCommand(sqlString,connection);
}可是结果并没有成功的读取啊求解
"select * from Student Where stuName='"+name+"'"好呢
tbName.Text有值,正确;stu.name 为null。求解。
因为那个要读取textbox中写入的值我觉得好像要这么写
form2中的name有值,值还是正确的
详见