Event handling with winform component - c#

I have a class StripButton which inherits from Label. I have overriden the onClick method. The user of this class also assigns events to the onClick eventhandler. I have noticed that the overriden method gets fired last. I want the overriden method to fire first. How can I do that?

The Click handler of a StripButton on your Form will fire when the base.OnClick method in the OnClick handler is called in your UserControl. So if you want your UserControl's OnClick code to run before the Click handler code runs on your Form, call base.OnClick last in your UserControl's OnClick handler. Conversely, if you want your Form's Click code to run after your UserControl's OnClick code, call base.OnClick first in your UserControl's OnClick handler.
It's probably easiest to illustrate with the simplest example:
// Your UserControl.
class StripButton : Label
{
protected override void OnClick(EventArgs e) {
Console.WriteLine("This runs before the Click handler on the parent form.");
base.OnClick(e);
Console.WriteLine("This runs after the Click handler on the parent form.");
}
}
// On your form.
private void stripButton1_Click(object sender, EventArgs e) {
Console.WriteLine("stripButton1_Click on form");
}
If you run this you'll see the following in the Output Window:
This runs before the Click handler on the parent form.
stripButton1_Click on form
This runs after the Click handler on the parent form.
Hopefully that answers your question.

Related

Custom UserControl's click event does not fire

So I created a custom UserControl just out of a PictureBox and a RadioButton, something like the following:
public partial class Foo : UserControl
{
//some declared properties for the designer...
}
Now I added this object to a Form and subscribed to it's Click() event.
private void customAddedContr_Click(object sender, EventArgs e) { }
But just clicking on this red marked part fires the Click() event.
So just by clicking on the UserControl itself, so when a click is performed on the PictureBox or RadioButton this Click() event does not get fired. I thought that the Click event for this new UserControl gets fired for each click aiming to this Window Handle.
So do I need to bind the PictureBox's and RadioButton's Click() event to the UserControl's Click() event earlier on or what do I oversee?
Edit:
For understanding purpose, here is a colored picture of the CustomControl.
The top dark grey part is the PictureBox. The blue one is the UserControl itself on which I placed the other controls. The light grey bottom is the RadioButton. So just clicking on the blue part (UserControl) fires the Click() event.
I would recommend exposing the picturebox and radio buttons click events separately.
You raise your own events for the radio and picturebox at the usercontrol level and inside each click even you would raise the controls events. then you handle those click events outside the control and respond accordingly.
public partial class Foo : UserControl
{
public event EventHandler RadioButtonClicked;
public event EventHandler PictureBoxClicked:
}
then in the picturebox or button click event inside the control you raise the individual event at the control level
private void PictureBox1_Click(object sender, EventArgs e)
{
if (PictureBoxClicked != null)
PictureBoxClicked(sender, e);
}

Call a method in another class from a control within a control?

I have a custom control created, comprised of a bunch of text boxes, 1 button and radio buttons.
I then have a "parent" control which only has this 1 custom control placed on it, and in the code behind I have a reference to this control's Presenter.
The presenter handles the actual code for searching (when the one button is pressed). How do I set up the button click event on the child control to call the Search method from the presenter?
So I have the following:
CtlSearchDetails
ViewSearchScreen
PresenterSearchScreen
The button click event is on CtlSearchDetails and it needs to call the method on PresenterSearchScreen. I cannot figure out how to reveal this method to the instace of the control on ViewSearchScreen.
In your custom child control, you want to expose an event for the button click:
public event Action OnButtonClicked;
Then hook the button clicked event from the designer
private void btn_myButton_Clicked(object sender, EventArgs e)
{
if (OnButtonClicked != null)
OnButtonClicked();
}
Then in your parent container, you want to handle this event from the child control
this.myChildControl.OnButtonClicked += new Action(onChildButtonClicked);
private void onChildButtonClicked()
{
// Do your search here
}

Handling LinkClicked event for the richtextbox; the code runs twice inside the event handler

