C# Windows form 並沒有內建Inputbox,我以前的做法是透過另一個Form來當作輸入視窗。
後來找到兩個不錯的方法,提供參考。
<方法一>使用Microsoft.VisualBasic.dll
01.方案上案右鍵→加入→參考
02.找到Microsoft.VisualBasic →勾選→確定
03.加入 using Microsoft.VisualBasic;
04.在Button加入底下程式
string input_str;
input_str = Interaction.InputBox("請輸入數值", "提示", "");
if (string.IsNullOrEmpty(input_str))
{
return;
}
else
{
MessageBox.Show(input_str,"資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
05.結果:
<方法二>使用System.Windows.Forms.dll
和 System.Drawing.dll
。
01.同方法一,加入兩個參考。
02.加入using
using System;
using System.Windows.Forms;
using System.Drawing;
03.測試底下程式碼
加入底下的方法
private static DialogResult ShowInputDialogBox(ref string input, string prompt,
string title = "Title", int width = 300, int height = 200)
{
// This function creates the custom input dialog box by individually creating the different
// window elements and adding them to the dialog box
// Specify the size of the window using the parameters passed
Size size = new Size(width, height);
// Create a new form using a System.Windows Form
Form inputBox = new Form();
inputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
// Set the window title using the parameter passed
inputBox.Text = title;
// Create a new label to hold the prompt
Label label = new Label();
label.Text = prompt;
label.Location = new Point(5, 5);
label.Width = size.Width - 10;
inputBox.Controls.Add(label);
// Create a textbox to accept the user's input
TextBox textBox = new TextBox();
textBox.Size = new Size(size.Width - 10, 23);
textBox.Location = new Point(5, label.Location.Y + 20);
textBox.Text = input;
inputBox.Controls.Add(textBox);
// Create an OK Button
Button okButton = new Button();
okButton.DialogResult = DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new Size(75, 23);
okButton.Text = "&OK";
okButton.Location = new Point(size.Width - 80 - 80, size.Height - 30);
inputBox.Controls.Add(okButton);
// Create a Cancel Button
Button cancelButton = new Button();
cancelButton.DialogResult = DialogResult.Cancel;
cancelButton.Name = "cancelButton";
cancelButton.Size = new Size(75, 23);
cancelButton.Text = "&Cancel";
cancelButton.Location = new Point(size.Width - 80, size.Height - 30);
inputBox.Controls.Add(cancelButton);
// Set the input box's buttons to the created OK and Cancel Buttons respectively so the window
// appropriately behaves with the button clicks
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
// Show the window dialog box
DialogResult result = inputBox.ShowDialog();
input = textBox.Text;
// After input has been submitted, return the input value
return result;
}
04.呼叫
string test = ""; // 回傳值
ShowInputDialogBox(ref test, "你今天學會了嗎?", "標題", 300, 200);
// Print the input provided by the user
Console.WriteLine(test);
05.結果
<小結>方法一簡單快速,方法二可以客製調整介面。
你今天學會了嗎?
參考文章:
https://www.twblogs.net/a/5c719306bd9eee68440f7184
https://blog.csdn.net/Never_Satisfied/article/details/129666763
https://www.delftstack.com/zh-tw/howto/csharp/create-an-input-dialog-box-in-csharp