實作這篇文章時最大的收穫是,原來要 override 時,VS 有快取的方式可以使用,之前看到範例,都會覺得怎麼這麼利害,Method 內的 Object Sender 和 EventArgs e 這兩個參數要怎麼知道,尤其是 EventArgs,不會都必須查詢 MSDN,然後再複制貼上到 VS 內吧,Orz ~~
- ctlClockLib
- 結合控制項及元件到使用者控制項
- 加入屬性至控制項
- 寫 Code 來自訂功能
namespace ctlClockLib
{
public partial class ctlClock: UserControl
{
private Color colFColor;
private Color colBColor;
// Declares the name and type of the property.
public Color ClockBackColor
// Retrieves the value of the private variable colBColor.
{
get{return colBColor;}
// Stores the selected value in the private variable colBColor, and
// updates the backcolor of the label control lblDisplay.
set
{
colBColor = value;
lblDisplay.BackColor = colBColor;
}
}
// Provides a similar set of instructions for the forecolor.
public Color ClockForeColor
{
get{return colFColor;}
set
{
colFColor = value;
lblDisplay.ForeColor = colFColor;
}
}
public ctlClock()
{
InitializeComponent();
}
protected virtual void timer1_Tick(object sender, EventArgs e)
{
// Causes the label to display the current time
lblDisplay.Text = DateTime.Now.ToString();
}
}
}
- ctlAlarmClock
- 繼承
- 覆寫(Override)
namespace ctlClockLib
{
public partial class ctlAlarmClock : ctlClockLib.ctlClock
{
public ctlAlarmClock()
{
InitializeComponent();
}
private DateTime dteAlarmTime;
private bool blnAlarmSet;
private bool blnColorTicker;
// These properties will be declared as public to allow future
// developers to access them.
public DateTime AlarmTime
{
get { return dteAlarmTime; }
set { dteAlarmTime = value; }
}
public bool AlarmSet
{
get { return blnAlarmSet; }
set { blnAlarmSet = value; }
}
protected override void timer1_Tick(object sender, EventArgs e)
{
// Calls the Timer1_Tick method of ctlClock.
base.timer1_Tick(sender, e);
// Checks to see if the Alarm is set.
if (blnAlarmSet == false) return;
// If the date, hour and minute of the alarm time are the same as
// now, flash!
if (AlarmTime.Date == DateTime.Now.Date && AlarmTime.Hour ==
DateTime.Now.Hour && AlarmTime.Minute == DateTime.Now.Minute)
{
// Makes lblAlarmVisible, and changes the backcolor based on
// the value of blnColorTicker. The backcolor of the label
// will flash once per tick of the clock.
lblAlarm.Visible = true;
if (blnColorTicker == false)
{
lblAlarm.BackColor = Color.Red;
blnColorTicker = true;
}
else
{
lblAlarm.BackColor = Color.Blue;
blnColorTicker = false;
}
}
else
{
// Once the alarm has sounded for a minute, the label is made
// invisible again.
lblAlarm.Visible = false;
}
}
private void btnOff_Click(object sender, EventArgs e)
{
AlarmSet = false;
lblAlarm.Visible = false;
}
private void dtpTest_ValueChanged(object sender, EventArgs e)
{
AlarmTime = dtpTest.Value;
AlarmSet = true;
lblTest.Text = string.Format("Alarm Time is {0}", AlarmTime.ToShortTimeString());
}
}
}
完成 ctlClockLib Project 後,在同一個 Solution 內再開一個 Test Project 就可以看見工具箱內出現自訂控制項把自訂控制項拖曳進入 Test Form 並執行,就可以看見效果
跨 Solution 使用,必須要把 ctlClockLib Project build 成一個 dll 檔案,該 dll 檔案會根據 Solution 的方案組態,產生在 solution 內的 bin\release 或 bin\dedug 中
新開一個新 Solution UC 並在工具箱內先建立一個工具箱索引標籤 UC
UC 索引標籤 => 滑鼠右鍵 => 選擇項目
.NET Framework 元件 => 瀏覽 button
選擇 ctlClockLib Project 後產生的 DLL 檔案,就可以在 UC 索引標籤內看見自訂控件
另一種作法是把 ctlClockLib dll 直接拖曳進 UC 索引標籤內,也可以作到
沒有留言:
張貼留言