VB.net 的 InputDialog 的 C# 版本是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the C# version of VB.net's InputDialog?
提问by wusher
What is the C# version of VB.net's InputBox?
VB.net 的 InputBox 的 C# 版本是什么?
采纳答案by Ozgur Ozcitak
Add a reference to Microsoft.VisualBasic
, InputBox
is in the Microsoft.VisualBasic.Interaction
namespace:
在命名空间中添加对Microsoft.VisualBasic
,的引用:InputBox
Microsoft.VisualBasic.Interaction
using Microsoft.VisualBasic;
string input = Interaction.InputBox("Prompt", "Title", "Default", x_coordinate, y_coordinate);
Only the first argument for prompt
is mandatory
只有第一个参数 forprompt
是强制性的
回答by MADMap
There is no such thing: I recommend to write it for yourself and use it whenever you need.
没有这样的东西:我建议自己编写它,并在需要时使用它。
回答by Joel Coehoorn
You mean InputBox? Just look in the Microsoft.VisualBasic namespace.
你是说输入框?只需查看 Microsoft.VisualBasic 命名空间。
C# and VB.Net share a common library. If one language can use it, so can the other.
C# 和 VB.Net 共享一个公共库。如果一种语言可以使用它,那么另一种语言也可以。
回答by Ryan Farley
There isn't one. If you really wanted to use the VB InputBox in C# you can. Just add reference to Microsoft.VisualBasic.dll and you'll find it there.
没有一个。如果你真的想在 C# 中使用 VB InputBox,你可以。只需添加对 Microsoft.VisualBasic.dll 的引用,您就会在那里找到它。
But I would suggest to notuse it. It is ugly and outdated IMO.
但我建议不要使用它。这是丑陋和过时的 IMO。
回答by chakrit
Add reference to Microsoft.VisualBasic
and use this function:
添加Microsoft.VisualBasic
对此函数的引用并使用:
string response = Microsoft.VisualBasic.Interaction.InputBox("What's 1+1?", "Title", "2", 0, 0);
The last 2 number is an X/Y position to display the input dialog.
最后 2 个数字是显示输入对话框的 X/Y 位置。
回答by Tomas Sedovic
To sum it up:
把它们加起来:
- There is none in C#.
You can use the dialog from Visual Basic by adding a reference to Microsoft.VisualBasic:
- In Solution Explorerright-click on the Referencesfolder.
- Select Add Reference...
- In the .NETtab (in newer Visual Studio verions - Assemblytab) - select Microsoft.VisualBasic
- Click on OK
- 有没有在C# 。
您可以通过添加对 Microsoft.VisualBasic 的引用来使用 Visual Basic 中的对话框:
- 在解决方案资源管理器中,右键单击References文件夹。
- 选择添加引用...
- 在.NET选项卡中(在较新的 Visual Studio 版本 -程序集选项卡中) - 选择Microsoft.VisualBasic
- 点击确定
Then you can use the previously mentioned code:
然后你可以使用前面提到的代码:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", 0, 0);
- Write your own InputBox.
- Use someone else's.
- 编写自己的输入框。
- 用别人的。
That said, I suggest that you consider the need of an input box in the first place. Dialogs are not always the best way to do things and sometimes they do more harm than good - but that depends on the particular situation.
也就是说,我建议您首先考虑输入框的需要。对话并不总是最好的做事方式,有时它们弊大于利 - 但这取决于特定情况。
回答by AdamL
Not only should you add Microsoft.VisualBasic to your reference list for the project, but also you should declare 'using Microsoft.VisualBasic;' so you just have to use 'Interaction.Inputbox("...")' instead of Microsoft.VisualBasic.Interaction.Inputbox
您不仅应该将 Microsoft.VisualBasic 添加到项目的参考列表中,还应该声明“使用 Microsoft.VisualBasic;” 所以你只需要使用 'Interaction.Inputbox("...")' 而不是 Microsoft.VisualBasic.Interaction.Inputbox
回答by Ian Boyd
Returns the string the user entered; empty string if they hit Cancel:
返回用户输入的字符串;如果它们命中,则为空字符串Cancel:
public static String InputBox(String caption, String prompt, String defaultText)
{
String localInputText = defaultText;
if (InputQuery(caption, prompt, ref localInputText))
{
return localInputText;
}
else
{
return "";
}
}
Returns the String
as a refparameter, returning true
if they hit OK, or false
if they hit Cancel:
将 返回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;
}
}
/// <summary>
/// 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.
/// </summary>
public static int MulDiv(int nNumber, int nNumerator, int nDenominator)
{
return (int)Math.Round((float)nNumber * nNumerator / nDenominator);
}
Note: Any code is released into the public domain. No attribution required.
注意:任何代码都发布到公共领域。不需要归属。
回答by Gorkem
Dynamic creation of a dialog box. You can customize to your taste.
动态创建对话框。您可以根据自己的口味进行定制。
Note there is no external dependency here except winform
注意这里除了 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;
}
usage
用法
string input="hede";
ShowInputDialog(ref input);
回答by Stefan Steiger
Without adding a reference to Microsoft.VisualBasic:
不添加对 Microsoft.VisualBasic 的引用:
// "dynamic" requires reference to Microsoft.CSharp
Type tScriptControl = Type.GetTypeFromProgID("ScriptControl");
dynamic oSC = Activator.CreateInstance(tScriptControl);
oSC.Language = "VBScript";
string sFunc = @"Function InBox(prompt, title, default)
InBox = InputBox(prompt, title, default)
End Function
";
oSC.AddCode(sFunc);
dynamic Ret = oSC.Run("InBox", "メッセージ", "タイトル", "初期値");
See these for further information:
ScriptControl
MsgBox in JScript
Input and MsgBox in JScript
有关更多信息,请参阅以下内容:JScript Input 中的
ScriptControl
MsgBox 和 JScript 中的
MsgBox
.NET 2.0:
.NET 2.0:
string sFunc = @"Function InBox(prompt, title, default)
InBox = InputBox(prompt, title, default)
End Function
";
Type tScriptControl = Type.GetTypeFromProgID("ScriptControl");
object oSC = Activator.CreateInstance(tScriptControl);
// https://github.com/mono/mono/blob/master/mcs/class/corlib/System/MonoType.cs
// System.Reflection.PropertyInfo pi = tScriptControl.GetProperty("Language", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.CreateInstance| System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.IgnoreCase);
// pi.SetValue(oSC, "VBScript", null);
tScriptControl.InvokeMember("Language", System.Reflection.BindingFlags.SetProperty, null, oSC, new object[] { "VBScript" });
tScriptControl.InvokeMember("AddCode", System.Reflection.BindingFlags.InvokeMethod, null, oSC, new object[] { sFunc });
object ret = tScriptControl.InvokeMember("Run", System.Reflection.BindingFlags.InvokeMethod, null, oSC, new object[] { "InBox", "メッセージ", "タイトル", "初期値" });
Console.WriteLine(ret);