The InputBox in C #

Source: Internet
Author: User
In the. NET Framework, there is no similar InputBox function in VB, although it can be referenced by VB runtime to use VB's InputBox in the. NET program except VB, but why not write a class to achieve similar function?

The following class implements the functionality of a similar InputBox function:

Using System;

Using System.Windows.Forms;



Namespace Input

{

<summary>

Summary description of the 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 ("XXFarEastFont-Arial", 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 ("XXFarEastFont-Arial", 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] confirm | [ESC] Cancel ";

//

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);



}

Responding to a keyboard

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 ();

}

}

Show 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;

}



}

}

You can call the static method of the class directly Showinputbox, where the title argument is the text of the Text,keyinfo parameter of the dialog box is the label lblinfo (which can display the custom information). The specific call is as follows:

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

{

Microsoft.VisualBasic.Interaction.InputBox ("Type your name", "Input", "" ", 0,0);

You can replace the following string with the text message you want to display. Empty.

String inmsg= Input.InputBox.ShowInputBox ("Input Info", string. Empty);

Check the user's input information

if (Inmsg.trim ()!= string. Empty)

{

MessageBox.Show (INMSG);

}

else if

{

MessageBox.Show ("Enter as String.") Empty ");

}

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.