What is the C# version of VB.net's InputDialog?
What is the C# version of VB.net's InputBox?
c# vb.net
add a comment |
What is the C# version of VB.net's InputBox?
c# vb.net
add a comment |
What is the C# version of VB.net's InputBox?
c# vb.net
What is the C# version of VB.net's InputBox?
c# vb.net
c# vb.net
edited Dec 9 '18 at 19:53
Community♦
11
11
asked Sep 18 '08 at 21:20
wusherwusher
6,050216093
6,050216093
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
Add a reference to Microsoft.VisualBasic
, InputBox
is in the Microsoft.VisualBasic.Interaction
namespace:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", -1, -1);
8
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
3
@hims056InputBox
does not natively support masked input. You will need to roll your own input form.
– Ozgur Ozcitak
Apr 8 '13 at 10:43
4
Just importusing Microsoft.VisualBasic
so you just writeInteraction.InputBox()
– stackptr
Nov 16 '13 at 18:10
3
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
|
show 2 more comments
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 Explorer right-click on the References folder.
- Select Add Reference...
- In the .NET tab (in newer Visual Studio verions - Assembly tab) - select Microsoft.VisualBasic
- Click on OK
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.
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
3
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
|
show 2 more comments
Dynamic creation of a dialog box. You can customize to your taste.
Note there is no external dependency here except 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);
1
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
2
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
1
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
8
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
2
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
|
show 3 more comments
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.
But I would suggest to not use it. It is ugly and outdated IMO.
17
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
3
Can't identify thecancel
fromempty input string
actually is a bug IMO.
– Joe.wang
Oct 11 '14 at 2:33
add a comment |
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
2
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
add a comment |
Returns the string the user entered; empty string if they hit 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 ref parameter, returning true
if they hit OK, or false
if they hit 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.
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
What isToolkit
?
– Markus L
Feb 21 '18 at 11:59
1
@markusL Toolkit was my class that holds an implementation ofMulDiv
. You can see Peter's comment for an example implementation ofMulDiv
.
– Ian Boyd
Feb 21 '18 at 15:41
add a comment |
Add reference to Microsoft.VisualBasic
and use this function:
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.
add a comment |
You mean InputBox? Just look in the Microsoft.VisualBasic namespace.
C# and VB.Net share a common library. If one language can use it, so can the other.
add a comment |
Without adding a reference to 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
.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);
add a comment |
I was able to achieve this by coding my own. I don't like extending into and relying on large library's for something rudimental.
Form and Designer:
public partial class InputBox
: Form
{
public String Input
{
get { return textInput.Text; }
}
public InputBox()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void InputBox_Load(object sender, EventArgs e)
{
this.ActiveControl = textInput;
}
public static DialogResult Show(String title, String message, String inputTitle, out String inputValue)
{
InputBox inputBox = null;
DialogResult results = DialogResult.None;
using (inputBox = new InputBox() { Text = title })
{
inputBox.labelMessage.Text = message;
inputBox.splitContainer2.SplitterDistance = inputBox.labelMessage.Width;
inputBox.labelInput.Text = inputTitle;
inputBox.splitContainer1.SplitterDistance = inputBox.labelInput.Width;
inputBox.Size = new Size(
inputBox.Width,
8 + inputBox.labelMessage.Height + inputBox.splitContainer2.SplitterWidth + inputBox.splitContainer1.Height + 8 + inputBox.button2.Height + 12 + (50));
results = inputBox.ShowDialog();
inputValue = inputBox.Input;
}
return results;
}
void labelInput_TextChanged(object sender, System.EventArgs e)
{
}
}
partial class InputBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.labelInput = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Location = new System.Drawing.Point(3, 0);
this.labelMessage.MaximumSize = new System.Drawing.Size(379, 0);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(50, 13);
this.labelMessage.TabIndex = 99;
this.labelMessage.Text = "Message";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(316, 126);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Cancel";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(235, 126);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "OK";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelInput
//
this.labelInput.AutoSize = true;
this.labelInput.Location = new System.Drawing.Point(3, 6);
this.labelInput.Name = "labelInput";
this.labelInput.Size = new System.Drawing.Size(31, 13);
this.labelInput.TabIndex = 99;
this.labelInput.Text = "Input";
this.labelInput.TextChanged += new System.EventHandler(this.labelInput_TextChanged);
//
// textInput
//
this.textInput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textInput.Location = new System.Drawing.Point(3, 3);
this.textInput.Name = "textInput";
this.textInput.Size = new System.Drawing.Size(243, 20);
this.textInput.TabIndex = 1;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.labelInput);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textInput);
this.splitContainer1.Size = new System.Drawing.Size(379, 50);
this.splitContainer1.SplitterDistance = 126;
this.splitContainer1.TabIndex = 99;
//
// splitContainer2
//
this.splitContainer2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitContainer2.IsSplitterFixed = true;
this.splitContainer2.Location = new System.Drawing.Point(12, 12);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.labelMessage);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.splitContainer1);
this.splitContainer2.Size = new System.Drawing.Size(379, 108);
this.splitContainer2.SplitterDistance = 54;
this.splitContainer2.TabIndex = 99;
//
// InputBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 161);
this.Controls.Add(this.splitContainer2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label labelMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label labelInput;
private System.Windows.Forms.TextBox textInput;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
Usage:
String output = "";
result = System.Windows.Forms.DialogResult.None;
result = InputBox.Show(
"Input Required",
"Please enter the value (if available) below.",
"Value",
out output);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}
Note this exhibits a bit of auto sizing to keep it pretty based on how much text you ask it display. I also know it's lacking the bells and whistles but it's a solid step forward for those facing this same dilemma.
add a comment |
There is no such thing: I recommend to write it for yourself and use it whenever you need.
add a comment |
protected by Community♦ May 12 '17 at 9:39
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add a reference to Microsoft.VisualBasic
, InputBox
is in the Microsoft.VisualBasic.Interaction
namespace:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", -1, -1);
8
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
3
@hims056InputBox
does not natively support masked input. You will need to roll your own input form.
– Ozgur Ozcitak
Apr 8 '13 at 10:43
4
Just importusing Microsoft.VisualBasic
so you just writeInteraction.InputBox()
– stackptr
Nov 16 '13 at 18:10
3
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
|
show 2 more comments
Add a reference to Microsoft.VisualBasic
, InputBox
is in the Microsoft.VisualBasic.Interaction
namespace:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", -1, -1);
8
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
3
@hims056InputBox
does not natively support masked input. You will need to roll your own input form.
– Ozgur Ozcitak
Apr 8 '13 at 10:43
4
Just importusing Microsoft.VisualBasic
so you just writeInteraction.InputBox()
– stackptr
Nov 16 '13 at 18:10
3
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
|
show 2 more comments
Add a reference to Microsoft.VisualBasic
, InputBox
is in the Microsoft.VisualBasic.Interaction
namespace:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", -1, -1);
Add a reference to Microsoft.VisualBasic
, InputBox
is in the Microsoft.VisualBasic.Interaction
namespace:
string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title", "Default", -1, -1);
edited Jul 31 '13 at 16:05
John
747815
747815
answered Sep 18 '08 at 21:25
Ozgur OzcitakOzgur Ozcitak
7,19873754
7,19873754
8
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
3
@hims056InputBox
does not natively support masked input. You will need to roll your own input form.
– Ozgur Ozcitak
Apr 8 '13 at 10:43
4
Just importusing Microsoft.VisualBasic
so you just writeInteraction.InputBox()
– stackptr
Nov 16 '13 at 18:10
3
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
|
show 2 more comments
8
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
3
@hims056InputBox
does not natively support masked input. You will need to roll your own input form.
– Ozgur Ozcitak
Apr 8 '13 at 10:43
4
Just importusing Microsoft.VisualBasic
so you just writeInteraction.InputBox()
– stackptr
Nov 16 '13 at 18:10
3
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
8
8
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
Bah... fastest gun in the west haha.... anyway I looked up the actual signature in Object Browser and prompt comes before title, so its "Prompt" first and then "Title".. the last 2 number is X/Y coordinates to display the inputbox
– chakrit
Sep 18 '08 at 21:32
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
What if I want to get password from user?
– hims056
Apr 6 '13 at 7:05
3
3
@hims056
InputBox
does not natively support masked input. You will need to roll your own input form.– Ozgur Ozcitak
Apr 8 '13 at 10:43
@hims056
InputBox
does not natively support masked input. You will need to roll your own input form.– Ozgur Ozcitak
Apr 8 '13 at 10:43
4
4
Just import
using Microsoft.VisualBasic
so you just write Interaction.InputBox()
– stackptr
Nov 16 '13 at 18:10
Just import
using Microsoft.VisualBasic
so you just write Interaction.InputBox()
– stackptr
Nov 16 '13 at 18:10
3
3
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
I have searched for this at least 10 times. Always resulting on this answer. Would upvote again if I could. Thanks!
– C4d
Jul 12 '16 at 16:47
|
show 2 more comments
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 Explorer right-click on the References folder.
- Select Add Reference...
- In the .NET tab (in newer Visual Studio verions - Assembly tab) - select Microsoft.VisualBasic
- Click on OK
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.
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
3
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
|
show 2 more comments
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 Explorer right-click on the References folder.
- Select Add Reference...
- In the .NET tab (in newer Visual Studio verions - Assembly tab) - select Microsoft.VisualBasic
- Click on OK
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.
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
3
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
|
show 2 more comments
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 Explorer right-click on the References folder.
- Select Add Reference...
- In the .NET tab (in newer Visual Studio verions - Assembly tab) - select Microsoft.VisualBasic
- Click on OK
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.
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 Explorer right-click on the References folder.
- Select Add Reference...
- In the .NET tab (in newer Visual Studio verions - Assembly tab) - select Microsoft.VisualBasic
- Click on OK
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.
edited Oct 1 '18 at 17:29
Martin Backasch
99311220
99311220
answered Sep 18 '08 at 22:02
Tomas SedovicTomas Sedovic
12.2k93329
12.2k93329
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
3
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
|
show 2 more comments
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
3
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
You can use the dialog from C# by adding that reference, too.
– Joel Coehoorn
Sep 19 '08 at 13:27
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
Input boxes are a godsend for testing ui...
– Mladen Mihajlovic
Apr 16 '09 at 17:35
3
3
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
Yeah, they are. But it seems to me that in most cases they're bad in the shipping code.
– Tomas Sedovic
Jul 9 '09 at 14:35
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
+1 for the the link Tomas posted. This one is better than the Virtual Basic InputBox.
– Joe.wang
Oct 11 '14 at 2:30
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
Still better than using /subsystem:console... Sometimes you just need very little interaction with the user, and then you can use them, instead of having 90% of your code be for UI.
– Nulano
Jun 23 '15 at 14:37
|
show 2 more comments
Dynamic creation of a dialog box. You can customize to your taste.
Note there is no external dependency here except 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);
1
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
2
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
1
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
8
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
2
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
|
show 3 more comments
Dynamic creation of a dialog box. You can customize to your taste.
Note there is no external dependency here except 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);
1
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
2
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
1
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
8
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
2
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
|
show 3 more comments
Dynamic creation of a dialog box. You can customize to your taste.
Note there is no external dependency here except 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);
Dynamic creation of a dialog box. You can customize to your taste.
Note there is no external dependency here except 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);
edited Feb 17 '14 at 8:48
answered Jul 9 '13 at 11:20
GorkemGorkem
1,159919
1,159919
1
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
2
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
1
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
8
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
2
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
|
show 3 more comments
1
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
2
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
1
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
8
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
2
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
1
1
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
+1, also would be nice if it accepted ESC and ENTER keys
– F.I.V
Nov 26 '13 at 6:04
2
2
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
OK, very easy, I found it: inputBox.AcceptButton = okButton; inputBox.CancelButton = cancelButton;
– F.I.V
Nov 26 '13 at 6:13
1
1
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
Working! Better than VB solution. Thanks beehorf!
– Filipe YaBa Polido
Feb 3 '14 at 0:08
8
8
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
inputBox.StartPosition = FormStartPosition.CenterParent; will center the dialog on the parent window.
– Andrew Cash
Mar 29 '14 at 7:01
2
2
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
+1 for writing your own code and sharing it here! others are too lazy to do so and just want easy free points.
– Kairan
Mar 14 '15 at 2:58
|
show 3 more comments
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.
But I would suggest to not use it. It is ugly and outdated IMO.
17
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
3
Can't identify thecancel
fromempty input string
actually is a bug IMO.
– Joe.wang
Oct 11 '14 at 2:33
add a comment |
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.
But I would suggest to not use it. It is ugly and outdated IMO.
17
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
3
Can't identify thecancel
fromempty input string
actually is a bug IMO.
– Joe.wang
Oct 11 '14 at 2:33
add a comment |
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.
But I would suggest to not use it. It is ugly and outdated IMO.
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.
But I would suggest to not use it. It is ugly and outdated IMO.
answered Sep 18 '08 at 21:23
Ryan FarleyRyan Farley
10.5k44043
10.5k44043
17
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
3
Can't identify thecancel
fromempty input string
actually is a bug IMO.
– Joe.wang
Oct 11 '14 at 2:33
add a comment |
17
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
3
Can't identify thecancel
fromempty input string
actually is a bug IMO.
– Joe.wang
Oct 11 '14 at 2:33
17
17
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
I think you are being too kind. It's far more ugly and outdated than that!
– BlackWasp
May 2 '09 at 17:29
3
3
Can't identify the
cancel
from empty input string
actually is a bug IMO.– Joe.wang
Oct 11 '14 at 2:33
Can't identify the
cancel
from empty input string
actually is a bug IMO.– Joe.wang
Oct 11 '14 at 2:33
add a comment |
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
2
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
add a comment |
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
2
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
add a comment |
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
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
answered Feb 19 '13 at 19:12
user429460user429460
8325
8325
2
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
add a comment |
2
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
2
2
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
If you're only using it once, this adds clutter if the OP decides they don't want the InputBox anymore. Also, this should be a comment.
– BalinKingOfMoria
May 6 '15 at 17:19
add a comment |
Returns the string the user entered; empty string if they hit 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 ref parameter, returning true
if they hit OK, or false
if they hit 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.
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
What isToolkit
?
– Markus L
Feb 21 '18 at 11:59
1
@markusL Toolkit was my class that holds an implementation ofMulDiv
. You can see Peter's comment for an example implementation ofMulDiv
.
– Ian Boyd
Feb 21 '18 at 15:41
add a comment |
Returns the string the user entered; empty string if they hit 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 ref parameter, returning true
if they hit OK, or false
if they hit 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.
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
What isToolkit
?
– Markus L
Feb 21 '18 at 11:59
1
@markusL Toolkit was my class that holds an implementation ofMulDiv
. You can see Peter's comment for an example implementation ofMulDiv
.
– Ian Boyd
Feb 21 '18 at 15:41
add a comment |
Returns the string the user entered; empty string if they hit 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 ref parameter, returning true
if they hit OK, or false
if they hit 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.
Returns the string the user entered; empty string if they hit 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 ref parameter, returning true
if they hit OK, or false
if they hit 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.
edited Feb 21 '18 at 15:43
answered Feb 21 '13 at 22:00
Ian BoydIan Boyd
121k1896871013
121k1896871013
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
What isToolkit
?
– Markus L
Feb 21 '18 at 11:59
1
@markusL Toolkit was my class that holds an implementation ofMulDiv
. You can see Peter's comment for an example implementation ofMulDiv
.
– Ian Boyd
Feb 21 '18 at 15:41
add a comment |
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
What isToolkit
?
– Markus L
Feb 21 '18 at 11:59
1
@markusL Toolkit was my class that holds an implementation ofMulDiv
. You can see Peter's comment for an example implementation ofMulDiv
.
– Ian Boyd
Feb 21 '18 at 15:41
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
public static int MulDiv(int number, int numerator, int denominator) { return (int)(((long)number * numerator + (denominator >> 1)) / denominator); }
– Peter Kalef ' DidiSoft
Sep 21 '16 at 11:27
What is
Toolkit
?– Markus L
Feb 21 '18 at 11:59
What is
Toolkit
?– Markus L
Feb 21 '18 at 11:59
1
1
@markusL Toolkit was my class that holds an implementation of
MulDiv
. You can see Peter's comment for an example implementation of MulDiv
.– Ian Boyd
Feb 21 '18 at 15:41
@markusL Toolkit was my class that holds an implementation of
MulDiv
. You can see Peter's comment for an example implementation of MulDiv
.– Ian Boyd
Feb 21 '18 at 15:41
add a comment |
Add reference to Microsoft.VisualBasic
and use this function:
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.
add a comment |
Add reference to Microsoft.VisualBasic
and use this function:
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.
add a comment |
Add reference to Microsoft.VisualBasic
and use this function:
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.
Add reference to Microsoft.VisualBasic
and use this function:
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.
answered Sep 18 '08 at 21:29
chakritchakrit
45.8k23118153
45.8k23118153
add a comment |
add a comment |
You mean InputBox? Just look in the Microsoft.VisualBasic namespace.
C# and VB.Net share a common library. If one language can use it, so can the other.
add a comment |
You mean InputBox? Just look in the Microsoft.VisualBasic namespace.
C# and VB.Net share a common library. If one language can use it, so can the other.
add a comment |
You mean InputBox? Just look in the Microsoft.VisualBasic namespace.
C# and VB.Net share a common library. If one language can use it, so can the other.
You mean InputBox? Just look in the Microsoft.VisualBasic namespace.
C# and VB.Net share a common library. If one language can use it, so can the other.
answered Sep 18 '08 at 21:22
Joel CoehoornJoel Coehoorn
310k96495729
310k96495729
add a comment |
add a comment |
Without adding a reference to 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
.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);
add a comment |
Without adding a reference to 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
.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);
add a comment |
Without adding a reference to 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
.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);
Without adding a reference to 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
.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);
edited Sep 19 '14 at 14:58
answered Sep 18 '14 at 12:26
Stefan SteigerStefan Steiger
45.1k56269358
45.1k56269358
add a comment |
add a comment |
I was able to achieve this by coding my own. I don't like extending into and relying on large library's for something rudimental.
Form and Designer:
public partial class InputBox
: Form
{
public String Input
{
get { return textInput.Text; }
}
public InputBox()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void InputBox_Load(object sender, EventArgs e)
{
this.ActiveControl = textInput;
}
public static DialogResult Show(String title, String message, String inputTitle, out String inputValue)
{
InputBox inputBox = null;
DialogResult results = DialogResult.None;
using (inputBox = new InputBox() { Text = title })
{
inputBox.labelMessage.Text = message;
inputBox.splitContainer2.SplitterDistance = inputBox.labelMessage.Width;
inputBox.labelInput.Text = inputTitle;
inputBox.splitContainer1.SplitterDistance = inputBox.labelInput.Width;
inputBox.Size = new Size(
inputBox.Width,
8 + inputBox.labelMessage.Height + inputBox.splitContainer2.SplitterWidth + inputBox.splitContainer1.Height + 8 + inputBox.button2.Height + 12 + (50));
results = inputBox.ShowDialog();
inputValue = inputBox.Input;
}
return results;
}
void labelInput_TextChanged(object sender, System.EventArgs e)
{
}
}
partial class InputBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.labelInput = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Location = new System.Drawing.Point(3, 0);
this.labelMessage.MaximumSize = new System.Drawing.Size(379, 0);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(50, 13);
this.labelMessage.TabIndex = 99;
this.labelMessage.Text = "Message";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(316, 126);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Cancel";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(235, 126);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "OK";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelInput
//
this.labelInput.AutoSize = true;
this.labelInput.Location = new System.Drawing.Point(3, 6);
this.labelInput.Name = "labelInput";
this.labelInput.Size = new System.Drawing.Size(31, 13);
this.labelInput.TabIndex = 99;
this.labelInput.Text = "Input";
this.labelInput.TextChanged += new System.EventHandler(this.labelInput_TextChanged);
//
// textInput
//
this.textInput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textInput.Location = new System.Drawing.Point(3, 3);
this.textInput.Name = "textInput";
this.textInput.Size = new System.Drawing.Size(243, 20);
this.textInput.TabIndex = 1;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.labelInput);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textInput);
this.splitContainer1.Size = new System.Drawing.Size(379, 50);
this.splitContainer1.SplitterDistance = 126;
this.splitContainer1.TabIndex = 99;
//
// splitContainer2
//
this.splitContainer2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitContainer2.IsSplitterFixed = true;
this.splitContainer2.Location = new System.Drawing.Point(12, 12);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.labelMessage);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.splitContainer1);
this.splitContainer2.Size = new System.Drawing.Size(379, 108);
this.splitContainer2.SplitterDistance = 54;
this.splitContainer2.TabIndex = 99;
//
// InputBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 161);
this.Controls.Add(this.splitContainer2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label labelMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label labelInput;
private System.Windows.Forms.TextBox textInput;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
Usage:
String output = "";
result = System.Windows.Forms.DialogResult.None;
result = InputBox.Show(
"Input Required",
"Please enter the value (if available) below.",
"Value",
out output);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}
Note this exhibits a bit of auto sizing to keep it pretty based on how much text you ask it display. I also know it's lacking the bells and whistles but it's a solid step forward for those facing this same dilemma.
add a comment |
I was able to achieve this by coding my own. I don't like extending into and relying on large library's for something rudimental.
Form and Designer:
public partial class InputBox
: Form
{
public String Input
{
get { return textInput.Text; }
}
public InputBox()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void InputBox_Load(object sender, EventArgs e)
{
this.ActiveControl = textInput;
}
public static DialogResult Show(String title, String message, String inputTitle, out String inputValue)
{
InputBox inputBox = null;
DialogResult results = DialogResult.None;
using (inputBox = new InputBox() { Text = title })
{
inputBox.labelMessage.Text = message;
inputBox.splitContainer2.SplitterDistance = inputBox.labelMessage.Width;
inputBox.labelInput.Text = inputTitle;
inputBox.splitContainer1.SplitterDistance = inputBox.labelInput.Width;
inputBox.Size = new Size(
inputBox.Width,
8 + inputBox.labelMessage.Height + inputBox.splitContainer2.SplitterWidth + inputBox.splitContainer1.Height + 8 + inputBox.button2.Height + 12 + (50));
results = inputBox.ShowDialog();
inputValue = inputBox.Input;
}
return results;
}
void labelInput_TextChanged(object sender, System.EventArgs e)
{
}
}
partial class InputBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.labelInput = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Location = new System.Drawing.Point(3, 0);
this.labelMessage.MaximumSize = new System.Drawing.Size(379, 0);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(50, 13);
this.labelMessage.TabIndex = 99;
this.labelMessage.Text = "Message";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(316, 126);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Cancel";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(235, 126);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "OK";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelInput
//
this.labelInput.AutoSize = true;
this.labelInput.Location = new System.Drawing.Point(3, 6);
this.labelInput.Name = "labelInput";
this.labelInput.Size = new System.Drawing.Size(31, 13);
this.labelInput.TabIndex = 99;
this.labelInput.Text = "Input";
this.labelInput.TextChanged += new System.EventHandler(this.labelInput_TextChanged);
//
// textInput
//
this.textInput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textInput.Location = new System.Drawing.Point(3, 3);
this.textInput.Name = "textInput";
this.textInput.Size = new System.Drawing.Size(243, 20);
this.textInput.TabIndex = 1;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.labelInput);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textInput);
this.splitContainer1.Size = new System.Drawing.Size(379, 50);
this.splitContainer1.SplitterDistance = 126;
this.splitContainer1.TabIndex = 99;
//
// splitContainer2
//
this.splitContainer2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitContainer2.IsSplitterFixed = true;
this.splitContainer2.Location = new System.Drawing.Point(12, 12);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.labelMessage);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.splitContainer1);
this.splitContainer2.Size = new System.Drawing.Size(379, 108);
this.splitContainer2.SplitterDistance = 54;
this.splitContainer2.TabIndex = 99;
//
// InputBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 161);
this.Controls.Add(this.splitContainer2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label labelMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label labelInput;
private System.Windows.Forms.TextBox textInput;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
Usage:
String output = "";
result = System.Windows.Forms.DialogResult.None;
result = InputBox.Show(
"Input Required",
"Please enter the value (if available) below.",
"Value",
out output);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}
Note this exhibits a bit of auto sizing to keep it pretty based on how much text you ask it display. I also know it's lacking the bells and whistles but it's a solid step forward for those facing this same dilemma.
add a comment |
I was able to achieve this by coding my own. I don't like extending into and relying on large library's for something rudimental.
Form and Designer:
public partial class InputBox
: Form
{
public String Input
{
get { return textInput.Text; }
}
public InputBox()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void InputBox_Load(object sender, EventArgs e)
{
this.ActiveControl = textInput;
}
public static DialogResult Show(String title, String message, String inputTitle, out String inputValue)
{
InputBox inputBox = null;
DialogResult results = DialogResult.None;
using (inputBox = new InputBox() { Text = title })
{
inputBox.labelMessage.Text = message;
inputBox.splitContainer2.SplitterDistance = inputBox.labelMessage.Width;
inputBox.labelInput.Text = inputTitle;
inputBox.splitContainer1.SplitterDistance = inputBox.labelInput.Width;
inputBox.Size = new Size(
inputBox.Width,
8 + inputBox.labelMessage.Height + inputBox.splitContainer2.SplitterWidth + inputBox.splitContainer1.Height + 8 + inputBox.button2.Height + 12 + (50));
results = inputBox.ShowDialog();
inputValue = inputBox.Input;
}
return results;
}
void labelInput_TextChanged(object sender, System.EventArgs e)
{
}
}
partial class InputBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.labelInput = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Location = new System.Drawing.Point(3, 0);
this.labelMessage.MaximumSize = new System.Drawing.Size(379, 0);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(50, 13);
this.labelMessage.TabIndex = 99;
this.labelMessage.Text = "Message";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(316, 126);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Cancel";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(235, 126);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "OK";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelInput
//
this.labelInput.AutoSize = true;
this.labelInput.Location = new System.Drawing.Point(3, 6);
this.labelInput.Name = "labelInput";
this.labelInput.Size = new System.Drawing.Size(31, 13);
this.labelInput.TabIndex = 99;
this.labelInput.Text = "Input";
this.labelInput.TextChanged += new System.EventHandler(this.labelInput_TextChanged);
//
// textInput
//
this.textInput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textInput.Location = new System.Drawing.Point(3, 3);
this.textInput.Name = "textInput";
this.textInput.Size = new System.Drawing.Size(243, 20);
this.textInput.TabIndex = 1;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.labelInput);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textInput);
this.splitContainer1.Size = new System.Drawing.Size(379, 50);
this.splitContainer1.SplitterDistance = 126;
this.splitContainer1.TabIndex = 99;
//
// splitContainer2
//
this.splitContainer2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitContainer2.IsSplitterFixed = true;
this.splitContainer2.Location = new System.Drawing.Point(12, 12);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.labelMessage);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.splitContainer1);
this.splitContainer2.Size = new System.Drawing.Size(379, 108);
this.splitContainer2.SplitterDistance = 54;
this.splitContainer2.TabIndex = 99;
//
// InputBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 161);
this.Controls.Add(this.splitContainer2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label labelMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label labelInput;
private System.Windows.Forms.TextBox textInput;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
Usage:
String output = "";
result = System.Windows.Forms.DialogResult.None;
result = InputBox.Show(
"Input Required",
"Please enter the value (if available) below.",
"Value",
out output);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}
Note this exhibits a bit of auto sizing to keep it pretty based on how much text you ask it display. I also know it's lacking the bells and whistles but it's a solid step forward for those facing this same dilemma.
I was able to achieve this by coding my own. I don't like extending into and relying on large library's for something rudimental.
Form and Designer:
public partial class InputBox
: Form
{
public String Input
{
get { return textInput.Text; }
}
public InputBox()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void InputBox_Load(object sender, EventArgs e)
{
this.ActiveControl = textInput;
}
public static DialogResult Show(String title, String message, String inputTitle, out String inputValue)
{
InputBox inputBox = null;
DialogResult results = DialogResult.None;
using (inputBox = new InputBox() { Text = title })
{
inputBox.labelMessage.Text = message;
inputBox.splitContainer2.SplitterDistance = inputBox.labelMessage.Width;
inputBox.labelInput.Text = inputTitle;
inputBox.splitContainer1.SplitterDistance = inputBox.labelInput.Width;
inputBox.Size = new Size(
inputBox.Width,
8 + inputBox.labelMessage.Height + inputBox.splitContainer2.SplitterWidth + inputBox.splitContainer1.Height + 8 + inputBox.button2.Height + 12 + (50));
results = inputBox.ShowDialog();
inputValue = inputBox.Input;
}
return results;
}
void labelInput_TextChanged(object sender, System.EventArgs e)
{
}
}
partial class InputBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.labelInput = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Location = new System.Drawing.Point(3, 0);
this.labelMessage.MaximumSize = new System.Drawing.Size(379, 0);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(50, 13);
this.labelMessage.TabIndex = 99;
this.labelMessage.Text = "Message";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(316, 126);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Cancel";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(235, 126);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "OK";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelInput
//
this.labelInput.AutoSize = true;
this.labelInput.Location = new System.Drawing.Point(3, 6);
this.labelInput.Name = "labelInput";
this.labelInput.Size = new System.Drawing.Size(31, 13);
this.labelInput.TabIndex = 99;
this.labelInput.Text = "Input";
this.labelInput.TextChanged += new System.EventHandler(this.labelInput_TextChanged);
//
// textInput
//
this.textInput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textInput.Location = new System.Drawing.Point(3, 3);
this.textInput.Name = "textInput";
this.textInput.Size = new System.Drawing.Size(243, 20);
this.textInput.TabIndex = 1;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.labelInput);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textInput);
this.splitContainer1.Size = new System.Drawing.Size(379, 50);
this.splitContainer1.SplitterDistance = 126;
this.splitContainer1.TabIndex = 99;
//
// splitContainer2
//
this.splitContainer2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitContainer2.IsSplitterFixed = true;
this.splitContainer2.Location = new System.Drawing.Point(12, 12);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.labelMessage);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.splitContainer1);
this.splitContainer2.Size = new System.Drawing.Size(379, 108);
this.splitContainer2.SplitterDistance = 54;
this.splitContainer2.TabIndex = 99;
//
// InputBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 161);
this.Controls.Add(this.splitContainer2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label labelMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label labelInput;
private System.Windows.Forms.TextBox textInput;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
Usage:
String output = "";
result = System.Windows.Forms.DialogResult.None;
result = InputBox.Show(
"Input Required",
"Please enter the value (if available) below.",
"Value",
out output);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}
Note this exhibits a bit of auto sizing to keep it pretty based on how much text you ask it display. I also know it's lacking the bells and whistles but it's a solid step forward for those facing this same dilemma.
answered Sep 16 '15 at 18:25
David CarriganDavid Carrigan
333415
333415
add a comment |
add a comment |
There is no such thing: I recommend to write it for yourself and use it whenever you need.
add a comment |
There is no such thing: I recommend to write it for yourself and use it whenever you need.
add a comment |
There is no such thing: I recommend to write it for yourself and use it whenever you need.
There is no such thing: I recommend to write it for yourself and use it whenever you need.
answered Sep 18 '08 at 21:22
MADMapMADMap
1,85222029
1,85222029
add a comment |
add a comment |
protected by Community♦ May 12 '17 at 9:39
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?