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);
}
Related
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.
I am working with a Web Form in ASP.NET and I want to understand that why can I omit the parameter in override Event Handler?
This is Event :
public event EventHandler Init;
This is Delegate to Hold Data(with two parameters) :
public delegate void EventHandler(object sender, EventArgs e);
Event Handler goes like :
protected override void OnInit(EventArgs e)
{
i++;
this.Load += Page_Load;
}
Adding parameter is not allowed in above method(handler) but at the same time we are restricted to pass parameter in below case
protected void Page_Load(object sender, EventArgs e)
{
i++;
Response.Write(i);
}
Note : I have disabled my Auto Event Wire-Up attribute in Directive.
Actually you are not subscribing to Init event. AutoEventWireUp make sure Page_Init method is called when Init Event is fired.
As you have disable it, then you have to manually hook up the method like this
protected override void OnPreInit(EventArgs e)
{
Init += new EventHandler(OnInit);
base.OnPreInit(); // make sure you call this
}
Reason you will not get compile time error is because there are two types of EventHandler delegate
EventHandler(object,EvnetArgs)
EvnetHandler(EventArgs)
The difference is that Page_Load really is an event handler whereas OnInit is a method that is defined in the base class and raises the Init event. So in the case of your OnInit method, you are overriding the method and not defining an event handler.
If you want to create an event handler for the Init event, you would also have to create a method with the following signature:
protected void Page_Init(object Sender, EventArgs e)
{
// ...
}
This only works if AutoEventWireUp is set to true. Otherwise, you need to register event handlers in your ASPX page or the codebehind file. As the Init event is raised very in the page lifecycle, the event handler needs to be registered at the beginning of the request. In the special case of Init it is very common to override OnInit instead of defining an event handler. Important to note though, if you override OnInit (or one of the other On* methods) you should always call the base version in order to allow for the ASP.NET framework to do its stuff:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// initialization code
}
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
I am able to hook the event and also allowed to override load and init.., events in my aspx codebehind file. I have following questions.
public partial class Default : System.Web.UI.Page
{
public Default()
{
this.Init += Default_Init;
this.Load+=Default_Load;
}
protected void Default_Load(object sender, EventArgs e)
{ }
protected void Default_Init(object sender, EventArgs e)
{ }
protected override void OnLoad(EventArgs e)
{ }
protected override void OnInit(EventArgs e)
{ }
}
Qs:
Why such option(both virtual method and event handler) is exist in Page class?
When override methods got executed, my event handlers(Default_Load and Default_Init) are not executed. what is the reason behind?
Should i call base.OnLoad(e); in overridden OnLoad method? Why should/shouldn't I?
By default, OnInit and OnLoad are not handlers of Init and Load events. They are used to Fire Init and Load events.
When you override, you change the behavior of these methods thus, events are not fired by OnInit and OnLoad methods (unless you explicitly write base.OnInit etc.)
If you change the behavior of OnLoad and do not call base method, then you may break the page lifecycle of asp.net application. For instance, Load event will not be called.
Normally, the method OnXxx raises the event named Xxx. This is also the case in a System.Web.UI.Control like Page.
If you do:
protected override void OnLoad(EventArgs e)
{
// nothing here!
}
you change the behavior of OnLoad so that it now does nothing. Therefore, most probably, no-one will ever raise ("fire") the Load event. So its event handler is never run.
If instead you do:
protected override void OnLoad(EventArgs e)
{
// maybe add some code of your own here ...
base.OnLoad(e);
// ... or here
}
then the base call will make sure the method still raises the event.
You can only raise the event explicitly, i.e. invoke the underlying delegate, if you're in the class where the event was declared. So that's not a possibility for you. The base class where Load is defined does that like this:
// inside same class where 'Load' event is defined:
protected virtual void OnLoad(EventArgs e)
{
// maybe they put some other code here ...
var loadDelegate = Load; // gets the current underlying delegate of the event
if (loadDelegate != null)
loadDelegate(this, e);
// ... or here
}
But in most situation you shouldn't override any method at all. Just add to Load when you want some code to run when the Load event fires.
This seems fairly simple, but I haven't been able to accomplish it. I have a BaseForm class that every form in my application inherits.
I simply want to execute a line of code every time a key is pressed in any form inheriting the BaseForm. In my BaseForm I've attempted the following with no luck:
public class BaseForm : Form
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
//Perform action
}
}
public class MainForm : BaseForm
{
//All of my main form code goes here.
}
Any help would be greatly appreciated! Thanks in advance
Probably you need to set KeyPreview of your base form to true for it to be able catch all the key presses from any control. Consider doing this in the form designer or in the base class constructor. I guess you've got some editors (a textbox, for example) on your derived forms, so you need the KeyPreview to be set to true for the base form to be able to catch those key presses.
You can either override the OnKeyPress method (as in your question) or add an event handler for the KeyPress event in the base form.
public class BaseForm : Form
{
public BaseForm()
{
this.KeyPreview = true; //it's necessary!!
//or just override the OnKeyPress method instead
this.KeyPress += new KeyPressEventHandler(BaseForm_KeyPress);
}
private void BaseForm_KeyPress(object sender, KeyPressEventArgs e)
{
//do your action
}
}
What you have done so far is correct. If your OnKeyPress is not being executed then you have something wrong - do you have an OnKeyDown that is interfering?
What you do next is to have the same override in your derived forms:
public class MainForm : BaseForm
{
//All of my main form code goes here.
protected override void OnKeyPress(KeyPressEventArgs e)
{
//do whatever action this form needs to, if any
base.OnKeyPress(e);
}
}
See that call to the base.OnKeyPress? That executes that line of code you have in the base. Note that you can put that call anywhere within the function, it might be more appropriate to have it at the start before the form specific code.