The read-only property control. : User Control « GUI Windows Form « C# / C Sharp






The read-only property control.

 


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


public class TimerCounter : System.Windows.Forms.UserControl {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;
    private int fCounter = 0;
    public int Counter {
        get {
            return fCounter;
        }
    }
    public TimerCounter() {
        this.components = new System.ComponentModel.Container();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.OnTick);
        this.Name = "TimerCounter";
    }
    private void OnTick(object sender, System.EventArgs e) {
        fCounter++;
    }
}
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
    private System.Windows.Forms.Label CounterLabel;
    private System.Windows.Forms.Button Update;
    private System.ComponentModel.Container components = null;
    private TimerCounter counter;

    public Form1() {
        counter = new TimerCounter();
        this.CounterLabel = new System.Windows.Forms.Label();
        this.Update = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(32, 24);
        this.label1.Size = new System.Drawing.Size(48, 23);
        this.label1.Text = "Counter: ";
        this.CounterLabel.Location = new System.Drawing.Point(96, 24);
        this.CounterLabel.Size = new System.Drawing.Size(32, 23);
        this.Update.Location = new System.Drawing.Point(80, 72);
        this.Update.Text = "Update";
        this.Update.Click += new System.EventHandler(this.Update_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(224, 133);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.Update,this.CounterLabel,this.label1});
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void Update_Click(object sender, System.EventArgs e) {
        CounterLabel.Text = counter.Counter.ToString();
    }
}

 








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.Creating an event for a component.