Get multiple returns from one string function - c#

Based on /users/668272/bas dialog box. I tried writing more in order to get multiple values
public static class ControlInfo
{
public static TextBox[] TextData { get; set; }
}
public static class Prompt
{
public static Form prompt = new Form();
public static void ShowDialog(string Text, string caption)
{
//string[] inputval = new string[6];
prompt = new Form()
{
Width = 500,
Height = 650,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
#region:create
Label textLabel = new Label() { Left = 20, Top = 20, Text = Text };
TextBox textBox1 = new TextBox() { Left = 50, Top = 100, Width = 400 };
Button confirmationok = new Button()
{ Text = "Save", Left = 250, Width = 100, Top = 550, DialogResult = DialogResult.OK };
Button confirmationcancel = new Button()
{ Text = "Cancel", Left = 350, Width = 100, Top = 550, DialogResult = DialogResult.Cancel };
confirmationok.Click += (sender, e) => { Save(); };
confirmationcancel.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmationcancel);
prompt.Controls.Add(textBox1);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textBox3);
prompt.Controls.Add(textBox4);
prompt.Controls.Add(textBox5);
prompt.Controls.Add(textBox6);
prompt.Controls.Add(confirmationok);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textLabel1);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(textLabel3);
prompt.Controls.Add(textLabel4);
prompt.Controls.Add(textLabel5);
prompt.Controls.Add(textLabel6);
prompt.AcceptButton = confirmationok;
prompt.AcceptButton = confirmationcancel;
prompt.ShowDialog();
}
public static void Save()
{
var cArray = prompt.Controls.OfType<TextBox>().ToArray();
ControlInfo.TextData = cArray;
}
}
[Update] I tried using string[] to get the values but this keeps on happening: return inputval; 'Index was outside the bounds of the array.' So how do I return the values to string[] array properly?
[Update 2] Fixed the problem by creating a TextData[] to store the values then calling it out like this ControlInfo.TextData[0].Text

I used a Textdata class to store the values from dialog through SAVE button. The completed code is in the question

Related

Close button also save values

