winform 参数传递一个简单问题: 我想把登录窗体里的public 变量如 public string username(用户名), public string power(用户权限)点击登录按钮后将值传递到给MDI窗体中的两个变量中如何实现?请高手随便举个小例子!在此先谢过!急用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把这两个变量放在某个类中。。用属性进行设置就行了。。Class 公共类{ private string username; private string power; Public string UserName{get...set...}Public string Power{get...set...}}登录的时候:公共类.UserName=xxx公共类.Power=xxx大致是这样。。 winform用setting来设置,类似于web中的session一样 可以通过楼上写的通过他们的属性来访问或者你也可以将他们设置成static静态的public static string username(用户名), 在另个窗体通过类名.username来直接访问,但为了安全最好还是通过属性来访问 我没有用公共类,后来我想到这样弄也是可以,虽不是很好,但勉强可用)(跟3楼的方法类似):在登录窗体中public static string power;public static string username; public string getp { get { return power; } } public string getc { get { return username; } }在mDI 窗体中:定义公共变量:登录窗体 f=null;在MDI load 事件中:f=new 登录窗体();然后用f.getp和f.getc得到所需参数。1楼min_jie 的回复,很好建议跟我一样遇到这个问题的同仁,用他的方法。很感谢,大家的热情回复! 我前不就也在考虑这问题:方法一://在Form2中show窗体的代码,Form1中同理Form3 f3 = new Form3();f3.ShowDialog(this);//form3中这样写Form2 f2 = (Form2)this.Owner;Form1 f1 = (Form1)f2.Owner;//使用f1访问Form1公开的对象或方向就可以了f1.label1.Text="更新了";f1.更新方法();方法2:还有中就是mainform中 public void updataControl(string txt) { TextBox1.Text=txt; } private void button1_Click(object sender, EventArgs e) { SubForm frmSub= new SubForm (); //SubForm frmSub= new SubForm (this);//构造函数传对象 frmSub.frmMain= this; frmSub.show(); } 在SubForm public MainForm frmMain; private void buttonOK_Click(object sender, EventArgs e) { frmMain.updataControl (TextBox1.Text) ; this.Close() } 用static来修饰,用类名.字段名来调用 关于DateTime.toString(string)返回字符串中加入反斜杠的问题 StreamReader VS.NET2003下怎么做 wap 请教有关弹出快捷菜单的问题,欢迎赐教、关注,大家共同提高!!!! 为什么注释文档看不到? 请教一下如何在C# WEB 项目中使用media player 和 real player ?(50) 新手提问:关于<DropDownList>控件绑定,在线等... 关于工作流持久化 ReportView做的报表为啥身份证字段显示不了? CSC文件在那里? 名字空间的问题,急! c# 正则表达式
用属性进行设置就行了。。
Class 公共类
{
private string username;
private string power;
Public string UserName
{
get...
set...
}Public string Power
{
get...
set...
}
}登录的时候:
公共类.UserName=xxx
公共类.Power=xxx大致是这样。。
或者你也可以将他们设置成static静态的
public static string username(用户名),
在另个窗体通过类名.username来直接访问,但为了安全最好还是通过属性来访问
在登录窗体中
public static string power;
public static string username;
public string getp
{
get
{
return power;
}
}
public string getc
{
get
{
return username;
}
}
在mDI 窗体中:
定义公共变量:登录窗体 f=null;
在MDI load 事件中:
f=new 登录窗体();
然后用f.getp和f.getc得到所需参数。1楼min_jie 的回复,很好建议跟我一样遇到这个问题的同仁,用他的方法。很感谢,大家的热情回复!
方法一:
//在Form2中show窗体的代码,Form1中同理
Form3 f3 = new Form3();
f3.ShowDialog(this);
//form3中这样写
Form2 f2 = (Form2)this.Owner;
Form1 f1 = (Form1)f2.Owner;
//使用f1访问Form1公开的对象或方向就可以了
f1.label1.Text="更新了";
f1.更新方法();方法2:
还有中就是mainform中
public void updataControl(string txt)
{
TextBox1.Text=txt;
} private void button1_Click(object sender, EventArgs e)
{
SubForm frmSub= new SubForm ();
//SubForm frmSub= new SubForm (this);//构造函数传对象 frmSub.frmMain= this;
frmSub.show();
} 在SubForm public MainForm frmMain; private void buttonOK_Click(object sender, EventArgs e)
{
frmMain.updataControl (TextBox1.Text) ;
this.Close()
}