C#裡的InputBox

來源:互聯網
上載者:User

在.NET Framework中並沒有類似VB中的InputBox函數,雖然可以藉助對VB 的Runtime引用,以在VB除外的.NET程式中使用VB的InputBox,但是為什麼不自己寫一個類實作類別似的功能呢?

下面一個類實現了類似的InputBox函數的功能:

using System;

using System.Windows.Forms;

 

namespace Input

{

     /// <summary>

     /// clsInputBox 的摘要說明。

     /// </summary>

     public class InputBox : System.Windows.Forms.Form

     {

         private System.Windows.Forms.TextBox txtData;

         private System.Windows.Forms.Label lblInfo;

         private System.ComponentModel.Container components = null;

 

         private InputBox()

         {

              InitializeComponent();

         }

 

         protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if(components != null)

                   {

                       components.Dispose();

                   }

              }

              base.Dispose( disposing );

         }

 

         private void InitializeComponent()

         {

              this.txtData = new System.Windows.Forms.TextBox();

              this.lblInfo = new System.Windows.Forms.Label();

              this.SuspendLayout();

              //

              // txtData

              //

              this.txtData.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

              this.txtData.Location = new System.Drawing.Point(19, 8);

              this.txtData.Name = "txtData";

              this.txtData.Size = new System.Drawing.Size(317, 23);

              this.txtData.TabIndex = 0;

              this.txtData.Text = "";

              this.txtData.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtData_KeyDown);

              //

              // lblInfo

              //

              this.lblInfo.BackColor = System.Drawing.SystemColors.Info;

              this.lblInfo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

              this.lblInfo.FlatStyle = System.Windows.Forms.FlatStyle.System;

              this.lblInfo.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

              this.lblInfo.ForeColor = System.Drawing.Color.Gray;

              this.lblInfo.Location = new System.Drawing.Point(19, 32);

              this.lblInfo.Name = "lblInfo";

              this.lblInfo.Size = new System.Drawing.Size(317, 16);

              this.lblInfo.TabIndex = 1;

              this.lblInfo.Text = "[Enter]確認 | [Esc]取消";

              //

              // InputBox

              //

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(350, 48);

              this.ControlBox = false;

              this.Controls.Add(this.lblInfo);

              this.Controls.Add(this.txtData);

              this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

              this.Name = "InputBox";

              this.Text = "InputBox";

              this.ResumeLayout(false);

 

         }

         //對鍵盤進行響應

         private void txtData_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

         {

              if(e.KeyCode == Keys.Enter)

              {

                   this.Close();

              }

              else if (e.KeyCode == Keys.Escape )

              {

                   txtData.Text = string.Empty ;

                   this.Close();

              }

         }

         //顯示InputBox

         public static string ShowInputBox(string Title,string keyInfo)

         {

              InputBox inputbox = new InputBox();

              inputbox.Text =Title;

              if (keyInfo.Trim() != string.Empty )

                   inputbox.lblInfo.Text =keyInfo;

              inputbox.ShowDialog();

              return inputbox.txtData.Text;

         }

        

     }

}

直接調用該類的靜態方法ShowInputBox就可以,其中Title參數是對話方塊的Text,keyInfo參數是標籤lblInfo(可以顯示自訂的資訊)的Text。具體調用如下:

private void button_Click(object sender, System.EventArgs e)

         {

              //Microsoft.VisualBasic.Interaction.InputBox(  "type  your  name  ",  "input  ","",0,0);

              //可以將你要顯示的文本資訊代替下面的string.Empty。

              string inMsg= Input.InputBox.ShowInputBox("輸入資訊",string.Empty );

              //對使用者的輸入資訊進行檢查

              if (inMsg.Trim() != string.Empty )

              {

                   MessageBox.Show(inMsg);

              }

              else if

              {

                   MessageBox.Show(“輸入為string.Empty”);

               }

         }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.