I've a windows form with richtextbox in it.There is a link which when clicked triggers the LinkClicked Event handler which is something like,
private void textSampleResults_LinkClicked(object sender, LinkClickedEventArgs e)
{
CallSomeMethod1;
CallSomeMethod2;
}
When I started debugging, I was expecting the code inside the event handler to run once only but it started Calling CalleSomethod1 and CallSomeMethod2 again.In summary, for every click, the code under the event handler is executed twice. Is it something expected? How to make it behave so that the code under the event handler gets executed only once?

Delegates, UpdatePanels and ascx controls

I have built an ascx control that is part of many different components of my application. There is a Previous and Next button on this control, which should be signaled to the parent aspx page. This is done by having the parent page add some Delegates for postbacks, such as OnPreviousClicked, OnNextClicked etc.
Everything in this app is 'ajaxified' with an updatepanel. Now I notice that my app breaks if I don't set the delegates on every single Page_Load call in the parent. In other words, if I don't ALWAYS set the delegates in the Page_Load of the parent aspx, then the ascx ends up with null delegates and an exception. Am I coding stuff correctly?
// inside the control
public event EventHandler OnPreviousClicked;
private void PreviousButton_OnClick(object sender, EventArgs e)
{
if(OnPreviousClicked != null) {
OnPreviousClicked(this, e); // or whatever args you want
}
}
// and inside the Page code-behind
private void Page_Load(...)
{
MyUserControl.OnPreviousClicked += new EventHandler(myHandler);
}
// OR inside the Page aspx, you also could just set the OnPreviousClicked property.
<xx:MyUserControl ID="MyUserControl1" runat="server" OnPreviousClicked="myHandler" />
See http://asp.net-tutorials.com/user-controls/events/.
If all you're doing is signaling the clicking of a button, I would have your ASCX control raise a simple event instead. That way each page can listen for the event if they need to and the controls can function regardless if anybody is listening.
First declare the events in your ASCX codebehind:
public event System.EventHandler NextSelected;
Then you create your button click events that raise the event.
protected void btnNextSelected_Click(object sender, EventArgs e)
{
if (EmployeeSelected != null)
{
NextSelected(this, new EventArgs());
}
}
Then in your parent ASP.Net pages you can add your ASCX control (we'll call it NavControl) and create methods that listen for these events.
NavControl.NextSelected += new EventHandler(NextPageRedirect);
protected void NextPageRedirect(object sender, EventArgs e)
{
Response.Redirect("~/ViewEmployee.aspx", false);
}
Note that with this you don't have to create the event handler or method and you can still use the nav control on your page. It should also eliminate the issues you are having with the delegates.
remember each postback has to restart your code. The event handlers don't get serialized to viewstate so you have to set them up again.
That is the expected behavior in asp.net.
The entire page life cycle occurs even when it is a partial postback (update panel). So in order for the events to fire, you will have to wire them up programmatically during page_load or declaratively in your markup (if those delegates you mentioned are events).

User control page_load event invoked before the Button click event of the aspx page

I want to assign property value on button click in aspx page and want to pass the value to the usercontrol and than bind data according to the Property value.
But the problem is before button click event is fired the page_load of user control is invoved ? is the any way to call the page_load of user control again on button click or is there anyother alternative to do it ?
Thanks
Utilizing Page_Load from a UserControl makes the control very dependant on the page life cycle and thus not very flexible. A better way would be to add a public method to the control that you call from the button OnClick event. That method would then perform the data binding.
Kinda like this:
//MyPage.aspx
void Button_OnClick(object sender, EventArgs e)
{
MyUserControl.DataBind(MyTextBox.Text);
}
//MyUserControl.ascx
public void DataBind(string value)
{
UpdateView(value);
}
This is kludgy, but if you have to you can always call the Page_load event manually after the button fires.
A better approach would be: depending on what code needs to fire after the button_click event, you can move it to another event handler, like the OnPreRender method.

Categories

Resources