Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace Articulos.Cap04.Excepciones.Parte3
  6. {
  7.     public class AplicacionWinForms : Form
  8.     {
  9.         private TextBox txtNombreUsuario;
  10.         private TextBox txtContrasegnia;
  11.    
  12.          System.ComponentModel.Container components = null;
  13.         ///<summary>
  14.         /// Constructor de sin-argumentos
  15.         ///</summary>
  16.         public AplicacionWinForms()
  17.         {
  18.             InitializeComponents();
  19.         }
  20.         ///<summary>
  21.         /// Uso del patrón Disposable esencial para la liberación
  22.         /// de recursos del sistema.
  23.         ///</summary>
  24.         protected override void Dispose(bool disposing)
  25.         {
  26.             if (disposing)
  27.             {
  28.                 if (components != null)
  29.                 {
  30.                     components.Dispose();
  31.                 }
  32.             }
  33.             base.Dispose(disposing);
  34.         }
  35.        
  36.         protected void InitializeComponents ()
  37.         {
  38.             this.FormBorderStyle = FormBorderStyle.FixedSingle;
  39.             this.MaximizeBox = false;
  40.             this.Name = "FormLogin";
  41.             this.Size = new Size (250, 150);
  42.             this.StartPosition = FormStartPosition.CenterScreen;
  43.             this.Text = "Inicio de Sesión";
  44.            
  45.             CreacionBotonesInicioSesion();
  46.         }
  47.        
  48.         private void CreacionBotonesInicioSesion()
  49.         {
  50.             Label lblNombreUsuario = new Label ();
  51.             lblNombreUsuario.AutoSize = false;
  52.             lblNombreUsuario.Location = new Point (7, 15);
  53.             lblNombreUsuario.Name = "lblNombreUsuario";
  54.             lblNombreUsuario.Size = new Size (120, 23);
  55.             lblNombreUsuario.Text = "Nombre de usuario:";
  56.            
  57.             Label lblContrasegnia = new Label ();
  58.             lblContrasegnia.Location = new Point (7, 44);
  59.             lblContrasegnia.Name = "lblContrasegnia";
  60.             lblContrasegnia.Text = "Contraseña:";
  61.            
  62.             txtNombreUsuario = new TextBox();
  63.             txtNombreUsuario.Location = new Point (129, 13);
  64.             txtNombreUsuario.Name = "txtNombreUsuario";
  65.            
  66.             txtContrasegnia = new TextBox ();
  67.             txtContrasegnia.Location = new Point (128, 40);
  68.             txtContrasegnia.Name = "txtContrasegnia";
  69.             txtContrasegnia.PasswordChar = '*';
  70.            
  71.             Button btnIniciarSesion = new Button();
  72.             btnIniciarSesion.AutoSize = true;
  73.             btnIniciarSesion.Click += btnIniciarSesion_Click;
  74.             btnIniciarSesion.Location = new Point (89, 75);
  75.             btnIniciarSesion.Text = "Iniciar Sesión";
  76.            
  77.             this.Controls.Add (lblNombreUsuario);
  78.             this.Controls.Add (lblContrasegnia);
  79.             this.Controls.Add (txtNombreUsuario);
  80.             this.Controls.Add (txtContrasegnia);
  81.             this.Controls.Add (btnIniciarSesion);
  82.         }
  83.        
  84.         // Evento click para inicio de sesión:
  85.         private void btnIniciarSesion_Click(object sender, EventArgs e)
  86.         {
  87.             try
  88.             {
  89.                 if (txtNombreUsuario.Text.Equals ("root") && txtContrasegnia.Text.Equals ("Admin2k14"))
  90.                 {
  91.                     MessageBox.Show (this, "Ha iniciado sesión.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
  92.                 }
  93.                 else
  94.                 {
  95.                     // Lanza la excepción LoginFalloException:
  96.                     throw new LoginFalloException ("Las credenciales no son válidas.");
  97.                 }
  98.             }
  99.             // La excepción es atrapada como una instancia de Exception,
  100.             // para demostrar la compatiblidad con la jerarquía de herencia:
  101.             catch (Exception ex)
  102.             {
  103.                 MessageBox.Show (this, ex.Message, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
  104.             }
  105.         }
  106.        
  107.         public static void Main()
  108.         {
  109.             Application.EnableVisualStyles();
  110.             Application.Run (new AplicacionWinForms());
  111.         }
  112.     }
  113. }