Csharp/C Sharp by API/System.Windows.Forms/MessageBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

MessageBox.Show

<source lang="csharp">

using System.Windows.Forms;

class TwoForms {

    public static void Main()
    {
         Form form1 = new Form();
         Form form2 = new Form();
  
         form1.Text = "Form passed to Run()";
         form2.Text = "Second form";
         form2.Show();
  
         Application.Run(form1);
  
         MessageBox.Show("Application.Run() has returned control back to Main.","TwoForms");
    }

}

 </source>


MessageBox.Show(String, String);

<source lang="csharp"> using System; using System.Windows.Forms; class MainForm : Form {

   private Label label1;
   private TextBox textBox1;
   private Button button1;
   public MainForm()
   {
        this.label1 = new Label();
        this.textBox1 = new TextBox();
        this.button1 = new Button();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(16, 36);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(128, 16);
        this.label1.TabIndex = 0;
        this.label1.Text = "Please enter your name:"; 
        this.textBox1.Location = new System.Drawing.Point(152, 32);
        this.textBox1.Name = "textBox1";
        this.textBox1.TabIndex = 1;
        this.textBox1.Text = "";
        this.button1.Location = new System.Drawing.Point(109, 80);
        this.button1.Name = "button1";
        this.button1.TabIndex = 2;
        this.button1.Text = "Enter";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.ClientSize = new System.Drawing.Size(292, 126);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        this.Name = "form1";
        this.Text = "Visual C#";
        this.ResumeLayout(false);
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
       System.Console.WriteLine("User entered: " + textBox1.Text);
       MessageBox.Show("Welcome, " + textBox1.Text, "Visual C#");
    }
    [STAThread]
    public static void Main()
    {
       Application.EnableVisualStyles();
       Application.Run(new MainForm());
    }

}

 </source>


MessageBox.Show(String value)

<source lang="csharp"> using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; public class frmLogin : System.Windows.Forms.Form {

   System.Windows.Forms.TextBox txtUser;
   System.Windows.Forms.Button btnOK;
   System.Windows.Forms.Button btnCancel;
   public frmLogin() {
       txtUser = new System.Windows.Forms.TextBox();
       txtUser.Location = new Point(30, 15);
       txtUser.Size = new Size(250, 20);
       txtUser.Text = "";
       txtUser.Name = "txtUser";
       this.Controls.Add(txtUser);
       btnOK = new System.Windows.Forms.Button();
       btnOK.Location = new Point(40,(txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
       btnOK.Text = "OK";
       btnOK.Name = "btnOK";
       this.Controls.Add(btnOK);
       btnCancel = new System.Windows.Forms.Button();
       btnCancel.Location = new Point((this.Size.Width -
                                       btnCancel.Size.Width) - 40,
          (txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
       btnCancel.Text = "Cancel";
       btnCancel.Name = "btnCancel";
       this.Controls.Add(btnCancel);
       this.Size = new Size(this.Size.Width, btnCancel.Location.Y +
                            btnCancel.Size.Height + 60);
       btnCancel.Click += new System.EventHandler(btnCancelHandler);
       btnOK.Click += new System.EventHandler(btnEventHandler);
   }
   private void btnEventHandler(object sender, System.EventArgs e) {
       MessageBox.Show(((Button)sender).Name);
   }
   private void btnCancelHandler(object sender, System.EventArgs e) {
       MessageBox.Show("The second handler");
   }
   [STAThread]
   static void Main() {
       Application.Run(new frmLogin());
   }

}

 </source>