在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