I have a dialog where the user can input two integers with OK button. The entered values are saved to variables. However, if I close it from the close button it also save the last entered values, which means not only the OK button save them. what am I doing wrong here?
The dialog:
namespace WindowsFormsApplication1
{
public static class inputBoxDialog
{
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170 };
confirmation.Click += (sender, e) => { prompt.Close();};
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
prompt.ShowDialog();
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
}
}
I call the dialog here:
namespace WindowsFormsApplication1
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
int[] loopParams = new int[2] { 0, 0 };
private void button1_Click(object sender, EventArgs e)
{
inputBoxDialog.ShowDialog("For Loop Begin: ", "For Loop End: ", "Popup", loopParams);
//display the result
label1.Text = "1:- " + loopParams[0]+"\r\n2:- " + loopParams[1];
}
}
}
As pointed out by Jimi, you need to set the OK buttons DialogResult property to DialogResult.OK:
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
And to check the returned DialogResult value from the prompt.ShowDialog() method:
if (prompt.ShowDialog() == DialogResult.OK)
{
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
Here is your code with the above additions:
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
if (prompt.ShowDialog() == DialogResult.OK)
{
// OK
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
else
{
// Cancel
}
}
Checking the prompt.DialogResult in the prompt.FormClosed event:
prompt.FormClosed += (sender, e) =>
{
if (prompt.DialogResult == DialogResult.OK)
{
// OK
}
else
{
// Cancel
}
};

Create multiple textbox dynamically in single form using c#?

My aim is to create a .dll file dynamically having TextBox,Button which can be used by anyone in a program using Visual C#.
It would get created in Class Library, No WFA tools would get used.
I need help in creating a form which can generate multiple TextBox according to the attributes provided by the user.
1)No of TextBox
2)Location
3)Size etc
Code
CLASS.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace Forms
{
public class TextForm : Form
{
public TextBox txtBox1;
public TextForm(int a, int b, int c, int d, string e)
{
Form f1 = new Form();
txtBox1 = new TextBox();
txtBox1.Visible = true;
//f1.ActiveControl=txtBox1;
f1.Controls.Add(txtBox1);
txtBox1.Focus();
f1.Visible = true;
txtBox1.Size = new Size(a, b);
txtBox1.Location = new Point(c, d);
txtBox1.Text = (e).ToString();
this.Controls.Add(txtBox1);
txtBox1.Visible = true;
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
}
}}
PROGRAM.CS
using System;
using Forms;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TextForm Box1 = (new TextForm(150, 14, 20, 32, "This is a TextBox 1"));
TextForm Box2 = (new TextForm(180, 34, 40, 52, "This is a TextBox 2"));
}}}
What should be the code?
The problem is that you are creating a Form for each TextBox. This is not what you want, provided that you plan to have forms with multiple text boxes.
I see two possibilities: you either want to create a) a textbox that you can easily add to your form, or b) a form with textboxes.
public class TextInput : Form
{
public TextBox TxtBox {
get; private set;
}
public Control Container {
get; private set;
}
public TextInput(Control c, int a, int b, int c, int d, string e)
{
this.Container = c;
this.TxtBox = new TextBox();
var txtBox1 = this.TxtBox;
txtBox1.Visible = true;
c.Controls.Add(txtBox1);
txtBox1.Focus();
txtBox1.Size = new Size(a, b);
txtBox1.Location = new Point(c, d);
txtBox1.Text = (e).ToString();
txtBox1.Visible = true;
}
}
You'd use this as follows:
var f = new Form();
var txtBox1 = new TextInput( f, 100, 25, 10, 10, "Name" );
var txtBox1 = new TextInput( f, 100, 25, 10, 50, "Age" );
var txtBox1 = new TextInput( f, 100, 25, 10, 100, "Address" );
var txtBox1 = new TextInput( f, 100, 25, 10, 150, "Phone" );
The second possibility is much more interesting, in my opinion. You want to create a special Form that automatically adds text boxes as soon a you call a simple method. I'm going to simplify your code, though. It is not a good idea (at all), to use absolute positioning in your forms.
The following creates a form with text boxes and their labels. The textboxes occupy the whole width of the form. This is achieved by using a TableLayoutPanel in which a Panel subPanel is used for each row.
This subPanel holds a label and a text box.
public class InputForm: Form {
public InputForm()
{
this.Panel = new TableLayoutPanel{ Dock = DockStyle.Fill };
this.textBoxes = new List<TextBox>();
this.Controls.Add( this.Panel );
}
public TextBox AddTextBox(string label)
{
var subPanel = new Panel { Dock = DockStyle.Top };
var lblLabel = new Label { Text = label, Dock = DockStyle.Left };
var tbEdit = new TextBox{ Dock = DockStyle.Fill };
subPanel.Controls.Add( tbEdit );
subPanel.Controls.Add( lblLabel );
this.Panel.Controls.Add( subPanel );
return tbEdit;
}
public TableLayoutPanel Panel {
get; private set;
}
public TextBox[] TextBoxes {
get {
return this.textBoxes.ToArray();
}
}
private List<TextBox> textBoxes;
}
You can use this with the following simple code:
var form = new InputForm();
var tbName = form.AddTextBox( "Name" );
var tbAge = form.AddTextBox( "Age" );
var tbAddress = form.AddTextBox( "Address" );
form.Show();
Application.Run( form );
If you'd like to give a few attributes to the text boxes to be created (colors, font, bold...), then you have two ways. The first one is to add parameters to the AddTextBox() method, though that would not scalate well as long as the number of attributes grows. The alternative is to create a TextBoxAttributes class, which will hold the configuring attributes for a given TextBox.
public class InputForm: Form {
public class TextBoxAttributes {
public TextBoxAttributes() {
this.ForeColor = DefaultForeColor;
this.BackColor = DefaultBackColor;
this.Font = DefaultFont;
}
public Color ForeColor {
get; set;
}
public Color BackColor {
get; set;
}
public Font Font {
get; set;
}
public bool Bold {
get {
return this.Font.Bold;
}
set {
var style = FontStyle.Regular;
if ( value ) {
style = FontStyle.Bold;
}
this.Font = new Font( this.Font, style );
}
}
public bool Italic {
get {
return this.Font.Bold;
}
set {
var style = FontStyle.Regular;
if ( value ) {
style = FontStyle.Italic;
}
this.Font = new Font( this.Font, style );
}
}
public bool Underline {
get {
return this.Font.Bold;
}
set {
var style = FontStyle.Regular;
if ( value ) {
style = FontStyle.Underline;
}
this.Font = new Font( this.Font, style );
}
}
public float FontSize {
get {
return this.Font.Size;
}
set {
this.Font = new Font( this.Font.FontFamily, value );
}
}
}
// ... more things...
public TextBox AddTextBox(string label)
=> this.AddTextBox( label, new TextBoxAttributes() );
public TextBox AddTextBox(string label, TextBoxAttributes attr)
{
var subPanel = new Panel { Dock = DockStyle.Top };
var lblLabel = new Label { Text = label, Dock = DockStyle.Left };
var tbEdit = new TextBox{
Dock = DockStyle.Fill,
ForeColor = attr.ForeColor,
BackColor = attr.BackColor,
Font = attr.Font
};
subPanel.Controls.Add( tbEdit );
subPanel.Controls.Add( lblLabel );
this.Panel.Controls.Add( subPanel );
return tbEdit;
}
// ... more things...
}
The main code would be:
public static void Main()
{
var form = new InputForm();
var tbName = form.AddTextBox( "Name", new InputForm.TextBoxAttributes {
ForeColor = Color.Yellow,
BackColor = Color.Blue
});
var tbAge = form.AddTextBox( "Age", new InputForm.TextBoxAttributes {
ForeColor = Color.Green,
BackColor = Color.Black,
Bold = true
});
var tbAddress = form.AddTextBox( "Address" );
form.Show();
Application.Run( form );
}
Hope this helps.
If you are willing to switch to WPF this will become a lot easier, since you can profit from Autolayout and Bindings.
You could easily switch the Wrappanel for a StackPanel or a DockPanel.
The class will create a View based on the public properties of the handed over object. You might have to add additional Types to the Types-Dictionary. In my case those two where sufficient.
Create a Property of the DynamicControl and Bind to it in XAML.
XAML:
<ContentPresenter Content="{Binding Path=DynView}" />
public ViewModel
{
public UserControl DynView {get; private set};
private ModelType _model;
public ViewModel(ModelType model)
{
_model = model;
DynView = new DynamicControl<ModelType>(_model);
}
}
public class DynamicControl<T> : UserControl
{
static DynamicControl()
{
Types[typeof(bool)] = (binding) =>
{
CheckBox cb = new CheckBox();
cb.SetBinding(CheckBox.IsCheckedProperty,binding);
return cb;
};
Types[typeof(String)] = (binding) =>
{
TextBox tb = new TextBox();
tb.SetBinding(TextBox.TextProperty, binding);
return tb;
};
// add additional Types if necessary
}
private T _model;
public DynamicControl(T model)
{
_model = model;
WrapPanel wp = new WrapPanel();
foreach (PropertyInfo pi in model.GetType().GetProperties())
{
Grid g = new Grid();
g.Margin = new Thickness(5, 5, 25, 5);
g.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
g.ColumnDefinitions.Add(new ColumnDefinition());
g.ColumnDefinitions.Add(new ColumnDefinition());
g.RowDefinitions.Add(new RowDefinition());
TextBlock tb = new TextBlock() { Text = pi.Name };
tb.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, 0);
g.Children.Add(tb);
System.Windows.FrameworkElement uie = GetUiElement(pi);
uie.Margin = new Thickness(10, 0, 0, 0);
uie.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(uie, 1);
Grid.SetRow(uie, 0);
g.Children.Add(uie);
wp.Children.Add(g);
}
this.Content = wp;
}
private FrameworkElement GetUiElement(PropertyInfo pi)
{
System.Windows.Data.Binding binding = new System.Windows.Data.Binding(pi.Name);
binding.Source = _model;
Func<System.Windows.Data.Binding, FrameworkElement> func;
FrameworkElement uie = null;
if (Types.TryGetValue(pi.PropertyType, out func))
uie = func(binding);
else
uie = Types[typeof(String)](binding);
return uie;
}
private static Dictionary<Type, Func<System.Windows.Data.Binding, FrameworkElement>> Types = new Dictionary<Type, Func<System.Windows.Data.Binding, FrameworkElement>>();
}
I think you're going at this all wrong. Just define your dimensions and text in advance, put them in a data class, and then feed a list of those data classes to your form constructor so it can construct them all on the fly.
The data class:
public class TextboxInfo
{
public Int32 Width { get; set; }
public Int32 Height { get; set; }
public Int32 X { get; set; }
public Int32 Y { get; set; }
public String Text { get; set; }
public TextboxInfo(Int32 w, Int32 h, Int32 x, Int32 y, String text)
{
this.Width = w;
this.Height = h
this.X = x;
this.Y = y
this.Text = text;
}
}
The code to construct the form:
public class TextForm : Form
{
public TextBox[] TextBoxes
{
get { return _textBoxes.ToArray(); }
}
private List<TextBox> _textBoxes;
public TextForm(TextboxInfo[] textboxes, Int32 padX, Int32 padY)
{
_textBoxes = new List<TextBox>();
Int32 reqWidth = 0;
Int32 reqHeight = 0;
foreach (TextboxInfo tbi in textboxes)
{
reqWidth = Math.Max(reqWidth, tbi.X + tbi.Width);
reqHeight = Math.Max(reqHeight, tbi.Y + tbi.Height);
TextBox txtB = new TextBox();
txtB.Size = new Size(tbi.Width, tbi.Height);
txtB.Location = new Point(tbi.X, tbi.Y);
txtB.Text = tbi.Text;
_textBoxes.Add(txtB);
this.Controls.Add(txtB);
}
// You may want to add some kind of OK button at the end here (based on reqHeight)
// and link that to a click listener that closes the form.
// Don't forget to adjust your reqHeight to the added height of that button!
// ...
// Set form to the minimum needed size according to its elements.
this.Size = new Size(reqWidth + padX, reqHeight + padY);
}
}
The calling code:
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TextboxInfo[] info = new TextboxInfo[2];
info[0] = new TextboxInfo(150, 14, 20, 32, "This is a TextBox 1");
info[1] = new TextboxInfo(180, 34, 40, 52, "This is a TextBox 2");
TextForm frm = new TextForm(info, 20, 32);
frm.ShowDialog();
// Now you can access the form's text box values through frm.TextBoxes[i].Text
}
}
Mind you, this whole system may seem useful at first, but consider that none of the text boxes have labels on them. Just starting values.
I've made systems for custom data before in a project I created, to generate a custom save options dialog depending on the chosen file type to save to, since each file type needed specific options.
Realistically, you'd create a form with some kind of description at the top and an OK and a Cancel button at the bottom, with a panel in between which has its vertical scrollbar set to enable-when-needed. Then you can dynamically put different custom controls in there to support different data types, like say, a checkbox, a text field, a numeric field, et cetera. They'll automatically be listed vertically in the list simply by keeping track of each control's height to get the next control's Y-offset, and if they'd exceed the form size the panel will make sure you can scroll down.
All you'd give to the form are objects of a data class like the one I showed, but without positioning data. They'd have a type, to figure out what kind of custom control to create, a description text, a default value to set the input control to, and possibly some kind of initialisation value, for example, to limit the range of a numeric value, or, as in the image I showed, values for a dropdown list.

