[英]How to create custom control with buttons and how to add event button click event in silverlight for windows mobile 7
[英]How to create a Custom Image Button, made of three individual buttons and add event handler
我要制作一個“ Custom Image Button
,該Custom Image Button
應由三個帶有附加click event
按鈕組成。
我試圖制作一個“ Customize button
但無法附加單獨的event handler
。
對於每個按鈕:
class MulitButtons : UserControl
{
public Color bckColor1 = Color.Blue;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
RectangleF recF1 = new RectangleF(0, 0, 100, 40);
RectangleF recF2 = new RectangleF(100, 0, 100, 40);
RectangleF recF3 = new RectangleF(200, 0, 100, 40);
RectangleF[] arrRecF = { recF1, recF2, recF3 };
Pen pen = new Pen(Color.Black, 2);
int fontHeight = 10;
Font font = new Font("Arial", fontHeight);
SolidBrush brush = new SolidBrush(bckColor1);
SolidBrush textBrush = new SolidBrush(Color.Black);
graphics.DrawRectangles(pen, arrRecF);
graphics.DrawString(Text, font, textBrush, 10, 10);
}
}
添加事件處理程序:
MulitButtons objMltBtn = new MulitButtons();
EventHandler handler= new EventHandler(but1_Click);
objMltBtn.Click += handler;
請幫幫我。 謝謝。
據我了解,您想發布內部按鈕的Click
事件。
解決方案1
這只是發布內部點擊事件。
public event EventHandler Button1Click
{
add { button1.Click += value; }
remove { button1.Click -= value; }
}
解決方案2
如果要控制調用,請執行以下操作:
public MultiButtons()
{
InitializeComponents();
button1.Click += ButtonClick;
button2.Click += ButtonClick;
button3.Click += ButtonClick;
}
// this handles all of your clicks
private void ButtonClick(object sender, EventArgs e)
{
if (sender == button1)
OnButton1Click(EventArgs.Empty);
// TODO: the other buttons...
}
public event EventHandler Button1Click;
protected virtual void OnButton1Click(EventArgs e)
{
var handler = Button1Click;
if (handler != null)
handler(this, e);
else
// you can call some default action if there is no event subscription
DefaultButton1Click();
}
更新:我剛剛看到您的評論:
我需要一個具有三個部分的自定義圖像按鈕,然后在每個部分上單擊以調用一個將執行某些操作的事件。
只需創建三個彼此相鄰的單獨按鈕,然后在“ Paint
繪制它們,就好像它們是單個按鈕一樣。 然后,您可以使用上述解決方案之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.