Creating an event for a component. : User Control « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » GUI Windows Form » User Control 




Creating an event for a component.
 


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


public class CompoundControl : System.Windows.Forms.UserControl {
    public delegate Boolean ValueChangedEventHandler(int nValue);
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ComboBox comboBox1;
    public event ValueChangedEventHandler Changed;

    public CompoundControl() {
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.comboBox1.DropDownWidth = 121;
        this.comboBox1.Items.AddRange(new object[] {"A","B","C","F","G","N"});
        this.comboBox1.Location = new System.Drawing.Point(2448);
        this.comboBox1.Size = new System.Drawing.Size(20021);
        this.comboBox1.Text = "comboBox1";
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.OnSelectionIndexChange);
        this.label1.Location = new System.Drawing.Point(1624);
        this.label1.Text = "Select An Entry";
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
        this.comboBox1,
        this.label1});
        this.Size = new System.Drawing.Size(24096);
        this.ResumeLayout(false);
    }
    private void OnSelectionIndexChange(object sender,
        System.EventArgs e) {
        if (Changed != null)
            Changed(this.comboBox1.SelectedIndex);
    }
}


public class Form1 : System.Windows.Forms.Form {
    private System.ComponentModel.Container components = null;
    private CompoundControl compoundcomponent1 = null;

    public Form1() {
        this.compoundcomponent1 = new CompoundControl();
        this.compoundcomponent1.Location = new System.Drawing.Point(2450);
        this.compoundcomponent1.Name = "compound1";
        this.compoundcomponent1.Size = new System.Drawing.Size(250100);
        this.compoundcomponent1.Changed += new CompoundControl.ValueChangedEventHandler(OnChanged);
        this.components = new System.ComponentModel.Container();
        this.Size = new System.Drawing.Size(300300);
        this.Controls.AddRange(new System.Windows.Forms.Control[]{this.compoundcomponent1,});
    }
    private bool OnChanged(int nIndex) {
        MessageBox.Show(this, "New Index!" + nIndex);
        return true;
    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
}

 














Related examples in the same category
1.subclass System.Windows.Forms.UserControl to create custom control
2.Draw a Custom Control
3.Define user controlDefine user control
4.The read-only property control.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.