how to override built in event handler in WPF c# - c#

In my project, I want to override Touchup Event Handler which is build in the WPF. I do not know how to override this event handler for my own use. Is that possible? Someone could give some examples, I do not get some references or example about it.

You can create a custom control and override the events. refer the below code i tried for TextBox control.
class TextBoxEx : TextBox
{
protected override void OnTouchUp(System.Windows.Input.TouchEventArgs e)
{
base.OnTouchUp(e);
}
protected override void OnTouchDown(System.Windows.Input.TouchEventArgs e)
{
base.OnTouchDown(e);
}
}

It is possible.
Let's say you want to override this TouchUp event (Completely override it). You will need to define a new function to handle this. Something like :
private void Custom_TouchUp(object sender, TouchEventArgs e)
{
// Do some stuff there
}
(It may not be TouchEventArgs, I haven't tried it, but it looks like it)
Then, in your xaml, in your object definition, you need to specify to the targeted object that it should use this function. If it's a combobox (for example), you'll have something like this :
<Combobox [...] TouchUp=Custom_TouchUp>
<Eventual parameters>
</Combobox>
And voila! Your object will use your new function.
Now, let's say you just want to alter a tiny bit the current event, then you can just override the OnTouchUp function that will be called when the event occurs. Something like this should do :
public override void OnTouchUp()
{
base.OnTouchUp();
// Other stuffs
}
But then, every element of the same class will act the same. So that's really useful when you want to define a new custom class

Related

Prevent event from firing in derived form

I have a base form and a derived form. Suppose I have only one button on my base form and derived form also uses same button.
I want to prevent the button click event from firing on derived form if the condition is not satisfied on base form click event. Could you please help how can I achieve this? Here is my code:
You can create a separate method that you mark as virtual then instead of adding an event handler to the derived form you just call the virtual method in the original form.
public Base : Form
{
public button1_Click(Object sender, EventArgs e)
{
DoSomethingOnClick();
}
protected virtual void DoSomethingOnClick()
{
// Do something here
}
}
Then in your derived form you can override DoSomethingOnClick to do what you need it to do:
public Derived : Base
{
private bool SpecificCondition = false;
protected override void DoSomethingOnClick()
{
if (SpecificCondition)
base.DoSomethingOnClick();
else
{
// Do something else here.
}
}
}
What this does is it means you only ever have one event handler for the click event so when it's clicked it calls DoSomethingOnClick() as this is a virtual method it can be overridden in the derived form and made to do something else. If you have a specific condition where you want the button click to only perform the base forms method then you can just call base.DoSomethingOnClick(), otherwise you insert your code to do something else.
This stops you having to worry about suppressing click events as there is only ever one.

In a Control extension, Override or addHandler?

I created an Extension for the DataGrid. I would like to know what is the better way to do thing in event.
Is it better to add an Handler like this :
class MyDataGrid : DataGrid {
public MyDataGrid() {
this.PreviewKeyDown+= MyDataGrid_PreviewKeyDown;
}
protected void MyDataGrid_PreviewKeyDown(/* args */) {/* do stuff */}
}
or with override like this:
class MyDataGrid : DataGrid {
public MyDataGrid() { }
protected override void OnPreviewKeyDown(/* args */) {/* do stuff */}
}
And if I let base.OnPreviewKeyDown(e); in the override, does it do the same as the eventHandler?
It probably does not matter either way. That said, you should override the method -- its whole purpose is to handle the event from within the control. If you add an event handler, then you are needlessly adding another method to the event's invocation list (ie, slight performance hit).
If you want to be strict/fanatical about performance, then you could also make your "MyDataGrid" class "sealed".
sealed class MyDataGrid : DataGrid { ... }
That allows compiler/runtime to determine that it doesn't need to check for any overrides of the virtual method "OnPreviewKeyDown" above your "MyDataGrid" class.

Pre_ and Post_ Events

I am creating an class library in C++/CLI to be used with C#, and as part of that library, i am offering up a customised version of System.Windows.Forms.Form and System.Windows.Forms.Control as System.Windows.Forms.HAForm and System.Windows.Forms.HAControl. I want to override OnPaint in a way that allows me to have OnPaint_Pre, OnPaint_Post AND OnPaint as i need to handle certain things every time before any painting is done, and handle some things AFTER painted has completed.
While this alone is simple enough, with this being a class in a library that is to be inherited from, i do not want to simply create an OnPaint in my class as this will be overwritten by the end developers OnPaint, and even if they do call base.OnPaint, the events will be fired out of order. i.e. OnPaint_Pre, my OnPaint, OnPaint_Post, sub classes OnPaint.
How would i create a class that inserts two events, one before the existing event, and one after?
I hope you don't mind me using C# syntax instead of C++/CLI...
You can make the HAForm/HAControl OnPaint override sealed, and create a new virtual function that derived classes can override. You can even use an intermediate class to give the new virtual function the same name:
public class HAControlBase : Control
{
protected virtual void OnPaintPre(PaintEventArgs e) { }
protected virtual void OnPaintPost(PaintEventArgs e) { }
internal virtual void OnPaintImpl(PaintEventArgs e) {
base.OnPaint(e);
}
protected sealed override OnPaint(PaintEventArgs e) {
OnPaintPre(e);
OnPaintImpl(e);
OnPaintPost(e);
}
}
public class HAControl : HAControlBase
{
internal sealed override void OnPaintImpl(PaintEventArgs e) {
OnPaint(e);
}
protected new virtual void OnPaint(PaintEventArgs e) {
base.OnPaintImpl(e);
}
}
Now, even if a derived class overrides HAControl.OnPaint, it will only be called after HAControlBase.OnPaint has already finished with OnPaintPre, there is no way to override Control.OnPaint (because that override is sealed) to call anything before OnPaintPre.

UserControl Load event not fired

I have WinForms application. My Form derived class has UserControl derived class.
I simply put several controls into one UserControl to simplify reuse. The Load event of UserControl is not fired. Do I have to set some property?
Try overriding the OnLoad() method in your UserControl. From MSDN:
The OnLoad method also allows derived
classes to handle the event without
attaching a delegate. This is the
preferred technique for handling the
event in a derived class.
protected override void OnLoad(EventArgs e)
{
//Your code to run on load goes here
// Call the base class OnLoad to ensure any delegate event handlers are still callled
base.OnLoad(e);
}
There wouldn't be any special properties you need to set for a UserControl's events to fire. You have one of 2 ways to subscribe to the event. In the Properties (property grid) select the events list...double-click at the Load property. All the necessary pieces of code will be put in place, and your cursor will be waiting for you at the proper method.
The second method is subscribing to the event like so:
public MyMainForm( )
{
InitializeComponents();
myUserControl.Load += new System.EventHandler(myUserControl_Load);
}
void myUserControl_Load(object sender, EventArgs e)
{
MessageBox.Show(((UserControl)sender).Name + " is loaded.");
}
One reason for the Load event to stop firing is when you have a parent of your control that does something like this
protected override void OnLoad(EventArgs e)
{
//do something
}
you always need to make sure to do this
protected override void OnLoad(EventArgs e)
{
//do something
base.OnLoad(e);
}

Casting and Types Question

I am bubbling events in my application and so therefore using the bubble events method. As this method handles all sorts of bubbled events their is a switch or if statement within it to determine what sort of event we're dealing with. I was wondering if I could get around this by creating different versions of the event args class. So let me explain, say I have two types of event that are handled differently called X and Y, I create new event args classes for these two events as they store different types of info.
public class EventsArgsX : EventsArgs
public class EventsArgsY : EventsArgs
then when I RaiseBubbleEvent from somewhere in my application I can pass either of the two event arg based types, so..
EventArgsX foox = new EventArgsX();
RaiseBubbleEvent(null,foox);
or
EventArgsY fooy = new EventArgsY();
RaiseBubbleEvent(null,fooy);
then the OnBubbleEvent method picks this up, who's signature is
override OnBubbleEvent(object source, EventArgs e)
now i cant overload this method as its overriden in the first place, so what I thought I could do was have another method with overloads in it to handle this, so
protected override OnBubbleEvent(object source, EventArgs e)
{
DoStuff(e);
}
private void DoStuff(EventArgsY args)
{}
private void DoStuff(EventArgsX args)
{}
but of course the problem is that EventArgs e in the OnBubbleEvent method is of type EventArgs at the time of calling. However we know its not. So how would i case it back to its actual type in order for the method call to work?
Many thanks, hope you can help me with this, its seems really easy like a might be missing something or that it just cant be done
any ideas??
It's simple:
protected override OnBubbleEvent(object source, EventArgs e)
{
if(e is EventArgsX)
DoStuff((EventArgsX)e);
else if(e is EventArgsY)
DoStuff((EventArgsY)e);
}
This, being KISS, is not very extensible. If you're planning on adding more event types, you can try double dispatch:
public abstract class EventArgsBase : EventArgs
{
public abstract void Bubble(IEventBubbler eb);
}
public interface IEventBubbler
{
Bubble(EventArgsX ex);
Bubble(EventArgsY ey);
}
public class EventArgsX : EventArgsBase
{
public virtual void Bubble(IEventBubbler eb)
{
eb.Bubble(this);
}
}
public class EventArgsY : EventArgsBase
{
public virtual void Bubble(IEventBubbler eb)
{
eb.Bubble(this);
}
}

Categories

Resources