C#如何彈出輸入框


在C#中,進行windows窗體應用程序編程的時候,經常需要彈出輸入框,輸入密碼,輸入文本之類的。然而,C#中沒有直接彈出輸入框的語句,MessageBox只能顯示一段消息而不能輸入。我們需要調用Microsoft.VisualBasic,使用VB中的inputbox,實現彈出輸入框的功能。

1、菜單欄,選擇【項目】;然后在彈出的菜單中選擇【添加引用】

 

2、彈出“添加引用”的窗口,找到名稱為Microsoft.VisualBasic的組件,選擇它並點擊【確定】

 

3、使用命名空間Microsoft.VisualBasic。添加代碼:using Microsoft.VisualBasic;

using Microsoft.VisualBasic;

4、在窗體中添加一個Button1和textBox1。我們要實現點擊button1,用textBox1顯示輸入的文本的內容。

 

5、

調用VB中的InputBox,輸入一串字符串。給按鈕添加代碼:

string str = Interaction.InputBox("提示信息","標題","文本內容",-1,-1);

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

 6、參考代碼:

 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 }

7、結果顯示:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM