C#里的InputBox
2024-07-21 02:18:00
供稿:网友
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。在.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”);
}
}