Csharp/C Sharp/GUI Windows Form/User Control
Содержание
Creating an event for a component.
using System;
using System.Collections;
using System.ruponentModel;
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.ruboBox comboBox1;
public event ValueChangedEventHandler Changed;
public CompoundControl() {
this.ruboBox1 = new System.Windows.Forms.ruboBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.ruboBox1.DropDownWidth = 121;
this.ruboBox1.Items.AddRange(new object[] {"A","B","C","F","G","N"});
this.ruboBox1.Location = new System.Drawing.Point(24, 48);
this.ruboBox1.Size = new System.Drawing.Size(200, 21);
this.ruboBox1.Text = "comboBox1";
this.ruboBox1.SelectedIndexChanged += new System.EventHandler(this.OnSelectionIndexChange);
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Text = "Select An Entry";
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.ruboBox1,
this.label1});
this.Size = new System.Drawing.Size(240, 96);
this.ResumeLayout(false);
}
private void OnSelectionIndexChange(object sender,
System.EventArgs e) {
if (Changed != null)
Changed(this.ruboBox1.SelectedIndex);
}
}
public class Form1 : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null;
private CompoundControl compoundcomponent1 = null;
public Form1() {
this.rupoundcomponent1 = new CompoundControl();
this.rupoundcomponent1.Location = new System.Drawing.Point(24, 50);
this.rupoundcomponent1.Name = "compound1";
this.rupoundcomponent1.Size = new System.Drawing.Size(250, 100);
this.rupoundcomponent1.Changed += new CompoundControl.ValueChangedEventHandler(OnChanged);
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300, 300);
this.Controls.AddRange(new System.Windows.Forms.Control[]{this.rupoundcomponent1,});
}
private bool OnChanged(int nIndex) {
MessageBox.Show(this, "New Index!" + nIndex);
return true;
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
Define user control
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private ClockUserControl clockUserControl1;
public Form1() {
InitializeComponent();
}
private void InitializeComponent()
{
this.clockUserControl1 = new ClockUserControl();
this.SuspendLayout();
//
// clockUserControl1
//
this.clockUserControl1.Location = new System.Drawing.Point(12, 12);
this.clockUserControl1.Name = "clockUserControl1";
this.clockUserControl1.Size = new System.Drawing.Size(154, 74);
this.clockUserControl1.TabIndex = 0;
//
// Clock
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(178, 99);
this.Controls.Add(this.clockUserControl1);
this.Name = "Clock";
this.Text = "Clock";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
public class ClockUserControl : UserControl
{
private System.Windows.Forms.Label displayLabel;
private System.Windows.Forms.Timer clockTimer;
public ClockUserControl() {
InitializeComponent();
}
private void clockTimer_Tick(object sender, EventArgs e) {
displayLabel.Text = DateTime.Now.ToLongTimeString();
}
private void InitializeComponent()
{
this.displayLabel = new System.Windows.Forms.Label();
this.clockTimer = new System.Windows.Forms.Timer( new System.ruponentModel.Container() );
this.SuspendLayout();
//
// displayLabel
//
this.displayLabel.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.displayLabel.Font = new System.Drawing.Font( "Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
this.displayLabel.Location = new System.Drawing.Point( 4, 10 );
this.displayLabel.Name = "displayLabel";
this.displayLabel.Size = new System.Drawing.Size( 143, 52 );
this.displayLabel.TabIndex = 0;
this.displayLabel.Text = "12:55:55 AM";
this.displayLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// clockTimer
//
this.clockTimer.Enabled = true;
this.clockTimer.Interval = 1000;
this.clockTimer.Tick += new System.EventHandler( this.clockTimer_Tick );
//
// ClockUserControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add( this.displayLabel );
this.Name = "ClockUserControl";
this.Size = new System.Drawing.Size( 150, 72 );
this.ResumeLayout( false );
}
}
Draw a Custom Control
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
public class SimpleCustomControl : System.Windows.Forms.Control {
public SimpleCustomControl() {
InitializeComponent();
}
private void SimpleCustomControl_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Yellow, ClientRectangle);
g.DrawString("Hello, world", Font, Brushes.Black, 0, 0);
}
private void InitializeComponent() {
this.SuspendLayout();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleCustomControl_Paint);
this.ResumeLayout(false);
}
}
public class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.simpleCustomControl1 = new SimpleCustomControl();
this.SuspendLayout();
this.simpleCustomControl1.Location = new System.Drawing.Point(12, 12);
this.simpleCustomControl1.Size = new System.Drawing.Size(178, 110);
this.simpleCustomControl1.Text = "simpleCustomControl1";
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.simpleCustomControl1);
this.ResumeLayout(false);
}
private SimpleCustomControl simpleCustomControl1;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
subclass System.Windows.Forms.UserControl to create custom control
using System;
using System.Collections;
using System.ruponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
public class UserControl1 : System.Windows.Forms.UserControl {
private System.ruponentModel.Container components = null;
public UserControl1() {
this.Name = "UserControl1";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.OnPaint);
}
private void OnPaint(object sender,
System.Windows.Forms.PaintEventArgs e) {
e.Graphics.DrawString("Hello world", Font,
new SolidBrush(Color.Blue), ClientRectangle);
}
}
public class Form1 : System.Windows.Forms.Form {
private System.ruponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private UserControl1 control1;
public Form1() {
this.control1 = new UserControl1();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.control1.Location = new System.Drawing.Point(32, 48);
this.control1.Size = new System.Drawing.Size(80, 24);
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Size = new System.Drawing.Size(144, 24);
this.label1.Text = "Custom Control:";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.control1});
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
The read-only property control.
using System;
using System.Collections;
using System.ruponentModel;
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.ruponentModel.IContainer components;
private int fCounter = 0;
public int Counter {
get {
return fCounter;
}
}
public TimerCounter() {
this.ruponents = new System.ruponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.ruponents);
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.ruponentModel.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();
}
}