//form2:void Main(){
 Application.Run(new Form2);
}public string un = null;
public string pw = null;public Form2(){
 Form1 f1 = new Form1(this);
 f1.ShowDialog();
}//form1:
private readonly Form2 f2 = null;
public Form1(Form2 f){
  f2 = f;
}private void logincontrol_onClick(...){
 f2.un = this.txtUserName.text;
 f2.pw = this.txtPassword.text;
 this.Close();
}

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;namespace LoginControlApp
    {
    /// <summary>
    /// Summary description for LoginControl.
    /// </summary>
    public class LoginControl : System.Windows.Forms.UserControl
    {
    private System.Windows.Forms.Button login;
    private System.Windows.Forms.Button cancel;
    private System.Windows.Forms.Label nameLabel;
    private System.Windows.Forms.TextBox password;
    private System.Windows.Forms.Label passwordLabel;
    private System.Windows.Forms.TextBox name;
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; public LoginControl()
    {
    // This call is required by the Windows.Forms Form Designer.
    InitializeComponent(); // TODO: Add any initialization after the InitializeComponent call } /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Component Designer generated code
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.login = new System.Windows.Forms.Button();
    this.cancel = new System.Windows.Forms.Button();
    this.nameLabel = new System.Windows.Forms.Label();
    this.name = new System.Windows.Forms.TextBox();
    this.password = new System.Windows.Forms.TextBox();
    this.passwordLabel = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // login
    // 
    this.login.Location = new System.Drawing.Point(168, 112);
    this.login.Name = "login";
    this.login.Size = new System.Drawing.Size(96, 24);
    this.login.TabIndex = 0;
    this.login.Text = "Login";
    this.login.Click += new System.EventHandler(this.login_Click);
    // 
    // cancel
    // 
    this.cancel.Location = new System.Drawing.Point(40, 112);
    this.cancel.Name = "cancel";
    this.cancel.Size = new System.Drawing.Size(96, 24);
    this.cancel.TabIndex = 1;
    this.cancel.Text = "Cancel";
    // 
    // nameLabel
    // 
    this.nameLabel.Location = new System.Drawing.Point(56, 24);
    this.nameLabel.Name = "nameLabel";
    this.nameLabel.Size = new System.Drawing.Size(40, 16);
    this.nameLabel.TabIndex = 2;
    this.nameLabel.Text = "Name:";
    // 
    // name
    // 
    this.name.Location = new System.Drawing.Point(120, 24);
    this.name.Name = "name";
    this.name.Size = new System.Drawing.Size(120, 20);
    this.name.TabIndex = 3;
    this.name.Text = "";
    // 
    // password
    // 
    this.password.Location = new System.Drawing.Point(120, 64);
    this.password.Name = "password";
    this.password.PasswordChar = '*';
    this.password.Size = new System.Drawing.Size(120, 20);
    this.password.TabIndex = 4;
    this.password.Text = "";
    // 
    // passwordLabel
    // 
    this.passwordLabel.Location = new System.Drawing.Point(40, 72);
    this.passwordLabel.Name = "passwordLabel";
    this.passwordLabel.Size = new System.Drawing.Size(64, 16);
    this.passwordLabel.TabIndex = 5;
    this.passwordLabel.Text = "Password:";
    // 
    // LoginControl
    // 
    this.Controls.Add(this.passwordLabel);
    this.Controls.Add(this.password);
    this.Controls.Add(this.name);
    this.Controls.Add(this.nameLabel);
    this.Controls.Add(this.cancel);
    this.Controls.Add(this.login);
    this.Name = "LoginControl";
    this.Size = new System.Drawing.Size(312, 150);
    this.ResumeLayout(false); }
    #endregion private void login_Click(object sender, System.EventArgs e)
    {
    this.OnValidityCheck();
    } public delegate void ValidityCheckEventHandler(object sender,ValidityCheckEventArgs e);
    public class ValidityCheckEventArgs
    {
    public ValidityCheckEventArgs(string name,string password)
    {
    this._name=name;
    this._password=password;
    } private string _name=null;
    private string _password=null;
    private bool _result=false;
    private string _message=null; public string Name
    {
    get{return this._name;}
    }
    public string Password
    {
    get{return this._password;}
    }
    public bool Result
    {
    get{return this._result;}
    set{this._result=value;}
    }
    public string Message
    {
    get{return this._message;}
    set{this._message=value;}
    }
    } public event ValidityCheckEventHandler ValidityCheck=null; protected virtual  void OnValidityCheck()
    {
    if(this.ValidityCheck!=null)
    {
    ValidityCheckEventArgs e=new ValidityCheckEventArgs(this.name.Text,this.password.Text);
    this.ValidityCheck(this,e);
    if(e.Result==true)
    {
    if(this.Parent is Form)
    (this.Parent as Form).Close();
    }
    else
    {
    MessageBox.Show(e.Message);
    }
    }
    }
    }
    }
    //----------------------------------------
    我觉得使用Event比较合适。