how to get return value from prompt input box? C# win Forms

I have created prompt input box in which user enter two values and press button and i want to return values on button click and get these values in other method.
Here is my code
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 10, Text = text };
Label textLabel2 = new Label() { Left = 50, Top = 55, Text = text };
textLabel2.Text = "Replace with";
TextBox textBox = new TextBox() { Left = 50, Top = 70, Width = 200 };
TextBox textBox2 = new TextBox() { Left = 50, Top = 30, Width = 200 };
Button confirmation = new Button() { Text = "Replace", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
private void stringReplacedToolStripMenuItem_Click(object sender, EventArgs e)
{
string promptValue = Form1.ShowDialog("Find What", "Replace");
}
I want to get values of textbox and textbox2 in other method.Thanks
Okay, just return an array with the two textbox values.
public static string[] ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 10, Text = text };
Label textLabel2 = new Label() { Left = 50, Top = 55, Text = text };
textLabel2.Text = "Replace with";
TextBox textBox = new TextBox() { Left = 50, Top = 70, Width = 200 };
TextBox textBox2 = new TextBox() { Left = 50, Top = 30, Width = 200 };
Button confirmation = new Button() { Text = "Replace", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? new string[] { textBox.Text, textBox2.Text } : null;
}
private void stringReplacedToolStripMenuItem_Click(object sender, EventArgs e)
{
string[] promptValue = Form1.ShowDialog("Find What", "Replace");
var textBoxValue = promptValue[0];
var textBox2Value = promptValue[1];
}

