CLR: .Net / Mono / Boo / Otros CLR > C#
Inputbox En C#
(1/1)
Emperorxdevil:
Si has programado en VB, te acordarás del tipico Inputbox para pedir datos.
Pues bien, teniendo en cuenta que en C# no existe...
Por qué no implementarlo ?
Aqui les dejo el code de la clase Inputbox.
Es estática (no hace falta construir el objeto para usarla) y devuelve datos tipo string.
Su sintaxis es: Inputbox.Show(Titulo_Ventana,Titulo_Pregunta,Posicion)
Se usa asi:
--- Código: Text --- string n;n=Inputbox.Show("Inputbox en C#", "Cual es su nombre ? ", FormStartPosition.CenterScreen);Messagebox.Show(n);
Q lo disfruten !!
--- Código: Text --- public static class Inputbox { // Conserva esta cabecera // Emperorxdevil 2007 static Form f; static Label l; static TextBox t; // Elementos necesarios static Button b1; static Button b2; static string Valor; /// <summary> /// Objeto Estático que muestra un pequeño diálogo para introducir datos /// </summary> /// <param name="title">Título del diálogo</param> /// <param name="prompt">Texto de información</param> /// <param name="posicion">Posición de inicio</param> /// <returns>Devuelve la escrito en la caja de texto como string</returns> public static string Show(string title, string prompt, FormStartPosition posicion) { f = new Form(); f.Text = title; f.ShowIcon = false; f.Icon = null; f.KeyPreview=true; f.ShowInTaskbar = false; f.MaximizeBox = false; f.MinimizeBox = false; f.Width = 200; f.FormBorderStyle = FormBorderStyle.FixedDialog; f.Height = 120; f.StartPosition = posicion; f.KeyPress += new KeyPressEventHandler(f_KeyPress); l = new Label(); l.AutoSize = true; l.Text = prompt; t = new TextBox(); t.Width = 182; t.Top = 40; b1 = new Button(); b1.Text = "Aceptar"; b1.Click += new EventHandler(b1_Click); b2 = new 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, KeyPressEventArgs 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(); } }
Salu2
Neopro:
Muchas gracias, pero con los TextBox de C# me basta, es cosa de saber implementar, podrias intentarlo, es mas fácil de lo que crees.
:D
Emperorxdevil:
No se esactamente a lo que te refieres NeoPro, (ya se que cuestion de codear) pero lo que quise hacer fue 'emular' el Inputbox de VB y creo que lo consegui.
Salu2
jcsanchez1:
Tenias Ciertos errores en el codigo anterior aqui lo escribo corregido y 100% funcional
--- Citar ---using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dialogos
{
public static class Input
{
static Form _f;
static Label _l;
static TextBox _t;
static Button _b1;
static Button _b2;
static String _Cadena;
private static String cadena
{
get
{
return _Cadena;
}
set
{
_Cadena = value;
}
}
private static void Aceptar()
{
cadena = _t.Text;
_f.Dispose();
}
private static void Cancelar()
{
cadena=null;
_f.Dispose();
}
private static String getmensaje(String a)
{
_l = new Label();
_l.Visible = true;
_l.BackColor = System.Drawing.SystemColors.Control;
_l.AutoSize = false;
_l.Location = new System.Drawing.Point(9, 9);
_l.Size = new System.Drawing.Size(232, 50);
_l.Text = a;
return _l.Text;
}
/// <summary>
/// Objeto Estático que muestra un pequeño diálogo para introducir datos
/// </summary>
/// <param name="title">Título del diálogo</param>
/// <param name="prompt">Texto de información</param>
/// <param name="posicion">Posición de inicio</param>
/// <returns>Devuelve la escrito en la caja de texto como string</returns>
public static String Show(String titulo, String Mensaje, FormStartPosition posicion)
{
getmensaje(Mensaje);
_f = new Form();
_f.Text = titulo;
_f.ShowIcon = false;
_f.Icon = null;
_f.KeyPreview = true;
_f.ShowInTaskbar = false;
_f.MinimizeBox = false;
_f.MaximizeBox = false;
_f.Width = 250;
_f.FormBorderStyle = FormBorderStyle.FixedDialog;
_f.Height = 150;
_f.StartPosition = posicion;
_f.KeyPress += new KeyPressEventHandler(f_KeyPress);
_t = new TextBox();
_t.Left = 5;
_t.Width = 232;
_t.Top = 60;
_b1 = new Button();
_b1.Text = "Aceptar";
_b1.Click += new EventHandler(b1_Click);
_b2 = new 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 = new Label();
_l.Visible = true;
_l.BackColor = System.Drawing.SystemColors.Control;
_l.AutoSize = false;
_l.Location= new System.Drawing.Point(9,9);
_l.Size = new System.Drawing.Size(232,50);
_l.Text = Mensaje;
_l.Visible = true;
_l.AutoSize = false;
_b1.Left = 165;
_b1.Top = 90;
_b2.Left = 90;
_b2.Top = 90;
_f.ShowDialog();
return(_Cadena);
}
static void f_KeyPress(object sender, KeyPressEventArgs e)
{
switch (Convert.ToChar(e.KeyChar))
{
case ('r'):
Aceptar();
break;;
case(' '):
Cancelar();
break;;
}
}
static void b1_Click(object sender, EventArgs e)
{
if (_t.Text != "")
{
Aceptar();
}
else
{
MessageBox.Show("No ha ingresado un valor",Environment.MachineName);
}
}
static void b2_Click(object sender, EventArgs e)
{
Cancelar();
}
}
}
--- Fin de la cita ---
JaviMarciano:
por qué puede ser que me de error cada vez que hay esto:
"
Navegación
Ir a la versión completa