自定义的InputBox函数(C#)

自定义的InputBox函数(C#)

在.NET Framework中并没有类似VB中的InputBox函数,虽然可以借助对VB 的Runtime引用,以在VB除外的.NET 程序中使用VB的InputBox,但是为什么不自己写一个类实现类似的 功能呢?
在使用时直接添加 调用 就OK了
下面一个类实现了类似的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”);

               }

         }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: EasyX 是一个 C++ 编程语言的图形库,它可以用来绘制图形界面。inputbox 函数是 EasyX 提供的一个用于创建输入框的函数。 使用方法如下: ```C++ char *inputbox(const char *title, const char *prompt, const char *default_text); ``` - title:输入框的标题; - prompt:输入框中的提示文本; - default_text:输入框中的默认文本。 该函数返回用户在输入框中输入的文本。 示例代码如下: ```C++ #include <graphics.h> #include <easyx.h> int main() { initgraph(640, 480); char *name = inputbox("Title", "Please enter your name:", "John Smith"); outtextxy(100, 100, name); getch(); closegraph(); return 0; } ``` 这段代码将会弹出一个输入框,标题为 "Title",提示文本为 "Please enter your name:",默认文本为 "John Smith"。在用户输入后,在屏幕上显示用户输入的文本。 ### 回答2: EasyX作为一款简单易用的图形库,提供了丰富的绘图、文本、输入输出等方便开发者快速开发可视化应用程序的函数,其中包括inputbox函数,下面详细介绍inputbox的使用步骤。 1. 引用头文件 在使用inputbox函数之前,需要先引用EasyX图形库的头文件 easyx.h,其代码如下: ```cpp #include <graphics.h> ``` 2. 调用inputbox函数 inputbox函数的原型为: ```cpp void inputbox(TCHAR * strTitle, TCHAR * strMsg, TCHAR * strBuf, int nBufSize); ``` 其分别代表的含义如下: strTitle:输入框的标题; strMsg:输入框的提示信息,即输入框前的文本; strBuf:存储输入框中输入的字符串的缓冲区; nBufSize:存储输入框中输入的字符串的缓冲区的大小。 在调用该函数时,需要提供上述四个参数,代码如下: ```cpp TCHAR strTitle[] = _T("请输入:"); TCHAR strMsg[] = _T("请输入您的姓名:"); TCHAR strBuf[20] = {0}; int nBufSize = 20; inputbox(strTitle, strMsg, strBuf, nBufSize); ``` 3. 显示输出结果 inputbox函数执行完毕后,输入的字符串会存储在strBuf中,我们需要将其显示在屏幕上。可使用outtextxy函数将其输出到指定位置,代码如下: ```cpp outtextxy(100, 100, strBuf); ``` 其中100,100代表输出的位置,可以根据需要进行调整。 通过以上三个步骤,我们就可以很容易地使用inputbox函数获取用户的输入,并将其在窗口上显示出来。需要特别注意的是,inputbox函数只能在窗口程序或控制台应用程序中使用,且仅对Windows平台有效,Linux和Mac OS系统不支持。 ### 回答3: easyx是一个基于C语言的简单图形化界面库,使用方便,适合初学者入门。其中的inputbox函数可以方便地实现弹出输入框,收集用户输入的信息。 inputbox函数的语法如下: int inputbox(TCHAR szCaption[], TCHAR szPrompt[], TCHAR szResult[], int nMaxResult); 其中,参数含义如下: szCaption:输入框的标题 szPrompt:输入框显示的提示信息 szResult:用户输入的结果将被保存在该参数指向的缓存中,该参数实际是一个指向字符数组的指针 nMaxResult:缓存的大小,即可输入的最大字符数 使用inputbox函数的步骤如下: 1. 在文件头部引入easyx头文件 #include <graphics.h> 2. 在程序中调用inputbox函数 int main() { initgraph(640, 480); // 初始化界面 TCHAR szResult[256]; // 定义一个缓存,保存用户输入的信息 inputbox(_T("请输入姓名"), _T("请输入您的姓名:"), szResult, 256); // 调用inputbox函数 closegraph(); // 关闭界面 return 0; } 在这个例子中,弹出了一个标题为“请输入姓名”的输入框,提示信息为“请输入您的姓名:”,用户输入的信息将保存在szResult中,缓存大小为256字节。可以在输入框中输入姓名,按下“确定”键后,输入框关闭。程序结束后,输入的姓名数据将保存在szResult中,可以在程序中使用该变量。 输入框支持英文、中文等各种字符,如果输入的字符数超过了nMaxResult指定的缓存大小,将只保留最前面的nMaxResult个字符。 总之,inputbox函数是easyx库中一个非常实用的函数,可以方便地收集用户输入的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值