如题!
两个FORM窗体,A是登录窗体,B是主程序窗体.
在A登录窗体中,当输入用户名,用户密码,当按"确认"时.
就去select数据库中的用户名,用户密码,然后比对,当错误就"提示",
当正确就打开B主程序窗体.问题是B主程序窗体中的一个lable要显示登录用户名.请教大侠们,如何实现??!!在线等待中...

解决方案 »

  1.   

    form a show form b的时候 在b的构造函数里将name传递过去 不知道行不行
      

  2.   

    在Show()B的时候传过去,或者想上面说的设置一个全局的静态类,或全局变量来存放喽
      

  3.   

    说白了就是弄个public的变量
    到处都能用它
      

  4.   

    public class FormB:Form
    {
    ...
        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();
            }
        }
      

  5.   

    这是Show()B的时候
    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();
                }
                        }
      

  6.   

    设置全局变量
    或通过XML等实现保存
      

  7.   

    全局的静态类,或全局变量来存放喽
    public class GlobalInfo
    {
      public string UserName;
    }
    FormA 中
      GlobalInfo.UserName = 你的用户名;FormB 中
      xxx = lobalInfo.UserName ;
      

  8.   

    一般 专业点的做法是 用一个单例 的属性类
    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;
      

  9.   

    改正一下 :public Static GetInstance()=>public Static model GetInstance()
      

  10.   

    搞那么复杂干啥呢在B.Show()后面加上
    B.Label = username;别忘了 把Label 弄成 public  或者 弄个赋值的方法
      

  11.   

    return _model =new model(); 
    这一句里有报错;
      

  12.   

    return _model =new model(); “model”不包含采用“0”参数的构造函数
      

  13.   

    大家说得很详细了。我建议lz放下手中的工作,只要花1星期的时间,找本书看下,把C#熟悉下,这样就自然明白了。
      

  14.   


    定义 一个user 类 传给B
    B.UserInfo = userinfo如果 窗口有很多的话 可以考虑全局类 这样大家可以共享
      

  15.   

    //私有的构造函数 
    private model(string password
    =>
    private model() 
      

  16.   

     System.Security.Principal.WindowsIdentity.GetCurrent().Name
      

  17.   

    静态类有个缺点,他的静态成员的值 可以被任意窗体 变更
    而属性不同,你可以不实现 set,只实现 get,比如,根据用户判断权限。
    我们不可能用静态类来 存用户ID,为安全性着想。哈哈
      

  18.   

    错了,我以为Windows登录用户呢
      

  19.   

    给你点参考代码Login的小窗口:
         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);
                }
            }
        }
      

  20.   

    我是新建了一个这样的类了,而且在A FORM show()B的代码中中插入
     model.GetInstance().userId=textBox1.Text;然后在B FORM也插入了
    public Form1()
            {
                string uid = model.GetInstance().userId;
                InitializeComponent();
                lbText.Text = uid ;
            }
    但lbText.Text 没显示....
      

  21.   


    试试写在窗体的LOAD事件里面
      

  22.   

    lz你知道怎么做了吗?
    如果还没有明白的话,我就按照你的方式给你填几句代码吧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是你的登陆窗口
      

  23.   

    呜...各位大侠,方法我是懂了,其实就是a=b,b=c;最后:a=c;方法我是懂了,但按楼上各大侠们写的,都没有返回显示...不知是什么原因,可能是我太笨了...
      

  24.   

    还没解决?
    在show B 之前,调用 model.GetInstance().userId=textBox1.Text; 
    还是不行贴下你  show B 代码,有那么玄乎么。呵呵
      

  25.   

    这是楼上大哥教我写的类:using System;
    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();
            }              
            
        }}
      

  26.   

    在A中定义一个静态的变量_LogedUserName和属性如下:
    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】
      

  27.   

    天啊...天啊...各位大哥,大侠,大姐...你们把我杀了吧.
    我是那么的愚蠢呀...
    问题解决了....
    原来我将这两句顺序反过来就行了...错误时:
    new webcam.ConsoleForm().ShowDialog();
    this.Hide();
    model.GetInstance().userId = textBox1.Text;正确为:
    model.GetInstance().userId = textBox1.Text; 
    this.Hide();
    new webcam.ConsoleForm().ShowDialog();
    对不起大家...我是一级大笨蛋...对不起浪费了大家这么多时间,不好意思....散分