InputBox in C #

xiaoxiao2021-03-06  40

In the .NET Framework, there is no similar network in VB, although it can be used with VB's Runtime references to use VB's INPUTBOX in VB, but why not write a similar function to a class implementation?

The following classes implements a similar inputbox function:

Using system;

Using system.windows.forms;

Namespace INPUT

{

///

/// CLSINPUTBOX summary description.

///

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

{

InitializationComponent ();

}

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Private vidinitiRizeComponent ()

{

THIS.TXTDATA = New System.windows.Forms.TextBox ();

THIS.LBLINFO = New System.windows.Forms.Label ();

THIS.SUSPENDLAYOUT ();

//

// txtdata

//

this.txtdata.font = new system.drawing.font ("Song",

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 ("Song",

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

}

/ / Respond to the keyboard

Private void txtdata_keydown (Object Sender, System.Windows.MMS.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;

KeyInfo.trim ()! = string.empty)

InputBox.lblinfo.text = keyInfo;

INPUTBOX.SHOWDIALOG ();

Return INPUTBOX.TXTDATA.TEXT;

}

}

}

Calling the static method ShowinputBox of this class directly, where the title parameter is the text of the dialog box, the KeyInfo parameter is the Text of the tag lblinfo (which can display 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 text information you want to display instead of String.empty.

String INMSG = INPUT.INPUTBOX.SHOWINPUTBOX ("Input Information", String.empty); / / Check the user's input information

IF (Inmsg.trim ()! = String.empty)

{

Messagebox.show (Inmsg);

}

Else IF

{

Messagebox.show ("Enter String.empty");

}

}

转载请注明原文地址:https://www.9cbs.com/read-59162.html

New Post(0)