我正在使用System.Windows.Forms
但奇怪的是沒有能力創建它們。Windows窗體中的提示對話框
我怎樣才能得到像JavaScript提示對話框,沒有JavaScript的東西?
MessageBox很好,但用戶無法輸入輸入。
我正在使用System.Windows.Forms
但奇怪的是沒有能力創建它們。Windows窗體中的提示對話框
我怎樣才能得到像JavaScript提示對話框,沒有JavaScript的東西?
MessageBox很好,但用戶無法輸入輸入。
您需要創建自己的提示對話框。你也許可以爲此創建一個類。
public static class Prompt
{
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}
,把它:
string promptValue = Prompt.ShowDialog("Test", "123");
更新:
添加默認按鈕(回車鍵),並根據意見和another question最初的焦點。
您必須爲此創建一個新窗體,然後顯示它。
創建一個簡單的輸入框形式的一些例子可以在這裏找到:http://www.bing.com/search?setmkt=en-GB&q=C%23+input+box
有沒有這樣的事情在本地Windows窗體。
你必須創建爲或你自己的形式:
使用Microsoft.VisualBasic
參考。
Inputbox是爲VB6兼容性帶入.Net的遺留代碼 - 所以我建議不要這樣做。
+1,指出這是VB6兼容性的遺留代碼。 – harsimranb 2014-02-11 21:50:18
將VisualBasic庫導入到C#程序中並不是一個真正的好主意(不是因爲它們不起作用,而僅僅是爲了兼容性,樣式和升級能力),但您可以調用Microsoft.VisualBasic.Interaction .InputBox()顯示您正在查找的框的種類。
如果你可以創建一個Windows.Forms對象,那最好,但是你說你不能這麼做。
爲什麼這不是一個好主意?什麼是可能的「兼容性」和「升級能力」問題?我會同意「風格上」的說法,大多數C#程序員寧願不使用名爲'VisualBasic'的命名空間中的類,但這只是他們的頭腦。這種感覺沒有現實。它也可以稱爲'Microsoft.MakeMyLifeEasierWithAlreadyImplementedMethods'命名空間。 – 2011-05-19 03:54:37
一般而言,Microsoft.VisualBasic包旨在簡化僅從VB 6升級代碼。微軟一直威脅永久性的日落VB(儘管這可能永遠不會實際發生),所以對這個命名空間的未來支持是不能保證的。此外,.Net的優勢之一應該是可移植性 - 相同的代碼可以在安裝了.Net框架的任何平臺上運行。然而,Microsoft.VisualBasic不能保證在任何其他平臺上可用(包括它的價值,.Net移動,根本不可用)。 – 2011-06-07 15:40:52
**不正確。**這是'Microsoft.VisualBasic.Compatibility'子命名空間,不是全部。許多「核心」功能包含在'Microsoft.VisualBasic'命名空間中;它不會去任何地方。微軟已經威脅要「日落」VB 6,而不是VB.NET。他們一再承諾*不會去任何地方。有些人似乎還沒有想出區別... – 2011-06-07 15:41:53
添加參考Microsoft.VisualBasic
並使用該到你的C#代碼:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt",
"Title",
"Default",
0,
0);
這就是說'Interaction'不存在於命名空間'Microsoft.VisualBasic'中 – 2017-02-21 01:44:24
要使用此方法,您需要添加一個就像他說的那樣,引用了Microsoft.VisualBasic。 要做到這一點,您必須右鍵單擊「項目瀏覽器」窗口中的引用,然後單擊添加引用,然後從該列表中選中VisualBasic。 – kisslory 2018-02-06 21:48:43
這比自定義分類解決方案稍好,因爲它支持高分辨率的屏幕 – 2018-02-07 18:17:48
巴斯Brekelmans的答案是非常優雅,在它的簡單性。但是,我發現對於實際應用,需要多一點,例如:
這裏的類處理這些限制: http://www.codeproject.com/Articles/31315/Getting-User-Input-With-Dialogs-Part-1
我剛剛下載源和複製InputBox.cs到我的項目。
驚訝沒有什麼更好的,雖然...我唯一真正的抱怨是它標題文本不支持換行符,因爲它使用標籤控件。
其他做法的方法: 假設您有一個TextBox輸入類型, 創建一個窗體,並將該文本框的值作爲公共屬性。
public partial class TextPrompt : Form
{
public string Value
{
get { return tbText.Text.Trim(); }
}
public TextPrompt(string promptInstructions)
{
InitializeComponent();
lblPromptText.Text = promptInstructions;
}
private void BtnSubmitText_Click(object sender, EventArgs e)
{
Close();
}
private void TextPrompt_Load(object sender, EventArgs e)
{
CenterToParent();
}
}
在主要形式,這將是代碼:
var t = new TextPrompt(this, "Type the name of the settings file:");
t.ShowDialog()
;
這樣,代碼看起來更清潔:
基於巴斯Brekelmans以上的工作,我也創建了兩個推導 - >「輸入」對話框,允許你從用戶接收兩個文本值和一個布爾(文本框和CheckBox):
public static class PromptForTextAndBoolean
{
public static string ShowDialog(string caption, string text, string boolStr)
{
Form prompt = new Form();
prompt.Width = 280;
prompt.Height = 160;
prompt.Text = caption;
Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
CheckBox ckbx = new CheckBox() { Left = 16, Top = 60, Width = 240, Text = boolStr };
Button confirmation = new Button() { Text = "Okay!", Left = 16, Width = 80, Top = 88, TabIndex = 1, TabStop = true };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.Controls.Add(ckbx);
prompt.Controls.Add(confirmation);
prompt.AcceptButton = confirmation;
prompt.StartPosition = FormStartPosition.CenterScreen;
prompt.ShowDialog();
return string.Format("{0};{1}", textBox.Text, ckbx.Checked.ToString());
}
}
...並與選擇的多個選項之一的(文本框和組合框)沿文:
public static class PromptForTextAndSelection
{
public static string ShowDialog(string caption, string text, string selStr)
{
Form prompt = new Form();
prompt.Width = 280;
prompt.Height = 160;
prompt.Text = caption;
Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
Label selLabel = new Label() { Left = 16, Top = 66, Width = 88, Text = selStr };
ComboBox cmbx = new ComboBox() { Left = 112, Top = 64, Width = 144 };
cmbx.Items.Add("Dark Grey");
cmbx.Items.Add("Orange");
cmbx.Items.Add("None");
Button confirmation = new Button() { Text = "In Ordnung!", Left = 16, Width = 80, Top = 88, TabIndex = 1, TabStop = true };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.Controls.Add(selLabel);
prompt.Controls.Add(cmbx);
prompt.Controls.Add(confirmation);
prompt.AcceptButton = confirmation;
prompt.StartPosition = FormStartPosition.CenterScreen;
prompt.ShowDialog();
return string.Format("{0};{1}", textBox.Text, cmbx.SelectedItem.ToString());
}
}
都需要相同usings:
using System;
using System.Windows.Forms;
打電話給他們,像這樣:
打電話給他們,像這樣:
PromptForTextAndBoolean.ShowDialog("Jazz", "What text should accompany the checkbox?", "Allow Scat Singing");
PromptForTextAndSelection.ShowDialog("Rock", "What should the name of the band be?", "Beret color to wear");
Microsoft.VisualBasic.Interaction.InputBox有幾個缺點。 如果用戶點擊確定或取消,如何管理?你不能。
你應該寫自己的班級或使用類似OOKII Dialogs'InputControl。 OOKII很棒。
下面是VB.NET
Public Function ShowtheDialog(caption As String, text As String, selStr As String) As String
Dim prompt As New Form()
prompt.Width = 280
prompt.Height = 160
prompt.Text = caption
Dim textLabel As New Label() With { _
.Left = 16, _
.Top = 20, _
.Width = 240, _
.Text = text _
}
Dim textBox As New TextBox() With { _
.Left = 16, _
.Top = 40, _
.Width = 240, _
.TabIndex = 0, _
.TabStop = True _
}
Dim selLabel As New Label() With { _
.Left = 16, _
.Top = 66, _
.Width = 88, _
.Text = selStr _
}
Dim cmbx As New ComboBox() With { _
.Left = 112, _
.Top = 64, _
.Width = 144 _
}
cmbx.Items.Add("Dark Grey")
cmbx.Items.Add("Orange")
cmbx.Items.Add("None")
cmbx.SelectedIndex = 0
Dim confirmation As New Button() With { _
.Text = "In Ordnung!", _
.Left = 16, _
.Width = 80, _
.Top = 88, _
.TabIndex = 1, _
.TabStop = True _
}
AddHandler confirmation.Click, Sub(sender, e) prompt.Close()
prompt.Controls.Add(textLabel)
prompt.Controls.Add(textBox)
prompt.Controls.Add(selLabel)
prompt.Controls.Add(cmbx)
prompt.Controls.Add(confirmation)
prompt.AcceptButton = confirmation
prompt.StartPosition = FormStartPosition.CenterScreen
prompt.ShowDialog()
Return String.Format("{0};{1}", textBox.Text, cmbx.SelectedItem.ToString())
End Function
一個例子巴斯的回答可以讓你在memorytrouble理論上,因爲ShowDialog的將不設置。 我認爲這是一種更合適的方式。 還提到textLabel可以用更長的文本閱讀。
public class Prompt : IDisposable
{
private Form prompt { get; set; }
public string Result { get; }
public Prompt(string text, string caption)
{
Result = ShowDialog(text, caption);
}
//use a using statement
private string ShowDialog(string text, string caption)
{
prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen,
TopMost = true
};
Label textLabel = new Label() { Left = 50, Top = 20, Text = text, Dock = DockStyle.Top, TextAlign = ContentAlignment.MiddleCenter };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
public void Dispose()
{
prompt.Dispose();
}
}
實現:
using(Prompt prompt = new Prompt("text", "caption")){
string result = prompt.Result;
}
你能後的你想要做什麼的代碼示例? – 2011-03-24 23:55:41
你在尋找什麼樣的輸入,給予更多的細節。 [CommonDialog](http://msdn.microsoft.com/en-us/library/system.windows.forms.commondialog.aspx)看看繼承它的類嗎?他們中的任何一個都適合你嗎? – 2011-03-24 23:55:49
有趣的是,三個人如何要求OP提供更多的細節和代碼示例,但很明顯他的意思是*「就像一個JavaScript提示對話框」*。 – 2013-02-05 04:53:34