Вы находитесь на странице: 1из 3

using System;

using System.Collections.Generic;
using System.Text;
public static class Inputbox
{

static System.Windows.Forms.Form f;
static System.Windows.Forms.Label l;
static System.Windows.Forms.TextBox t; // Elementos necesarios
static System.Windows.Forms.Button b1;
static System.Windows.Forms.Button b2;
static string Valor;
/// <summary>
/// Objeto Esttico que muestra un pequeo dilogo para introducir datos
/// </summary>
/// <param name="title">Ttulo del dilogo</param>
/// <param name="prompt">Texto de informacin</param>
/// <param name="posicion">Posicin de inicio</param>
/// <returns>Devuelve la escrito en la caja de texto como string</return
s>
public static string Show(string title, string prompt, System.Windows.Fo
rms.FormStartPosition posicion)
{
f = new System.Windows.Forms.Form();
f.Text = title;
f.ShowIcon = false;
f.KeyPreview = true;
f.ShowInTaskbar = false;
f.MaximizeBox = false;
f.MinimizeBox = false;
f.Width = 200;
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
;
f.Height = 120;
f.StartPosition = posicion;
f.KeyPress += new System.Windows.Forms.KeyPressEventHandler(f_KeyPre
ss);
l = new System.Windows.Forms.Label();
l.AutoSize = true;
l.Text = prompt;
t = new System.Windows.Forms.TextBox();
t.Width = 182;
t.Top = 40;
b1 = new System.Windows.Forms.Button();
b1.Text = "Aceptar";
b1.Click += new EventHandler(b1_Click);
b2 = new System.Windows.Forms.Button();
b2.Text = "Cancelar";
b2.Click += new EventHandler(b2_Click);
f.Controls.Add(l);
f.Controls.Add(t);
f.Controls.Add(b1);
f.Controls.Add(b2);
l.Top = 10;
t.Left = 5;
t.Top = 30;
b1.Left = 5;
b1.Top = 60;
b2.Left = 112;
b2.Top = 60;
f.ShowDialog();
return (Valor);
}
static void f_KeyPress(object sender, System.Windows.Forms.KeyPressEvent
Args e)
{
switch (Convert.ToChar(e.KeyChar))
{
case ('\r'):
Acepta();
break; ;
case (''):
Cancela();
break; ;
}
}
static void b2_Click(object sender, EventArgs e)
{
Cancela();
}
static void b1_Click(object sender, EventArgs e)
{
Acepta();
}
private static string Val
{
get { return (Valor); }
set { Valor = value; }
}
private static void Acepta()
{
Val = t.Text;
f.Dispose();
}
private static void Cancela()
{
Val = null;
f.Dispose();
}
}

Вам также может понравиться