winforms equivalent of asp.net page load event

  • Thread starter Thread starter parez
  • Start date Start date
P

parez

Hi,

Whats the winforms equivalent of asp.net page load event?

I am trying to clear a status message field every time a user clicks
on any of the buttons..
 
parez said:
Whats the winforms equivalent of asp.net page load event?

I am trying to clear a status message field every time a user clicks
on any of the buttons..

The equivalent would be the Form_Load, but the lifecycle of the winform
is different from the webform. The Class of the webform is created and then
destroyed on every postback, so the Page_Load executes every time you press
a button that submits the form. But the Winform Class is only created once,
and it doesn't recreate every time you press a button (there is no such
thing as a postback in winforms). There is no event that will execute on
every button click except for that button's button_click itself.

If you want a trick to clear your status message on every click on any
button, you can do the following: Write a "button_click" event handler that
clears the message. Then connect that event to every button in the form. You
can do this with a recursive routine that enumerates the Controls
collection. This does not interfere with the existing button_click for each
button, since you can have both event handlers connected to the same event
at the same time.
 
The equivalent would be the Form_Load, but the lifecycle of the winform
is different from the webform. The Class of the webform is created and then
destroyed on every postback, so the Page_Load executes every time you press
a button that submits the form. But the Winform Class is only created once,
and it doesn't recreate every time you press a button (there is no such
thing as a postback in winforms). There is no event that will execute on
every button click except for that button's button_click itself.

If you want a trick to clear your status message on every click on any
button, you can do the following: Write a "button_click" event handler that
clears the message. Then connect that event to every button in the form. You
can do this with a recursive routine that enumerates the Controls
collection. This does not interfere with the existing button_click for each
button, since you can have both event handlers connected to the same event
at the same time.

Thanks.. I asked the wrong question. but i got the right answer..
 
The equivalent would be the Form_Load, but the lifecycle of the winform
is different from the webform. The Class of the webform is created and then
destroyed on every postback, so the Page_Load executes every time you press
a button that submits the form. But the Winform Class is only created once,
and it doesn't recreate every time you press a button (there is no such
thing as a postback in winforms). There is no event that will execute on
every button click except for that button's button_click itself.

If you want a trick to clear your status message on every click on any
button, you can do the following: Write a "button_click" event handler that
clears the message. Then connect that event to every button in the form. You
can do this with a recursive routine that enumerates the Controls
collection. This does not interfere with the existing button_click for each
button, since you can have both event handlers connected to the same event
at the same time.

There is a problem. My first event which is wired up by the designer
runs first. The first event handler displays the current message and
the second event handler(button_click) clears it. I wire up the
button_click in my base form Load Event

Is there a way around this problem? or should i look for an
alternative solution for the initial problem?
 
parez said:
There is a problem. My first event which is wired up by the designer
runs first. The first event handler displays the current message and
the second event handler(button_click) clears it. I wire up the
button_click in my base form Load Event

Is there a way around this problem? or should i look for an
alternative solution for the initial problem?

I don't see any elegant and simple workaround.
You could inherit from the Button class, and then in your own button
override the OnClick method. You can then choose wether to call base.OnClick
before or after you execute your own operations such as clearing the
message.
Or you could experiment using a different event for clearing the
message, such as MouseDown, or better still GotFocus, so that it also works
with the keyboard. I think that this fires before the click event, but you
will have to experiment a little bit with the available events to find the
best one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top