如题!
两个FORM窗体,A是登录窗体,B是主程序窗体.
在A登录窗体中,当输入用户名,用户密码,当按"确认"时.
就去select数据库中的用户名,用户密码,然后比对,当错误就"提示",
当正确就打开B主程序窗体.问题是B主程序窗体中的一个lable要显示登录用户名.请教大侠们,如何实现??!!在线等待中...
两个FORM窗体,A是登录窗体,B是主程序窗体.
在A登录窗体中,当输入用户名,用户密码,当按"确认"时.
就去select数据库中的用户名,用户密码,然后比对,当错误就"提示",
当正确就打开B主程序窗体.问题是B主程序窗体中的一个lable要显示登录用户名.请教大侠们,如何实现??!!在线等待中...
到处都能用它
{
...
public string userName;
}public class FormA:Form
{
...
private void btnLogin_Click(object sender, EventArgs e)
{
if(succeeded)
{
FormB b= new FormB();
b.userName = txtUserName.Text;
b.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入您的信息");
textBox1.Text = textBox2.Text = "";
}
else
{
SqlConnection Con = new SqlConnection("Data Source=ZSHUIZHI-5877C0; Initial Catalog=EX_NEW; User ID=sa;Pwd=pp;");
String SqlCmd = "select count(*) from [USER] where UserName='" + textBox1.Text + "' and Userpassword='" + textBox2.Text + "'";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
Con.Open();
int i = (int)CmdObj.ExecuteScalar();
if (i == 0)
{
MessageBox.Show("请检查您的用户名和密码");
textBox1.Text = textBox2.Text = "";
}
else
{
this.Hide();
new webcam.Form1().ShowDialog();
}
Con.Close();
}
}
或通过XML等实现保存
public class GlobalInfo
{
public string UserName;
}
FormA 中
GlobalInfo.UserName = 你的用户名;FormB 中
xxx = lobalInfo.UserName ;
class model
{//私有的构造函数
private model(string password)
{}
public Static GetInstance()
{
if(null!=_model)
{
return _model;
}
else
{
return _model =new model();
}
}
//以下是属性
public string userId
{
get{return _userId;}
set{_userId=value;}
}
public string password
{
get{return _password;}
set{_password=value;}}private string _userId;//用户ID
private string _password;//密码
private static model _model=null;//私有静态对象
}
以上这个类使用方法是
在登录得时候 赋值
model.GetInstance().userId=用户密码;
model.GetInstance().password=用户密码;其他窗体赋值
string uid=model.GetInstance().userId;
string password =model.GetInstance(). password;
B.Label = username;别忘了 把Label 弄成 public 或者 弄个赋值的方法
这一句里有报错;
定义 一个user 类 传给B
B.UserInfo = userinfo如果 窗口有很多的话 可以考虑全局类 这样大家可以共享
private model(string password)
=>
private model()
而属性不同,你可以不实现 set,只实现 get,比如,根据用户判断权限。
我们不可能用静态类来 存用户ID,为安全性着想。哈哈
public partial class LoginForm : Form
{
//在别处定义,里面只有Username和Password属性
//用来保存用户名和密码
// class UserInfo
//{
// public string Username;
// public string Password;
//}
public LoginUserInfo UserInfo; //用于在单例中返回
private static LoginForm _loginForm = null; private LoginForm()
{
InitializeComponent();
}
public static LoginForm GetInstance()
{
if (null == _loginForm)
_loginForm = new LoginForm(); return _loginForm;
}
//设置DialogResult = DialogResult.OK
private void buttonLogin_Click(object sender, EventArgs e)
{
UserInfo = new LoginUserInfo();
UserInfo.Username = textBoxUsername.Text;
UserInfo.Password = textBoxPassword.Text;
}
}MainForm:
public partial class MainForm : Form
{
private LoginForm _loginForm = null; public MainForm()
{
InitializeComponent();
_loginForm = LoginForm.GetInstance();
DialogResult result = _loginForm.ShowDialog();
if (result == DialogResult.OK)
{
labelInfo.Text = String.Format("Username: {0}, Password: {1}", _loginForm.UserInfo.Username, _loginForm.UserInfo.Password);
}
}
}
model.GetInstance().userId=textBox1.Text;然后在B FORM也插入了
public Form1()
{
string uid = model.GetInstance().userId;
InitializeComponent();
lbText.Text = uid ;
}
但lbText.Text 没显示....
试试写在窗体的LOAD事件里面
如果还没有明白的话,我就按照你的方式给你填几句代码吧public static string strLogName=string.Empty;//添加这句
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入您的信息");
textBox1.Text = textBox2.Text = "";
}
else
{
SqlConnection Con = new SqlConnection("Data Source=ZSHUIZHI-5877C0; Initial Catalog=EX_NEW; User ID=sa;Pwd=pp;");
String SqlCmd = "select count(*) from [USER] where UserName='" + textBox1.Text + "' and Userpassword='" + textBox2.Text + "'";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
Con.Open();
int i = (int)CmdObj.ExecuteScalar();
if (i == 0)
{
MessageBox.Show("请检查您的用户名和密码");
textBox1.Text = textBox2.Text = "";
}
else
{
strLogName=this.textBox1.Text;//假设TextBox1文本框是输入的用户名
this.Hide();
new webcam.Form1().ShowDialog();
}
Con.Close();
}
}
//下面代码是在你的主窗体的Load事件来写
this.label1.Text=Form1.strLogName;//假设Form1是你的登陆窗口
在show B 之前,调用 model.GetInstance().userId=textBox1.Text;
还是不行贴下你 show B 代码,有那么玄乎么。呵呵
using System.Collections.Generic;
using System.Text;namespace Login
{
public class model
{ //私有的构造函数
public model()
{ }
public static model GetInstance()
{
if (null != _model)
{
return _model;
}
else
{
return _model = new model ();
}
}
//以下是属性
public string userId
{
get { return _userId; }
set { _userId = value; }
}
public string password
{
get { return _password; }
set { _password = value; } } private string _userId;//用户ID
private string _password;//密码
private static model _model = null;//私有静态对象
} }
这是登录界面:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Collections;
using System.Data.SqlClient;
using System.IO;
namespace Login
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent(); }
public void button1_Click(object sender, EventArgs e)
{ if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入您的信息");
textBox1.Text = textBox2.Text = "";
}
else
{
SqlConnection Con = new SqlConnection("Data Source=ZSHUIZHI-5877C0; Initial Catalog=EX_NEW; User ID=sa;Pwd=photosystem;");
String SqlCmd = "select count(*) from [USER] where UserName='" + textBox1.Text + "' and Userpassword='" + textBox2.Text + "'";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
Con.Open();
int i = (int)CmdObj.ExecuteScalar();
if (i != 0)
{
this.Hide();
new webcam.ConsoleForm().ShowDialog(); //这里是Show()B
model.GetInstance().userId = textBox1.Text;//这里是传值
}
else
{ MessageBox.Show("请检查您的用户名和密码");
textBox1.Text = textBox2.Text = ""; }
Con.Close();
}
} private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}这是主程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Collections;
using System.Data.SqlClient;
using System.IO;
using Login;
//using System.Data.OracleClient;namespace webcam
{
public partial class ConsoleForm : Form
{
string uid = model.GetInstance().userId;
private void ConsoleForm_Load(object sender, EventArgs e)
{
string uid = model.GetInstance().userId; label8.Text = uid; }
public ConsoleForm()
{
InitializeComponent();
}
private void ConsoleForm_FormClosing(object sender, FormClosingEventArgs e)
{
Application.ExitThread();
}
}}
public class A
{
private static string _LogedUserName;
public static string LogedUserName
{
get
{
return _LogedUserName;
}
set
{
_LogedUserName = value;
}
}
}然后,在A中
private void btnOK_Click(object sender, EventArgs e)
{
if(输入密码正确)
{
_LogedUserName = this.cmbUser.Text;
}
}最后在B中需要显示登录用户名的地方写上:this.lable.text=A.LogedUserName
【原创家庭记账簿1.2.5,简单实用,通俗易懂,欢迎工薪阶层、家庭主妇下载使用 http://download.csdn.net/source/1900400】
我是那么的愚蠢呀...
问题解决了....
原来我将这两句顺序反过来就行了...错误时:
new webcam.ConsoleForm().ShowDialog();
this.Hide();
model.GetInstance().userId = textBox1.Text;正确为:
model.GetInstance().userId = textBox1.Text;
this.Hide();
new webcam.ConsoleForm().ShowDialog();
对不起大家...我是一级大笨蛋...对不起浪费了大家这么多时间,不好意思....散分