Control Dynamically created listbox property in other function

for (int r = 0; r < m; r++)
{
TextBlock myTextBlockr = new TextBlock() { Text = "Text Block", Width = 350, Height = 40, FontSize = 20, VerticalAlignment = VerticalAlignment.Center, TextAlignment = TextAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center };
string name = "TextBlock" + r.ToString();
myTextBlockr.Name = name;
ListBox list = new ListBox() { Height = 200, Width = 200 };
string k="list"+r;
list.Name = k;
}
I have created list box dynamically.I want to control its visibility in other function.I have used activator.createinstance method but it throws exception.
public void myTextBlock1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
string name = ((TextBlock)sender).Name;
// name.Substring(10,9);
string strl = "list" + name.Substring(9);
object obj = Activator.CreateInstance(typeof(ListBox), strl);
ListBox list = (ListBox)obj;
list.Visibility = Visibility.Visible;
}
It throws following exception System.missingmethodexception .
Keep a reference to the ListBox in your class if you need to refer to it later. If you're creating multiple ListBoxes, add them to a Dictionary<string, ListBox> so you could look them up by a key later.
public class Demo {
private Dictionary<string, ListBox> _listboxes = new Dictionary<string, ListBox>();
private void CreateListBoxWithName(string name) {
var lb = new ListBox();
_listboxes.add(name, lb);
// do other stuff ...
}
private ListBox FindListBoxByName(string name) {
return _listboxes[name];
}
}

Windows C# Form: Prompt focus on a textbox

I was wondering how would I auto select a textbox when using a Prompt on windows form. My code below shows what I have tried, but it is still focusing on the button and not the textbox. Thank you in advance for the help and assistance.
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 };
confirmation.Click += (sender, e) => { prompt.Close(); };
textBox.Select();
textBox.Focus();
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.ShowDialog();
return textBox.Text;
You need to wait to focus the textbox until after the form is shown. Before the form has been shown for the first time it is not able to focus anything. You can use the Shown event to execute some code after the form is first shown.
string text = "Text";
string caption = "caption";
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 };
confirmation.Click += (s, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.Shown += (s, e) => textBox.Focus();
prompt.ShowDialog();
return textBox.Text;

Categories

Resources