开发者

How to add custom property and events to a control say textbox or button

I want to add a custom property to a button in window form. Currently i am using following code to create my logic. but i want to create an enum value for a button control.

btnPartyDetails.Text = "View";
{}
btnPartyDetails.Text = "Add";
{}    
btnPartyDetails.Text = "Delete";
{}
btnPartyDetails.Text = "Edit";
{}

I want to perform some action based on these values and i want to make a custom property for button so that i can use enum instead of using text match.

btnPartyDetails.ActionType= ActionType.View;
{}
btnPartyDetails.ActionType= ActionType.Add;
{}    
btn开发者_开发百科PartyDetails.ActionType= ActionType.Delete;
{}
btnPartyDetails.ActionType= ActionType.Edit;
{}

I want to do something like this, where ActionType will be my enum.

I also want to create custom event based on the value set. How can i do this ?


You will have to create a custom control and then inherit the button class. Then create your custom properties and / or events.

Check this or this out from MSDN


You can inherit from the control you want and extend it however you wish. AFAIK none of the controls are sealed classes in winform. So you could add additional properties and events. Something like:

public class MyTextBox : System.Windows.Forms.TextBox {

public string MetaMessage {get;set;}

public event SomeCoolEventHandler CoolEvent;
public delegate SomeCoolEventHandler(object sender, CoolEventArgs args);
}

public class CoolEventArgs: EventArgs{

....
}


You need to derive a new class from button

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