Inputbox in C #

Source: Internet
Author: User

In. NET Framework, there is no function similar to the inputbox function in VB. Although you can use the runtime reference of VB to use. Net except for VBProgramBut why not write a class to implement similar functions?

The following class implements functions similar to the inputbox function:

Using system;

Using system. Windows. forms;

 

Namespace Input

{

/// <Summary>

/// Summary of 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.txt DATA = new system. Windows. Forms. Textbox ();

This. lblinfo = new system. Windows. Forms. Label ();

This. suspendlayout ();

//

// Txtdata

//

This.txt data. font = new system. drawing. font ("", 10.5f, system. drawing. fontstyle. regular, system. drawing. graphicsunit. point, (system. byte) (134 )));

This.txt data. Location = new system. Drawing. Point (19, 8 );

This.txt data. Name = "txtdata ";

This.txt data. size = new system. Drawing. Size (317, 23 );

This.txt data. tabindex = 0;

This.txt data. Text = "";

This.txt data. keydown + = new system.windows.forms.keyeventhandler(this.txt data_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] OK | [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.txt data );

This. formborderstyle = system. Windows. Forms. formborderstyle. fixeddialog;

This. Name = "inputbox ";

This. Text = "inputbox ";

This. resumelayout (false );

 

}

// Respond to the 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 ();

}

}

// Display inputbox

Public static string showinputbox (String title, string keyinfo)

{

Inputbox = new inputbox ();

Inputbox. Text = title;

If (keyinfo. Trim ()! = String. Empty)

Inputbox. lblinfo. Text = keyinfo;

Inputbox. showdialog ();

Return inputbox.txt data. text;

}

}

}

You can directly call the static method showinputbox of this class. The title parameter is the text of the dialog box, And the keyinfo parameter is the text of the label lblinfo (custom information can be displayed. 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 );

// Replace the text information you want to display with the string. Empty below.

String inmsg = input. inputbox. showinputbox ("input information", String. Empty );

// Check user input information

If (inmsg. Trim ()! = String. Empty)

{

MessageBox. Show (inmsg );

}

Else if

{

MessageBox. Show ("input is 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.