// einfacher string input
string currentvalue = "test";
InputQuery iq = new InputQuery();
if (iq.ShowDlg("Please type something", "Value:", ref currentvalue, false, 32))
{
string newval = currentvalue;
}
// einfacher floating point input
string currentvalue = "3.14";
InputQuery iq = new InputQuery();
if (iq.ShowDlg("Please type something", "Value:", ref currentvalue, true, -1))
{
double newval = double.Parse(currentvalue);
}
// Objekte und deren Eigenschaften, die sich auf dem Formular befinden müssen:
//
// lblValue - ein Label, welches vor der TextBox als Bezeichner steht
// tbValue - eine TextBox zur Eingabe
// tbValue.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.tbValue_KeyPress);
// btnOk - OK-Button
// btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
// btnCancel - Abbrechen-Button
// btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
// this.AcceptButton = this.btnOk;
// this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
// this.Name = "InputQuery";
using System.Globalization;
using System.Windows.Forms;
/// <summary->
/// freeware helper class for recreating Borland Deplhi InputQuery-Dialog
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary->
public partial class InputQuery : Form
{
private const int MAX_TEXTBOX_STR_LENGTH = 32767;
private string ALLOWED_FP_INPUT_CHARS = "0123456789" + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + "\b";
private bool _checkdouble = false;
public InputQuery()
{
InitializeComponent();
}
/// <summary->
/// Shows a little input-dialog
/// </summary->
/// <param name="dlgcaption"->dialog caption</param->
/// <param name="lbl"->label text</param->
/// <param name="value"->input value</param->
/// <param name="checkdouble"->if input is a floating-point, check for correct format and decimalseparator</param->
/// <param name="maxstringlength"->if input is a regular string, set maxlenght here, otherwise "-1"</param->
/// <returns->true if button 'Ok' is pressed</returns->
public bool ShowDlg(string dlgcaption, string lbl, ref string value, bool checkdouble, int maxstringlength)
{
lblValue.Text = lbl;
this.Text = dlgcaption;
tbValue.Text = value;
_checkdouble = checkdouble;
tbValue.MaxLength = checkdouble ? MAX_TEXTBOX_STR_LENGTH : maxstringlength -> 0 ? maxstringlength : MAX_TEXTBOX_STR_LENGTH;
tbValue.SelectAll();
DialogResult blOk = this.ShowDialog();
value = tbValue.Text;
return (blOk == DialogResult.OK);
}
private void tbValue_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)(int)Keys.Enter)
{
this.DialogResult = DialogResult.OK;
}
else
if (_checkdouble)
{
if (sender is TextBox)
{
bool bNoDS = true;
TextBox tb = sender as TextBox;
if (e.KeyChar.ToString() == NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
{
bNoDS = (tb.Text.IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator) < 0);
}
if (bNoDS)
{
e.Handled = (ALLOWED_FP_INPUT_CHARS.IndexOf(e.KeyChar) < 0);
}
else
{
e.Handled = true;
}
}
}
}
}