Created
July 11, 2014 18:00
-
-
Save Fhernd/ef49346238bbc1504828 to your computer and use it in GitHub Desktop.
Creación de aplicación WinForms para probar la excepción LoginFalloException.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Windows.Forms; | |
namespace Articulos.Cap04.Excepciones.Parte3 | |
{ | |
public class AplicacionWinForms : Form | |
{ | |
private TextBox txtNombreUsuario; | |
private TextBox txtContrasegnia; | |
System.ComponentModel.Container components = null; | |
///<summary> | |
/// Constructor de sin-argumentos | |
///</summary> | |
public AplicacionWinForms() | |
{ | |
InitializeComponents(); | |
} | |
///<summary> | |
/// Uso del patrón Disposable esencial para la liberación | |
/// de recursos del sistema. | |
///</summary> | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
if (components != null) | |
{ | |
components.Dispose(); | |
} | |
} | |
base.Dispose(disposing); | |
} | |
protected void InitializeComponents () | |
{ | |
this.FormBorderStyle = FormBorderStyle.FixedSingle; | |
this.MaximizeBox = false; | |
this.Name = "FormLogin"; | |
this.Size = new Size (250, 150); | |
this.StartPosition = FormStartPosition.CenterScreen; | |
this.Text = "Inicio de Sesión"; | |
CreacionBotonesInicioSesion(); | |
} | |
private void CreacionBotonesInicioSesion() | |
{ | |
Label lblNombreUsuario = new Label (); | |
lblNombreUsuario.AutoSize = false; | |
lblNombreUsuario.Location = new Point (7, 15); | |
lblNombreUsuario.Name = "lblNombreUsuario"; | |
lblNombreUsuario.Size = new Size (120, 23); | |
lblNombreUsuario.Text = "Nombre de usuario:"; | |
Label lblContrasegnia = new Label (); | |
lblContrasegnia.Location = new Point (7, 44); | |
lblContrasegnia.Name = "lblContrasegnia"; | |
lblContrasegnia.Text = "Contraseña:"; | |
txtNombreUsuario = new TextBox(); | |
txtNombreUsuario.Location = new Point (129, 13); | |
txtNombreUsuario.Name = "txtNombreUsuario"; | |
txtContrasegnia = new TextBox (); | |
txtContrasegnia.Location = new Point (128, 40); | |
txtContrasegnia.Name = "txtContrasegnia"; | |
txtContrasegnia.PasswordChar = '*'; | |
Button btnIniciarSesion = new Button(); | |
btnIniciarSesion.AutoSize = true; | |
btnIniciarSesion.Click += btnIniciarSesion_Click; | |
btnIniciarSesion.Location = new Point (89, 75); | |
btnIniciarSesion.Text = "Iniciar Sesión"; | |
this.Controls.Add (lblNombreUsuario); | |
this.Controls.Add (lblContrasegnia); | |
this.Controls.Add (txtNombreUsuario); | |
this.Controls.Add (txtContrasegnia); | |
this.Controls.Add (btnIniciarSesion); | |
} | |
// Evento click para inicio de sesión: | |
private void btnIniciarSesion_Click(object sender, EventArgs e) | |
{ | |
try | |
{ | |
if (txtNombreUsuario.Text.Equals ("root") && txtContrasegnia.Text.Equals ("Admin2k14")) | |
{ | |
MessageBox.Show (this, "Ha iniciado sesión.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information); | |
} | |
else | |
{ | |
// Lanza la excepción LoginFalloException: | |
throw new LoginFalloException ("Las credenciales no son válidas."); | |
} | |
} | |
// La excepción es atrapada como una instancia de Exception, | |
// para demostrar la compatiblidad con la jerarquía de herencia: | |
catch (Exception ex) | |
{ | |
MessageBox.Show (this, ex.Message, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
} | |
} | |
public static void Main() | |
{ | |
Application.EnableVisualStyles(); | |
Application.Run (new AplicacionWinForms()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment