写在登陆窗口里,就是启动窗口内
主窗口名为formain
下定义一个变量就可以传了
主窗体public string str="";登陆窗体
formian aa=new formain();
formain.str="信息";
aa.Show();

解决方案 »

  1.   

    我刚做完登陆和主界面的程序,个人认为,把main方法放在主界面比较合适,因为如果放在登录窗体的话,登录成功后也不能关闭登录窗体。主界面Main函数如下:
    static void Main() 
    {

    loginForm login=new loginForm();
    login.ShowDialog();
    if (loginForm.LoginSuccess)
    {
    Application.Run(new Function());
    }
    else
    {
    Application.Exit();
    }
    }
    登录成功后,把用户名保存到一个公用类的静态变量里头,也可以在登录窗体成功验证后,取的登录者的所有权限,这样整个系统的各个窗体都能得到登录者的身份了,也利于权限的判断。
      

  2.   

    不是说不能用全局变量。只是说要小心使用而已。这里要把变量声明为public,否则窗体之间就没有办法通信。
    如果有不当之处,请指点
      

  3.   

    LogonFrm  代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace LogonToMainForm
    {
    public class LogonFrm : System.Windows.Forms.Form
    {
    #region 自定义的变量
    private string User;
    private string sPwd;
    #endregion
    #region 自身对象
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox txtUserId;
    private System.Windows.Forms.TextBox txtPwd;
    private System.Windows.Forms.Button btnOk;
    private System.Windows.Forms.Button btnCancel;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    #endregion
    #region 构造函数与折构函数
    public LogonFrm()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #endregion
    #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.txtUserId = new System.Windows.Forms.TextBox();
    this.txtPwd = new System.Windows.Forms.TextBox();
    this.btnOk = new System.Windows.Forms.Button();
    this.btnCancel = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(52, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(42, 14);
    this.label1.TabIndex = 0;
    this.label1.Text = "UserID";
    // 
    // label2
    // 
    this.label2.AutoSize = true;
    this.label2.Location = new System.Drawing.Point(48, 80);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(54, 14);
    this.label2.TabIndex = 1;
    this.label2.Text = "PassWord";
    // 
    // txtUserId
    // 
    this.txtUserId.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.txtUserId.Location = new System.Drawing.Point(108, 36);
    this.txtUserId.Name = "txtUserId";
    this.txtUserId.Size = new System.Drawing.Size(116, 21);
    this.txtUserId.TabIndex = 2;
    this.txtUserId.Text = "";
    // 
    // txtPwd
    // 
    this.txtPwd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.txtPwd.Location = new System.Drawing.Point(108, 76);
    this.txtPwd.Name = "txtPwd";
    this.txtPwd.Size = new System.Drawing.Size(116, 21);
    this.txtPwd.TabIndex = 3;
    this.txtPwd.Text = "";
    // 
    // btnOk
    // 
    this.btnOk.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
    this.btnOk.Location = new System.Drawing.Point(52, 136);
    this.btnOk.Name = "btnOk";
    this.btnOk.TabIndex = 4;
    this.btnOk.Text = "OK";
    this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
    // 
    // btnCancel
    // 
    this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
    this.btnCancel.Location = new System.Drawing.Point(172, 136);
    this.btnCancel.Name = "btnCancel";
    this.btnCancel.TabIndex = 5;
    this.btnCancel.Text = "Cancel";
    this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
    // 
    // LogonFrm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(302, 190);
    this.ControlBox = false;
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.btnCancel,
      this.btnOk,
      this.txtPwd,
      this.txtUserId,
      this.label2,
      this.label1});
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.Name = "LogonFrm";
    this.ResumeLayout(false); }
    #endregion
    #region 应用程序的主入口点
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new LogonFrm());
    }
    #endregion
    #region Event
    private void btnOk_Click(object sender, System.EventArgs e)
    {
    if(this.txtUserId.Text!=""&&this.txtPwd.Text!="")
    {
    this.User=this.txtUserId.Text.Trim();
    this.sPwd=this.txtPwd.Text.Trim();
    MainFrm main=new MainFrm(this.User,this.sPwd);
    this.Visible=false;
    main.Show();
    }
    else
    {
    MessageBox.Show("用户名和密码不能为空!");
    this.txtUserId.Text="";
    this.txtPwd.Text="";
    this.txtUserId.Focus();
    return;
    }
    } private void btnCancel_Click(object sender, System.EventArgs e)
    {
    Application.Exit();
    }
    #endregion
    }
    }
    MainFrm 代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace LogonToMainForm
    {
    public class MainFrm : System.Windows.Forms.Form
    {
    private System.ComponentModel.Container components = null;
    private string myUser;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private string myPwd;
    public MainFrm(string user,string pwd)
    {
    InitializeComponent();
    myUser=user;//将LogonFrm中的变量User传给myUser
    myPwd=pwd;//将LogonFrm中的变量sPwd传给myPwd
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(148, 52);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(144, 52);
    this.label1.TabIndex = 0;
    this.label1.Text = "label1";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(148, 120);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(144, 48);
    this.label2.TabIndex = 1;
    this.label2.Text = "label2";
    // 
    // MainFrm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(504, 313);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.label2,
      this.label1});
    this.Name = "MainFrm";
    this.Text = "MainFrm";
    this.Load += new System.EventHandler(this.MainFrm_Load);
    this.Closed += new System.EventHandler(this.MainFrm_Closed);
    this.ResumeLayout(false); }
    #endregion private void MainFrm_Closed(object sender, System.EventArgs e)
    {
    Application.Exit();
    } private void MainFrm_Load(object sender, System.EventArgs e)
    {
    this.label1.Text="";
    this.label2.Text="";
    this.label1.Text="这是在LogonFrm中输入的用户名:"+this.myUser;
    this.label2.Text="这是在LogonFrm中输入的密码:"+this.myPwd;
    }
    }
    }我觉得没有必要一定要关闭LogonFrm,可以让它隐藏起来.
      

  4.   

    main函数肯定应该放在主窗体。