日期:2014-05-19  浏览次数:20943 次

封装 TButton
要求封装一个按钮:
功能:在click之后按钮变enabled   变成   Flase   执行结束   变回来
同时要增加一个属性就是要不要这个功能:
这个功能为True,则实现上面的功能
为False的时候,则和普通的按钮一样

小弟,不懂,请指教!
贴点代码最好拉!
谢谢

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace lw.CSharp.Test
{
public class WrapperButton : Button
{
public delegate void FeedbackEventHandler(object sender, FeedbackEventArgs e);
public event FeedbackEventHandler OnFeedback;

private bool _waitFeedback = true;

public WrapperButton()
: base()
{
}
/// <summary>
/// Gets or Sets the value to indicate whether the Button wait for Feedback or not
/// </summary>
[DefaultValue(true),
Description( "Gets or Sets the value to indicate whether the Button wait for feedback or not ")]
public bool WaitFeedback
{
get { return _waitFeedback; }
set { _waitFeedback = value; }
}

protected override void OnClick(EventArgs e)
{
if (_waitFeedback) this.Enabled = false;
base.OnClick(e);
FeedbackEventArgs fe = new FeedbackEventArgs();
fe.e = e;
fe.WaitFeedback = _waitFeedback;
if (OnFeedback != null) OnFeedback(this, fe);
}
}

public class FeedbackEventArgs
{
private bool _waitFeedback;
private EventArgs _e;

public FeedbackEventArgs() { }

public bool WaitFeedback
{
get { return _waitFeedback; }
set { _waitFeedback = value; }
}

public EventArgs e
{
get { return _e; }
set { _e = value; }
}
}
}


Test Code:

private void wrapperButton1_Click(object sender, EventArgs e)
{
wrapperButton1.OnFeedback += new WrapperButton.FeedbackEventHandler(wrapperButton1_OnFeedback);
MessageBox.Show( "Testing... ");
}

void wrapperButton1_OnFeedback(object sender, FeedbackEventArgs e)
{
wrapperButton1.Enabled = true;
}

------解决方案--------------------
Test Code作下修改:

void wrapperButton1_OnFeedback(object sender, FeedbackEventArgs e)
{
if (e.WaitFeedback)
{
wrapperButton1.Enabled = true;
}
}
------解决方案--------------------
基本同意LS...

如果只是实现这样的简单功能,不用那么多代码的...

protected override void OnClick(EventArgs e)
{
if (_waitFeedback) {this.Enabled = false};
base.OnClick(e);
if (_waitFeedback) {this.Enabled = true};
}