With inputs:
Or like classical MessageBox
:
Introduction
In C#, there isn't anything like InputBox
. The only way how to use that is import Visual Basic library with that ugly one.
Using the Code
Description
- Five languages (default English)
- Four types of icon
- Four possibilities for button option
- Choose if you want
TextBox
, ComboBox
or nothing (only message text)
- Using
string
array for ComboBox
items
- Set font for message text
- Set visibility in taskbar
- Returns
DialogResult
- Returned
string
value is InputBox.ResultValue
Here is a sample of how to use:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MsgBox;
namespace InputBoxUsage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
InputBox.SetLanguage(InputBox.Language.English);
DialogResult res = InputBox.ShowDialog("Select some item from ComboBox below:",
"Combo InputBox", InputBox.Icon.Question, InputBox.Buttons.OkCancel, InputBox.Type.TextBox, new string[] { "Item1","Item2","Item3"}, true, new System.Drawing.Font("Calibri", 10F, System.Drawing.FontStyle.Bold));
if (res == System.Windows.Forms.DialogResult.OK || res == System.Windows.Forms.DialogResult.Yes)
listView1.Items.Add(InputBox.ResultValue); }
private void Form1_Load(object sender, EventArgs e)
{
listView1.Columns.Add("Result", 100);
listView1.View = View.Details;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
namespace MsgBox
{
public static class InputBox
{
private static System.Windows.Forms.Form frm = new System.Windows.Forms.Form();
public static string ResultValue;
private static DialogResult DialogRes;
private static string[] buttonTextArray = new string[4];
public enum Icon
{
Error,
Exclamation,
Information,
Question,
Nothing
}
public enum Type
{
ComboBox,
TextBox,
Nothing
}
public enum Buttons
{
Ok,
OkCancel,
YesNo,
YesNoCancel
}
public enum Language
{
Czech,
English,
German,
Slovakian,
Spanish
}
public static DialogResult ShowDialog(string Message, string Title = "",
Icon icon = Icon.Information, Buttons buttons = Buttons.Ok, Type type = Type.Nothing,
string[] ListItems = null, bool ShowInTaskBar = false, Font FormFont = null)
{
frm.Controls.Clear();
ResultValue = "";
frm.MaximizeBox = false;
frm.MinimizeBox = false;
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
frm.Size = new System.Drawing.Size(350, 170);
frm.Text = Title;
frm.ShowIcon = false;
frm.ShowInTaskbar = ShowInTaskBar;
frm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(frm_FormClosing);
frm.StartPosition = FormStartPosition.CenterParent;
Panel panel = new Panel();
panel.Location = new System.Drawing.Point(0, 0);
panel.Size = new System.Drawing.Size(340, 97);
panel.BackColor = System.Drawing.Color.White;
frm.Controls.Add(panel);
panel.Controls.Add(Picture(icon));
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
label.Text = Message;
label.Size = new System.Drawing.Size(245, 60);
label.Location = new System.Drawing.Point(90, 10);
label.TextAlign = ContentAlignment.MiddleLeft;
panel.Controls.Add(label);
foreach (Button btn in Btns(buttons))
frm.Controls.Add(btn);
Control ctrl = Cntrl(type, ListItems);
panel.Controls.Add(ctrl);
if (ctrl.Name == "textBox")
frm.ActiveControl = ctrl;
if (FormFont != null)
label.Font = FormFont;
frm.ShowDialog();
switch (type)
{
case Type.Nothing:
break;
default:
if (DialogRes == DialogResult.OK || DialogRes == DialogResult.Yes)
{ ResultValue = ctrl.Text; }
else ResultValue = "";
break;
}
return DialogRes;
}
private static void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
switch (button.Name)
{
case "Yes":
DialogRes = DialogResult.Yes;
break;
case "No":
DialogRes = DialogResult.No;
break;
case "Cancel":
DialogRes = DialogResult.Cancel;
break;
default:
DialogRes = DialogResult.OK;
break;
}
frm.Close();
}
private static void textBox_KeyDown(object sender, System.Windows .Forms .KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
DialogRes = DialogResult.OK;
frm.Close();
}
}
private static void frm_FormClosing(object sender, System .Windows .Forms .FormClosingEventArgs e)
{
if (DialogRes != null) { }
else DialogRes = DialogResult.None;
}
private static PictureBox Picture(Icon icon)
{
System.Windows.Forms.PictureBox picture = new System.Windows.Forms.PictureBox();
var assembly = Assembly.GetExecutingAssembly(); System.IO.Stream stream = null;
switch (icon)
{
case Icon.Error:
stream = assembly.GetManifestResourceStream(@"MsgBox.error.png");
break;
case Icon.Exclamation:
stream = assembly.GetManifestResourceStream(@"MsgBox.exclamation.png");
break;
case Icon.Information:
stream = assembly.GetManifestResourceStream(@"MsgBox.information.png");
break;
case Icon.Question:
stream = assembly.GetManifestResourceStream(@"MsgBox.question.png");
break;
case Icon.Nothing:
stream = assembly.GetManifestResourceStream(@"MsgBox.nic80x80.png");
break;
}
picture.Image = System.Drawing.Image.FromStream(stream);
picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picture.Size = new System.Drawing.Size(60, 60);
picture.Location = new System.Drawing.Point(10, 10);
return picture;
}
private static Button[] Btns(Buttons button, Language lang = Language.English)
{
System.Windows.Forms.Button[] returnButtons = new Button[3];
System.Windows.Forms.Button OkButton = new System.Windows.Forms.Button();
System.Windows.Forms.Button StornoButton = new System.Windows.Forms.Button();
System.Windows.Forms.Button AnoButton = new System.Windows.Forms.Button();
System.Windows.Forms.Button NeButton = new System.Windows.Forms.Button();
OkButton.Text = buttonTextArray[0];
OkButton.Name = "OK";
AnoButton.Text = buttonTextArray[1];
AnoButton.Name = "Yes";
NeButton.Text = buttonTextArray[2];
NeButton.Name = "No";
StornoButton.Text = buttonTextArray[3];
StornoButton.Name = "Cancel";
switch (button)
{
case Buttons.Ok:
OkButton.Location = new System.Drawing.Point(250, 101);
returnButtons[0] = OkButton;
break;
case Buttons.OkCancel:
OkButton.Location = new System.Drawing.Point(170, 101);
returnButtons[0] = OkButton;
StornoButton.Location = new System.Drawing.Point(250, 101);
returnButtons[1] = StornoButton;
break;
case Buttons.YesNo:
AnoButton.Location = new System.Drawing.Point(170, 101);
returnButtons[0] = AnoButton;
NeButton.Location = new System.Drawing.Point(250, 101);
returnButtons[1] = NeButton;
break;
case Buttons.YesNoCancel:
AnoButton.Location = new System.Drawing.Point(90, 101);
returnButtons[0] = AnoButton;
NeButton.Location = new System.Drawing.Point(170, 101);
returnButtons[1] = NeButton;
StornoButton.Location = new System.Drawing.Point(250, 101);
returnButtons[2] = StornoButton;
break;
}
foreach (Button btn in returnButtons)
{
if (btn != null)
{
btn.Size = new System.Drawing.Size(75, 23);
btn.Click += new System.EventHandler(button_Click);
}
}
return returnButtons;
}
private static Control Cntrl(Type type, string[] ListItems)
{
System.Windows.Forms.ComboBox comboBox = new System.Windows.Forms.ComboBox();
comboBox.Size = new System.Drawing.Size(180, 22);
comboBox.Location = new System.Drawing.Point(90, 70);
comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
comboBox.Name = "comboBox";
if (ListItems != null)
{
foreach (string item in ListItems)
comboBox.Items.Add(item);
comboBox.SelectedIndex = 0;
}
System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
textBox.Size = new System.Drawing.Size(180, 23);
textBox.Location = new System.Drawing.Point(90, 70);
textBox.KeyDown += new System.Windows.Forms.KeyEventHandler(textBox_KeyDown);
textBox.Name = "textBox";
Control returnControl = new Control();
switch (type)
{
case Type.ComboBox:
returnControl = comboBox;
break;
case Type.TextBox:
returnControl = textBox;
break;
}
return returnControl;
}
public static void SetLanguage(Language lang)
{
switch (lang)
{
case Language.Czech:
buttonTextArray = "OK,Ano,Ne,Storno".Split(',');
break;
case Language.German:
buttonTextArray = "OK,Ja,Nein,Stornieren".Split(',');
break;
case Language.Spanish:
buttonTextArray = "OK,Sí,No,Cancelar".Split(',');
break;
case Language.Slovakian:
buttonTextArray = "OK,Áno,Nie,Zrušit".Split(',');
break;
default:
buttonTextArray = "OK,Yes,No,Cancel".Split(',');
break;
}
}
}
}
The code is written in a very simple way, nothing special. InputBox
is a static
class to avoid the need to create a new instance in every use.
First is form defined. On the form is Panel
object which contains other controls but buttons.
- Icon is defined by Icon
Enum
. Pictures are compiled as integrated source.
- Buttons are defined by Buttons
Enum
. Function Btns
returns Buttons in array. The language of buttons text is defined by Language Enum
.
- Function
Cntrl
returns TextBox
or ComboBox
by Type enum
. If Type.Nothing
is selected, InputBox
will not have any input control. ResultValue
will be "". TextBox
has assigned KeyPress
event for Enter key that fills ResultValue
as TextBox.Text
, DialogResult
as OK and closes the form.
- The form returns classic
DialogResult
like a MessageBox
.
The source code is in the attachment (VS2012) and can be downloaded from the link at the top.
I hope it's a useful solution and it helps somebody.