I have some auto tests hitting web UI pages and web services using Selenium and NUnit. I recently added a dynamically created Windows form, which is displayed as a modal dialog and expects some user input like passwords, etc. Once the input has been entered, the settings are saved and encrypted using DPAPI and on a subsequent run, do not need user input anymore.
My issue is that I can debug up to the point where the dialog is displayed but as soon as the code reaches the modal dialog, Visual Studio just hangs. I can run the code and it works perfectly fine. I just can't debug it. Has anyone seen this issue before?
Code for user prompt:
public static class TestSettingsConfigExtensions
{
....
private static string PromptUserForSetting(string settingName)
{
string promptedValue = null;
DialogResult result = UserInput.Show("User Setting", $"Please enter a value for {settingName}", ref promptedValue);
if (result == DialogResult.OK)
return promptedValue;
return null;
}
private class UserInput
{
public static DialogResult Show(string title, string promptText, ref string value)
{
Form form = new Form();
Label lblPrompt = new Label();
TextBox txtSettingValue = new TextBox();
txtSettingValue.UseSystemPasswordChar = true;
Label lblUnmask = new Label();
CheckBox chkShouldUnmask = new CheckBox();
chkShouldUnmask.CheckedChanged += (object sender, EventArgs e) => { ChkShouldUnmask_CheckedChanged(chkShouldUnmask.Checked, ref txtSettingValue); };
Button btnOk = new Button();
Button btnCancel = new Button();
form.Text = title;
lblPrompt.Text = promptText;
txtSettingValue.Text = value;
lblUnmask.Text = "Unmask";
chkShouldUnmask.Checked = false;
btnOk.Text = "OK";
btnCancel.Text = "Cancel";
btnOk.DialogResult = DialogResult.OK;
btnCancel.DialogResult = DialogResult.Cancel;
lblPrompt.SetBounds(9, 20, 372, 13);
txtSettingValue.SetBounds(12, 36, 372, 20);
lblUnmask.SetBounds(9, 65, 50, 13);
chkShouldUnmask.SetBounds(59, 65, 372, 13);
btnOk.SetBounds(228, 102, 75, 23);
btnCancel.SetBounds(309, 102, 75, 23);
lblPrompt.AutoSize = true;
txtSettingValue.Anchor = txtSettingValue.Anchor | AnchorStyles.Right;
lblUnmask.AutoSize = true;
chkShouldUnmask.Anchor = chkShouldUnmask.Anchor | AnchorStyles.Right;
btnOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 137);
form.Controls.AddRange(new Control[] { lblPrompt, txtSettingValue, lblUnmask, chkShouldUnmask, btnOk, btnCancel });
form.ClientSize = new Size(Math.Max(300, lblPrompt.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = btnOk;
form.CancelButton = btnCancel;
DialogResult dialogResult = form.ShowDialog();
value = txtSettingValue.Text;
return dialogResult;
}
private static void ChkShouldUnmask_CheckedChanged(bool shouldUnmask, ref TextBox txtSettingValue)
{
if (shouldUnmask)
txtSettingValue.UseSystemPasswordChar = false;
else
txtSettingValue.UseSystemPasswordChar = true;
}
}
}
Related
Is it possible to have different colors for a message in MessageBox in C#?
Example:
Let the string be "Placeholder Text"
Can we display "Placeholder" in black and "Text" in red in a C# MessageBox?
As I know you cant change that in MessageBox because it depends on how your windows look
but you can create a new Form where you would do it
with this your shown Form will be only thing that can user manipulate until its closed:
Form2 form2 = new Form2();
form2.StartPosition = FormStartPosition.CenterScreen; //with this you can be sure that it will always open in the middle of the screen
form2.ShowDialog();
then you add from how I understand your question label and just write:
Label1.ForeColor = Color.Red;
Label1.BackColor = Color.Black;
I hope it will help and I hope I did understand your question
I have a custom MessageBox class that you can modify changing the Label for a RichTextBox as #Caius Jard comment.
This is the .designer file of the form:
partial class MessageBoxForm
{
/// <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.pictureBox = new System.Windows.Forms.PictureBox();
this.label = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBox
//
this.pictureBox.Location = new System.Drawing.Point(24, 32);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(52, 52);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// label
//
this.label.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.label.Location = new System.Drawing.Point(84, 28);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(433, 56);
this.label.TabIndex = 1;
this.label.Text = "Text";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.button1.Location = new System.Drawing.Point(12, 95);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 28);
this.button1.TabIndex = 2;
this.button1.Text = "&OK";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button2.Location = new System.Drawing.Point(93, 95);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 28);
this.button2.TabIndex = 3;
this.button2.Text = "&Cancel";
this.button2.UseVisualStyleBackColor = true;
this.button2.Visible = false;
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button3.Location = new System.Drawing.Point(174, 95);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 28);
this.button3.TabIndex = 4;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Visible = false;
//
// MessageBoxForm
//
this.AcceptButton = this.button1;
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.button2;
this.ClientSize = new System.Drawing.Size(529, 135);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label);
this.Controls.Add(this.pictureBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MessageBoxForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Text";
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
And the code:
public partial class MessageBoxForm : Form
{
public static Func<DialogResult, string> TranslateButtonText { get; set; }
public MessageBoxForm(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
this.InitializeComponent();
this.label.Text = text;
this.Text = string.IsNullOrEmpty(caption) ? AppDomain.CurrentDomain.FriendlyName : caption;
switch (buttons)
{
case MessageBoxButtons.OKCancel:
this.button2.Visible = true;
break;
case MessageBoxButtons.AbortRetryIgnore:
SetupButton(this.button1, DialogResult.Abort);
SetupButton(this.button2, DialogResult.Retry);
SetupButton(this.button3, DialogResult.Ignore);
this.CancelButton = null;
this.ControlBox = false;
break;
case MessageBoxButtons.YesNoCancel:
SetupButton(this.button1, DialogResult.Yes);
SetupButton(this.button2, DialogResult.No);
SetupButton(this.button3, DialogResult.Cancel);
this.CancelButton = this.button3;
break;
case MessageBoxButtons.YesNo:
SetupButton(this.button1, DialogResult.Yes);
SetupButton(this.button2, DialogResult.No);
this.CancelButton = null;
this.ControlBox = false;
break;
case MessageBoxButtons.RetryCancel:
SetupButton(this.button1, DialogResult.Retry);
SetupButton(this.button2, DialogResult.Cancel);
break;
}
switch (icon)
{
case MessageBoxIcon.None:
this.label.Width += this.label.Left - 12;
this.label.Left = 12;
this.Icon = null;
this.pictureBox.Visible = false;
break;
case MessageBoxIcon.Error:
this.SetIcon(SystemIcons.Error);
break;
case MessageBoxIcon.Question:
this.SetIcon(SystemIcons.Question);
break;
case MessageBoxIcon.Warning:
this.SetIcon(SystemIcons.Warning);
break;
case MessageBoxIcon.Information:
this.SetIcon(SystemIcons.Information);
break;
}
using (var g = this.CreateGraphics())
{
var size = g.MeasureString(text, this.label.Font);
if (size.Width <= this.label.Width)
{
var extraWidth = this.Width - this.label.Width;
this.Width = (int)(size.Width + extraWidth);
}
else
{
var lineHeight = (int)Math.Ceiling(size.Height);
size = g.MeasureString(text, this.label.Font, (int)this.label.Width);
var heightDiff = (int)size.Height - this.label.Height;
if (heightDiff > 0)
{
var extraHeight = this.Height - this.label.Height;
this.Height = extraHeight + (int)(Math.Ceiling(size.Height / lineHeight) * lineHeight);
}
this.label.TextAlign = ContentAlignment.MiddleLeft;
}
}
}
private void SetIcon(Icon icon)
{
if (Application.OpenForms.Count > 0)
{
this.Icon = Application.OpenForms[0].Icon;
}
this.pictureBox.Image = icon.ToBitmap();
}
private static void SetupButton(Button button, DialogResult result)
{
button.Text = TranslateButtonText != null ? TranslateButtonText(result) : $"&{result}";
button.Visible = true;
button.DialogResult = result;
}
public static DialogResult Show(string text)
{
return Show(text, AppDomain.CurrentDomain.FriendlyName);
}
public static DialogResult Show(string text, string caption)
{
return Show(text, caption, MessageBoxButtons.OK);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
{
return Show(text, caption, buttons, MessageBoxIcon.None);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
using (var form = new MessageBoxForm(text, caption, buttons, icon))
{
return form.ShowDialog();
}
}
}
TranslateButtonText allow you set text for buttons in other languages to localize your application. By default, english is used.
The last part of the contructor is used to determine the size of the dialog based in text length to show. The label has Anchor property set so here we change the form size (the label adapt to that size).
Basically you must replace Label with RichTextBox. You can expose RichTextBox as a property of the form to allow has full control (to set different colors, for example) outside of the form.
You can use like a MessageBox:
MessageBoxForm.Show("Message to show", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
MessageBoxForm.Show("All done");
Or get a form instance before show to make other things:
using (var form = new MessageBoxForm("Message to show", null, MessageBoxButtons.YesNo, MessageBoxIcon.Error))
{
// If you replace and expose the Label, you can do this:
//RichTextBox richTextBox = form.Message;
//richTextBox...
form.ShowDialog();
}
I´m stuck on a problem using controls.find and I can´t find the error.
I´m creating a loginform using different panels.
Panels are created as follows:
public Panel CreatePanel()
{
Panel login = new Panel();
TextBox Login_UsernameTB = new TextBox();
TextBox Login_PasswordTB = new TextBox();
Label label1 = new Label();
Label label2 = new Label();
Label loginstatus = new Label();
Button Login_Loginbtn = new Button();
Button Login_Registerbtn = new Button();
PictureBox Login_Logo = new PictureBox();
PictureBox Login_ECard = new PictureBox();
//creatingPanel
login.Location = new Point(0, 0);
login.Name = "Login";
login.Size = new Size(1000, 400);
//locations
Login_ECard.Location = new Point(500, 250);
Login_ECard.SizeMode = PictureBoxSizeMode.StretchImage;
Login_ECard.Image = PatientSimulator.Properties.Resources.Ecard;
Login_ECard.Name = "Login_Ecard";
Login_Logo.Location = new Point(148, 12);
Login_Logo.SizeMode = PictureBoxSizeMode.StretchImage;
Login_Logo.Image = PatientSimulator.Properties.Resources.Logo;
Login_Logo.Name = "Login_Logo";
label1.Location = new Point(31, 200);
label1.Size = new Size(58, 13);
label1.Text = "Username:";
label1.Name = "label1";
label2.Location = new Point(31, 244);
label2.Size = new Size(56, 13);
label2.Text = "Password:";
label2.Name = "label2";
Login_UsernameTB.Location = new Point(148, 197);
Login_UsernameTB.Size = new Size(359, 20);
Login_UsernameTB.Text = "Testfirma1";
Login_UsernameTB.Name = "Login_UsernameTB";
Login_PasswordTB.Location = new Point(148, 241);
Login_PasswordTB.Size = new Size(359, 20);
Login_PasswordTB.Text = "Hallo1234#";
Login_PasswordTB.PasswordChar = '*';
Login_PasswordTB.Name = "Login_PasswordTB";
Login_Loginbtn.Location = new Point(432, 334);
Login_Loginbtn.Size = new Size(75, 23);
Login_Loginbtn.Text = "Login";
Login_Loginbtn.Click += Login_Loginbtn_Click;
loginstatus.Location = new Point(323, 360);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
Login_Registerbtn.Location = new Point(323, 334);
Login_Registerbtn.Size = new Size(75, 23);
Login_Registerbtn.Text = "Register";
Login_Registerbtn.Click += Login_Registerbtn_Click;
login.Controls.Add(Login_ECard);
login.Controls.Add(Login_Logo);
login.Controls.Add(label1);
login.Controls.Add(label2);
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(Login_Loginbtn);
login.Controls.Add(Login_Registerbtn);
login.Controls.Add(loginstatus);
return login;
}
In Form_load:
Panel login = CreatePanel();
login.Visible = true;
Controls.Add(login);
When I start the application, everything is displayed. if I type some text into systemstatus.text isn't gets displayed.
When doing a check if the entered password & username are correct, the strange stuff happens.
private void Login_Loginbtn_Click(object sender, EventArgs e)
{
Patient.DBAccess db = new Patient.DBAccess();
Sha256 sha = new Sha256();
string username = login.Controls.Find("Login_UsernameTB", true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB", true)[0].Text;
Patient.DatabaseRequestInterface.UserInterface user = new Patient.DatabaseRequestInterface.UserInterface();
user.Username = username;
if (db.IsUserExisting(user)){
user = db.ReadUserPassword(user);
string salt = Encoding.ASCII.GetString(user.PasswordSalt);
byte[] pw = sha.GetSha256(userpw, salt);
if (pw.SequenceEqual(user.PasswordHash))
{
//Programm starten
//token generieren
Patient.DatabaseRequestInterface.UserInterface useri = db.ReadFullUser(user);
UserAuthentificationToken = new UserInfo(useri.Username, useri.Uuid, DateTime.Now);
LoginSuccessEvent?.Invoke(this, new EventArgs());
login.Controls.Find("loginstatus", true)[0].Text = "Login successfull";
this.Close();
}
else
{
MessageBox.Show("Login failed, Username and/or password incorrect");
}
}
else
{
MessageBox.Show("Falscher User oder Passwort");
//Controls.Find("Login_StatusL", true)[0].Text = "Login failed, Username and/or password incorrect!";
}
}
The first 2 times I use controls.find it works, I get the strings entered by the users at the corresponding TextBoxes. When I try to change loginstatus, I get a System.IndexOutOfRangeException. My interpretation of the exception is, that there was no loginstatus found. And I don´t understand why. (the other 2 elements can´t be found within the if as well)
Can anyone help?
the controls.find("string",true)[0].Text = "XXX" works, I use it a lot at another part of the application
Any ideas?
Thanks in advance,
David
You miss the Name property in your code and Controls.Find strictly work on Name
Login_UsernameTB.Name = "Login_UsernameTB";
So your Code should be something like this:
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900, 900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50, 50);
Login_UsernameTB.Text = "Username here";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150, 50);
Login_PasswordTB.Text = "password here";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150, 150);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
Also, the login should be class level but not defined inside the Form_Load
like this :
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
.....
I have tested it and works fine.
My test code is
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SackOverflow
{
public partial class Form1 : Form
{
Panel login;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
login = CreatePanel();
login.Visible = true;
Controls.Add(login);
}
public Panel CreatePanel()
{
Panel login = new Panel();
login.Size = new Size(900, 900);
TextBox Login_UsernameTB = new TextBox();
Login_UsernameTB.Name = "Login_UsernameTB";
Login_UsernameTB.Location = new Point(50, 50);
Login_UsernameTB.Text = "Username";
TextBox Login_PasswordTB = new TextBox();
Login_PasswordTB.Name = "Login_PasswordTB";
Login_PasswordTB.Location = new Point(150, 50);
Login_PasswordTB.Text = "password";
//stuff is asigned to TBs...
Label loginstatus = new Label();
loginstatus.Location = new Point(150, 150);
loginstatus.Size = new Size(300, 20);
loginstatus.Text = "";
loginstatus.Name = "loginstatus";
login.Controls.Add(Login_UsernameTB);
login.Controls.Add(Login_PasswordTB);
login.Controls.Add(loginstatus);
return login;
}
private void button1_Click(object sender, EventArgs e)
{
if (true)
{
string username = login.Controls.Find("Login_UsernameTB", true)[0].Text;
string userpw = login.Controls.Find("Login_PasswordTB", true)[0].Text;
//UserInterface user = //some database stuff to get the password saved by the user
if ("123" == userpw)
{
login.Controls.Find("loginstatus", true)[0].Text = "Logging in";
Login();
}
else
{
login.Controls.Find("loginstatus", true)[0].Text = "Error";
}
}
}
private void Login()
{
MessageBox.Show("Login");
}
}
}
Result is:
I receive the message box MessageBox.Show("Login"); shown and Loginstatus is shown too.
I am trying to show a WinForm (inputbox) from a console application in C# and wait until the user closes the form. It is important for me to have the inputbox ontop and active when it opens. ShowDialog() is not working in my case as in some cases it does not appears as an active form. So I'd like to change my code and use Show(). This way I can manually make find out if the form is active or not and if not activate it myself. With ShowDialog(). my code stops and I can not do anything until the from is closed.
Below is my code. It does show the inputbox, however it is frozen. What am I doing wrong please? As clear the while-loop after "inputBox.Show();" is not doing anything, but if I can manage to run the loop and the inputbox does not freeze, I will sort out the rest myself. I appreciate your help.
public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse)
{
string strResponse = null;
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
inputBox.ClientSize = new Size(500, 85);
inputBox.Text = strTitle;
inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width / 2) - (inputBox.ClientSize.Width / 2);
inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (inputBox.ClientSize.Height / 2);
Label lblPrompt = new Label();
lblPrompt.Text = strPrompt;
inputBox.Controls.Add(lblPrompt);
TextBox textBox = new TextBox();
textBox.Text = strDefaultResponse;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.Text = "&OK";
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.Text = "&Cancel";
inputBox.Controls.Add(cancelButton);
okButton.Click += (sender, e) =>
{
strResponse = textBox.Text;
inputBox.Close();
};
cancelButton.Click += (sender, e) =>
{
inputBox.Close();
};
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
SetWindowPos(inputBox.Handle, HWND_TOPMOST, inputBox.Left, inputBox.Top, inputBox.Width, inputBox.Height, NOACTIVATE);
inputBox.Show();
while {true}
Thread.Sleep(100);
Application.DoEvents();
return strResponse;
}
I'm not sure why you are taking this route, I'm sure there are better ways to do it (explain one at the end). however to make your code run you should add Application.DoEvents() inside your loop
the code should be something like this:
var formActive = true;
inputBox.FormClosed += (s, e) => formActive = false;
inputBox.Show();
inputBox.TopMost = true;
inputBox.Activate();
while (formActive)
{
Thread.Sleep(10);
Application.DoEvents();
}
and the whole method will be:
public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse)
{
string strResponse = null;
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
inputBox.ClientSize = new Size(500, 85);
inputBox.Text = strTitle;
inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (inputBox.ClientSize.Width/2);
inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (inputBox.ClientSize.Height/2);
Label lblPrompt = new Label();
lblPrompt.Text = strPrompt;
inputBox.Controls.Add(lblPrompt);
TextBox textBox = new TextBox();
textBox.Text = strDefaultResponse;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.Text = "&OK";
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.Text = "&Cancel";
inputBox.Controls.Add(cancelButton);
okButton.Click += (sender, e) =>
{
strResponse = textBox.Text;
inputBox.Close();
};
cancelButton.Click += (sender, e) =>
{
inputBox.Close();
};
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
var formActive = true;
inputBox.FormClosed += (s, e) => formActive = false;
inputBox.Show();
inputBox.TopMost = true;
inputBox.Activate();
while (formActive)
{
Thread.Sleep(10);
Application.DoEvents();
}
return strResponse;
}
but I think it would be a better Idea to Derive from Form and create a InputBox and set TopMost and call Activate OnLoad to create what you need, then simply call ShowDialog, something like:
class Inputbox : Form
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
TopMost = true;
Activate();
}
}
and better to put UI code in InputBox class as the whole code and usage would be like:
class Inputbox : Form
{
public string Response { get; set; }
public Inputbox(string strTitle, string strPrompt, string strDefaultResponse)
{
//add UI and Controls here
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
ClientSize = new Size(500, 85);
Text = strTitle;
StartPosition = System.Windows.Forms.FormStartPosition.Manual;
Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (ClientSize.Width/2);
Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (ClientSize.Height/2);
Label lblPrompt = new Label();
lblPrompt.Text = strPrompt;
Controls.Add(lblPrompt);
TextBox textBox = new TextBox();
textBox.Text = strDefaultResponse;
Controls.Add(textBox);
Button okButton = new Button();
okButton.Text = "&OK";
Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.Text = "&Cancel";
Controls.Add(cancelButton);
okButton.Click += (sender, e) =>
{
Response = textBox.Text;
Close();
};
cancelButton.Click += (sender, e) =>
{
Close();
};
AcceptButton = okButton;
CancelButton = cancelButton;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
TopMost = true;
Activate();
}
}
public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse)
{
string strResponse = null;
Inputbox inputBox = new Inputbox(strPrompt,strTitle,strDefaultResponse);
inputBox.ShowDialog();
return inputBox.Response;
}
You need to run a message loop:
Application.Run(inputBox);
I have made a class that contain such as form,button, etc in class. then i create the handle events of them in class. Take a look at my class code below:
public static class InputBox
{
static Form form = new Form();
static Label label = new Label();
static TextBox textBox = new TextBox();
static Button buttonOk = new Button();
static Button buttonCancel = new Button();
public static DialogResult Show(string title, string promptText, ref string value)
{
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
buttonOk.Click += new EventHandler(buttonOk_Click); //this is the handle code
buttonCancel.Click += new EventHandler(buttonCancel_Click);//this is the handle code
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
static void buttonCancel_Click(object sender, EventArgs e)
{
form.Close();
}
static void buttonOk_Click(object sender, EventArgs e)
{
MessageBox.Show("If u click this, You Will Get many Messages as many as you call the class");
}
}
then, i call it like this in my own form code:
string strbuffer; // Global Variable
InputBox.Show("Hi..?", "What Is Your Name?",ref strbuffer);
//do something...
InputBox.Show("Hi..?", "What Is Your Age?",ref strbuffer);
//do something
InputBox.Show("Hi..?", "How Many Cars do you have?",ref strbuffer);
//do something
if i call the Class as many as i call it, the old handler will never get dispose so the code in handler control will be excuted as many as you call the class
i guess the variable doesn't get dispose when the class is unused anymore so the variable always accumulate every times you call it
What would be the best way to solve this case?
I am not sure what you are asking but if its event related problem I would suggest deallocating the event.
static void buttonCancel_Click(object sender, EventArgs e)
{
form.Close();
buttonOk.Click -= new EventHandler(buttonOk_Click);
buttonCancel.Click -= new EventHandler(buttonCancel_Click);
}
static void buttonOk_Click(object sender, EventArgs e)
{
MessageBox.Show("If u click this, You Will Get many Messages as many as you call the class");
buttonOk.Click -= new EventHandler(buttonOk_Click);
buttonCancel.Click -= new EventHandler(buttonCancel_Click);
}
Here, I've moved the static variables for form, label, textbox etc into the Show method.
This means you get a new instance of these each time you call this method. Hence the Event is not already attached to it.
The event handler then had to change to not reference the static form (that doesn't exist anymore) so we got the reference to the form instance from the button.
public static class InputBox
{
public static DialogResult Show(string title, string promptText, ref string value)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
buttonOk.Click += new EventHandler(buttonOk_Click); //this is the handle code
buttonCancel.Click += new EventHandler(buttonCancel_Click);//this is the handle code
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
static void buttonCancel_Click(object sender, EventArgs e)
{
(sender as Button).FindForm().Close();
}
static void buttonOk_Click(object sender, EventArgs e)
{
MessageBox.Show("If u click this, You Will Get many Messages as many as you call the class");
}
}
What is the C# version of VB.NET's InputBox?
Add a reference to Microsoft.VisualBasic, InputBox is in the Microsoft.VisualBasic.Interaction namespace:
using Microsoft.VisualBasic;
string input = Interaction.InputBox("Prompt", "Title", "Default", x_coordinate, y_coordinate);
Only the first argument for prompt is mandatory
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);
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.
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.
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.
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
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.
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.
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);
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.
There is no such thing: I recommend to write it for yourself and use it whenever you need.