什么是VB.net的InputBox的C#版本?
加入一个参考Microsoft.VisualBasic
,InputBox
是在Microsoft.VisualBasic.Interaction
命名空间:
using Microsoft.VisualBasic; string input = Interaction.InputBox("Prompt", "Title", "Default", -1, -1);
把它们加起来:
有没有在C# .
您可以通过添加对Microsoft.VisualBasic的引用来使用Visual Basic中的对话框:
在解决方案资源管理器中,右键单击" 引用"文件夹.
选择添加参考...
在.NET选项卡中(在较新的Visual Studio版本 - 程序集选项卡中) - 选择Microsoft.VisualBasic
单击" 确定"
然后你可以使用前面提到的代码:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", 0, 0);
编写自己的InputBox.
使用别人的.
也就是说,我建议您首先考虑输入框的需要.对话并不总是最好的做事方式,有时它们弊大于利 - 但这取决于具体情况.
动态创建对话框.您可以根据自己的口味定制.
注意除了winform之外没有外部依赖
private static DialogResult ShowInputDialog(ref string input) { System.Drawing.Size size = new System.Drawing.Size(200, 70); Form inputBox = new Form(); inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; inputBox.ClientSize = size; inputBox.Text = "Name"; System.Windows.Forms.TextBox textBox = new TextBox(); textBox.Size = new System.Drawing.Size(size.Width - 10, 23); textBox.Location = new System.Drawing.Point(5, 5); textBox.Text = input; inputBox.Controls.Add(textBox); Button okButton = new Button(); okButton.DialogResult = System.Windows.Forms.DialogResult.OK; okButton.Name = "okButton"; okButton.Size = new System.Drawing.Size(75, 23); okButton.Text = "&OK"; okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39); inputBox.Controls.Add(okButton); Button cancelButton = new Button(); cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; cancelButton.Name = "cancelButton"; cancelButton.Size = new System.Drawing.Size(75, 23); cancelButton.Text = "&Cancel"; cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39); inputBox.Controls.Add(cancelButton); inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton; DialogResult result = inputBox.ShowDialog(); input = textBox.Text; return result; }
用法
string input="hede"; ShowInputDialog(ref input);
没有一个.如果你真的想在C#中使用VB InputBox,你可以.只需添加对Microsoft.VisualBasic.dll的引用,您就可以在那里找到它.
但我建议不要使用它.它是丑陋的,过时的IMO.
您不仅应该将Microsoft.VisualBasic添加到项目的引用列表中,还应该声明'using Microsoft.VisualBasic;' 所以你只需要使用'Interaction.Inputbox("...")'而不是Microsoft.VisualBasic.Interaction.Inputbox
返回用户输入的字符串; 如果它们击中空字符串Cancel:
public static String InputBox(String caption, String prompt, String defaultText) { String localInputText = defaultText; if (InputQuery(caption, prompt, ref localInputText)) { return localInputText; } else { return ""; } }
返回String
作为ref参数,true
如果它们命中OK,或者false
如果它们命中,则返回Cancel:
public static Boolean InputQuery(String caption, String prompt, ref String value) { Form form; form = new Form(); form.AutoScaleMode = AutoScaleMode.Font; form.Font = SystemFonts.IconTitleFont; SizeF dialogUnits; dialogUnits = form.AutoScaleDimensions; form.FormBorderStyle = FormBorderStyle.FixedDialog; form.MinimizeBox = false; form.MaximizeBox = false; form.Text = caption; form.ClientSize = new Size( Toolkit.MulDiv(180, dialogUnits.Width, 4), Toolkit.MulDiv(63, dialogUnits.Height, 8)); form.StartPosition = FormStartPosition.CenterScreen; System.Windows.Forms.Label lblPrompt; lblPrompt = new System.Windows.Forms.Label(); lblPrompt.Parent = form; lblPrompt.AutoSize = true; lblPrompt.Left = Toolkit.MulDiv(8, dialogUnits.Width, 4); lblPrompt.Top = Toolkit.MulDiv(8, dialogUnits.Height, 8); lblPrompt.Text = prompt; System.Windows.Forms.TextBox edInput; edInput = new System.Windows.Forms.TextBox(); edInput.Parent = form; edInput.Left = lblPrompt.Left; edInput.Top = Toolkit.MulDiv(19, dialogUnits.Height, 8); edInput.Width = Toolkit.MulDiv(164, dialogUnits.Width, 4); edInput.Text = value; edInput.SelectAll(); int buttonTop = Toolkit.MulDiv(41, dialogUnits.Height, 8); //Command buttons should be 50x14 dlus Size buttonSize = Toolkit.ScaleSize(new Size(50, 14), dialogUnits.Width / 4, dialogUnits.Height / 8); System.Windows.Forms.Button bbOk = new System.Windows.Forms.Button(); bbOk.Parent = form; bbOk.Text = "OK"; bbOk.DialogResult = DialogResult.OK; form.AcceptButton = bbOk; bbOk.Location = new Point(Toolkit.MulDiv(38, dialogUnits.Width, 4), buttonTop); bbOk.Size = buttonSize; System.Windows.Forms.Button bbCancel = new System.Windows.Forms.Button(); bbCancel.Parent = form; bbCancel.Text = "Cancel"; bbCancel.DialogResult = DialogResult.Cancel; form.CancelButton = bbCancel; bbCancel.Location = new Point(Toolkit.MulDiv(92, dialogUnits.Width, 4), buttonTop); bbCancel.Size = buttonSize; if (form.ShowDialog() == DialogResult.OK) { value = edInput.Text; return true; } else { return false; } } ////// Multiplies two 32-bit values and then divides the 64-bit result by a /// third 32-bit value. The final result is rounded to the nearest integer. /// public static int MulDiv(int nNumber, int nNumerator, int nDenominator) { return (int)Math.Round((float)nNumber * nNumerator / nDenominator); }
注意:任何代码都将发布到公共域中.无需归属.