C#如何弹出输入框

在C#中,进行windows窗体应用程序编程的时候,常常须要弹出输入框,输入密码,输入文本之类的。然而,C#中没有直接弹出输入框的语句,MessageBox只能显示一段消息而不能输入。咱们须要调用Microsoft.VisualBasic,使用VB中的inputbox,实现弹出输入框的功能。编程

一、菜单栏,选择【项目】;而后在弹出的菜单中选择【添加引用】windows

 

二、弹出“添加引用”的窗口,找到名称为Microsoft.VisualBasic的组件,选择它并点击【肯定】spa

 

三、使用命名空间Microsoft.VisualBasic。添加代码:using Microsoft.VisualBasic;3d

using Microsoft.VisualBasic;

四、在窗体中添加一个Button1和textBox1。咱们要实现点击button1,用textBox1显示输入的文本的内容。code

 

五、orm

调用VB中的InputBox,输入一串字符串。给按钮添加代码:blog

string str = Interaction.InputBox("提示信息","标题","文本内容",-1,-1);

Interaction.InputBox的格式:string  Interaction .InputBox(string Prompt,string title,string  Defaultresponce,int Xpos,int Ypose)字符串

 六、参考代码:input

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using Microsoft.VisualBasic;
10 
11 namespace WindowsFormsApplication1
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19 
20         private void Form1_Load(object sender, EventArgs e)
21         {
22 
23         }
24 
25         private void button1_Click(object sender, EventArgs e)
26         {
27             string str = Interaction.InputBox("提示信息","标题","文本内容",-1,-1);
28             
29             textBox1.Text = str;
30         }
31     }
32 }

七、结果显示:string

相关文章
相关标签/搜索