Ver Mensaje Individual
  #17 (permalink)  
Antiguo 28/02/2012, 13:10
alexg88
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: Pasar variable de WindowsForm a Control

Yo he probado y funciona perfectamente. Lo único raro es como insertas el control de usuario dentro del formulario, porque en tu código instancias un control de usuario pero no lo añades al formulario.

Si lo haces con el diseñador de windows Forms, tendrás una instancia al control de usuario, que será sobre el que tengas que poner el evento.

Te dejo mi código:

Código C#:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace PruebaControles
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             controlDeUsuario1.Control += new ControlDeUsuario.HandlerControl(MetodoDelEvento);
  22.         }
  23.  
  24.         void MetodoDelEvento(object sender, EventArgs e)
  25.         {
  26.             textBox1.Text = "SI";
  27.         }
  28.  
  29.     }
  30. }

Código C#:
Ver original
  1. namespace PruebaControles
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// Variable del diseñador requerida.
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.  
  10.         /// <summary>
  11.         /// Limpiar los recursos que se estén utilizando.
  12.         /// </summary>
  13.         /// <param name="disposing">true si los recursos administrados se deben eliminar; false en caso contrario, false.</param>
  14.         protected override void Dispose(bool disposing)
  15.         {
  16.             if (disposing && (components != null))
  17.             {
  18.                 components.Dispose();
  19.             }
  20.             base.Dispose(disposing);
  21.         }
  22.  
  23.         #region Código generado por el Diseñador de Windows Forms
  24.  
  25.         /// <summary>
  26.         /// Método necesario para admitir el Diseñador. No se puede modificar
  27.         /// el contenido del método con el editor de código.
  28.         /// </summary>
  29.         private void InitializeComponent()
  30.         {
  31.             this.controlDeUsuario1 = new PruebaControles.ControlDeUsuario();
  32.             this.textBox1 = new System.Windows.Forms.TextBox();
  33.             this.SuspendLayout();
  34.             //
  35.             // controlDeUsuario1
  36.             //
  37.             this.controlDeUsuario1.Location = new System.Drawing.Point(58, 52);
  38.             this.controlDeUsuario1.Name = "controlDeUsuario1";
  39.             this.controlDeUsuario1.Size = new System.Drawing.Size(150, 150);
  40.             this.controlDeUsuario1.TabIndex = 0;
  41.             //
  42.             // textBox1
  43.             //
  44.             this.textBox1.Location = new System.Drawing.Point(97, 83);
  45.             this.textBox1.Name = "textBox1";
  46.             this.textBox1.Size = new System.Drawing.Size(100, 20);
  47.             this.textBox1.TabIndex = 1;
  48.             //
  49.             // Form1
  50.             //
  51.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  52.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  53.             this.ClientSize = new System.Drawing.Size(284, 262);
  54.             this.Controls.Add(this.textBox1);
  55.             this.Controls.Add(this.controlDeUsuario1);
  56.             this.Name = "Form1";
  57.             this.Text = "Form1";
  58.             this.Load += new System.EventHandler(this.Form1_Load);
  59.             this.ResumeLayout(false);
  60.             this.PerformLayout();
  61.  
  62.         }
  63.  
  64.         #endregion
  65.  
  66.         private ControlDeUsuario controlDeUsuario1;
  67.         private System.Windows.Forms.TextBox textBox1;
  68.     }
  69. }

Código C#:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace PruebaControles
  11. {
  12.     public partial class ControlDeUsuario : UserControl
  13.     {
  14.         public delegate void HandlerControl(object sender, EventArgs e);
  15.         public event HandlerControl Control;
  16.  
  17.         public ControlDeUsuario()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             if (Control != null)
  25.                 Control(this, e);
  26.  
  27.         }
  28.     }
  29. }

Código C#:
Ver original
  1. namespace PruebaControles
  2. {
  3.     partial class ControlDeUsuario
  4.     {
  5.         /// <summary>
  6.         /// Variable del diseñador requerida.
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.  
  10.         /// <summary>
  11.         /// Limpiar los recursos que se estén utilizando.
  12.         /// </summary>
  13.         /// <param name="disposing">true si los recursos administrados se deben eliminar; false en caso contrario, false.</param>
  14.         protected override void Dispose(bool disposing)
  15.         {
  16.             if (disposing && (components != null))
  17.             {
  18.                 components.Dispose();
  19.             }
  20.             base.Dispose(disposing);
  21.         }
  22.  
  23.         #region Código generado por el Diseñador de componentes
  24.  
  25.         /// <summary>
  26.         /// Método necesario para admitir el Diseñador. No se puede modificar
  27.         /// el contenido del método con el editor de código.
  28.         /// </summary>
  29.         private void InitializeComponent()
  30.         {
  31.             this.button1 = new System.Windows.Forms.Button();
  32.             this.SuspendLayout();
  33.             //
  34.             // button1
  35.             //
  36.             this.button1.Location = new System.Drawing.Point(40, 96);
  37.             this.button1.Name = "button1";
  38.             this.button1.Size = new System.Drawing.Size(75, 23);
  39.             this.button1.TabIndex = 0;
  40.             this.button1.Text = "button1";
  41.             this.button1.UseVisualStyleBackColor = true;
  42.             this.button1.Click += new System.EventHandler(this.button1_Click);
  43.             //
  44.             // ControlDeUsuario
  45.             //
  46.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  47.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  48.             this.Controls.Add(this.button1);
  49.             this.Name = "ControlDeUsuario";
  50.             this.ResumeLayout(false);
  51.  
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         private System.Windows.Forms.Button button1;
  57.     }
  58. }