Vous êtes sur la page 1sur 7

Tao User Control

1. Tao new project

Chon Windows Control Library , ten la MyControl1

1 progressBar : Bar

1 Label : lblProgress

Nhap noi dung cua UserControl

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

namespace MyControl1
{
public partial class MyProgress : UserControl
{
public MyProgress()
{
InitializeComponent();
}
// Tao cac properties moi cho doi tuong thuoc MyProgress
public int Value
{
get { return Bar.Value; }
set
{
Bar.Value = value;
UpdateLabel();
}
}
public int Maximum
{
get { return Bar.Maximum; }
set { Bar.Maximum = value; }
}
public int Step
{
get { return Bar.Step; }
set { Bar.Step = value; }
}
// Tao cac methods
public void PerformStep()
{
Bar.PerformStep();
UpdateLabel();
}
public void UpdateLabel()
{
decimal d=(Math.Round((decimal)(Bar.Value*100)/Bar.Maximum));
lblProgress.Text = d.ToString();
lblProgress.Text += " % Done";
}

}
}

2. Bien dich MyControl1


3. Tao strongname key : MyControl1.snk
Them vao trong AssemblyInfo.cs
[assembly: AssemblyKeyName("MyControl1")]
[assembly: AssemblyKeyFile("..//..//MyControl1.snk")]

4. Dang ky thanh shared assembly

D:\MyControls\ProgressControl>cd D:\MyControls\MyControl1

D:\MyControls\MyControl1>sn -k MyControl1.snk

Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42


Copyright (c) Microsoft Corporation. All rights reserved.

Key pair written to MyControl1.snk

D:\MyControls\MyControl1>cd bin\debug

D:\MyControls\MyControl1\bin\Debug>gacutil -i MyControl1.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.

Assembly successfully added to the cache

D:\MyControls\MyControl1\bin\Debug>
5. Dung regedit tao chi dan den Mycontrol1
6. Tao windows application dung MyControl1
Tham chieu den MyControl1
Dua MyControl1 vao trong toolbox
7. Tao form
8. Viet lenh de su dung MyControl1

private void cmdStart_Click(object sender, EventArgs e)


{
status.Value = 0;
status.Maximum = 20;
status.Step = 1;
tmrIncrementBar.Enabled = true;
}

private void tmrIncrementBar_Tick(object sender, EventArgs e)


{
status.PerformStep();
if (status.Maximum == status.Value)
tmrIncrementBar.Enabled = false;
}
}

Vous aimerez peut-être aussi