Is there a way to execute code in VB.NET after *any* event - vb.net

In VB.NET, is there a way to execute code after any event for which I have written an explicit handler, other than placing a call as the last line of each individual event handler? In ASP.NET I can put code in PreRender but there is no equivalent of that in VB.NET because there is no page life cycle.
I understand that for desktop apps the model is very different and that PreRender doesn't fit the desktop model but I hoped it would illustrate what I meant. In ASP.NET I often determine whether controls are visible, or enabled, in PreRender, after events have been processed and the underlying database has changed as a result. It seems reasonable to want to do something similar in VB.NET - multiple events can alter the underlying database, and multiple controls may need refreshed as a result, so write a routine that determines the visibility and enablement of the controls, and call it after explicitly-handled events have been dealt with.
I've tried the form validating/validated events but can't make them work.

Related

Debugging problem in VB.Net with Windows Forms with losing events

We have a huge application that runs through a mass of code for every click you do. In one specific case, where you have a specific control in plain sight (so that the events are firing) and you load some special data into it and then click on the new button the form loses some events, like the closing event (you cannot close the form anymore and stop points in debug mode are not triggered) and one validating of a ComboBox that is really needed for the program. The only thing you can do when you get this kind of error is to restart the application to get out of this subform. But I tested it for 2 days now and even when I activate all points in the 'Exception Settings' I don't get any exceptions. The form (and other controls) are not losing ALL events. The resize, for example, still works. Even a specifically called AddHandler is not firing. Can you iterate somehow through the EventHandlerList? Or is there any way to test WHERE the event dies? Somehow any ideas on how to debug such a problem?

Is there any intelligent way of handling VS event handler removal after copy/past of control

I am using VB.Net 2008 and have been experiencing event handler removal after I do copy/paste of controls, such as to group box or panels. I had to add event handles manually for every control. When I try to move them instead of copy/paste, I had to deal with the z-order of each control. That is also a manual work. And this all becomes time consuming when there are several of them.
My questions are:
1) Is this a problem only for VS 2008 or for all VS?
2) Is there any other intelligent way of handling this issue?
Unfortunately, that's the way it is up through VS2013 at least.
If this is a recurring problem with a certain set of controls, you can create a user control containing them and put most of the offending handlers inside the user control.

How to call multiple subs with a BackgroundWorker

I have not yet used BackgroundWorker but believe I need to use these in my code.
My app does a lot of database work, running many SQL queries in sequence. My problem appears typical, that the main form becomes unresponsive.
I want to be able to display progress using a progressbar and a toolstripstatuslabel. I am doing this already, but without the BackgroundWorker.
My code is - unsurprisingly - divided into a several subs, which are called in sequence by the main form.
All the examples I have seen include just simple BackgroundWorker DoWork events. What is the correct way to deal with calling other code? Just call the modules as usual in the DoWork event? I do understand these must contain no user interface code.
Am using VB.Net 2010
Thanks!

IntializeComponent keeps getting overwritten

I have some conditionals in my InitializeComponent which affect the layout based on some variables. Unfortunately, it seems like whenever I rebuild my application, this code is reverted back to its previous state. Is this code being regenerated based on the Designer interface? Is there a way to prevent it from doing this?
Yes, InitializeComponent is completely IDE-generated; don't even try to mess with it.
If you have conditional logic wherein you want to add/remove some controls, do it in your control's constructor after the auto-generated call to InitializeComponent.
Note that if the conditional stuff will depend on features enabled/disabled at design time (e.g., if someone else is using your control and you've provided properties to affect how that control behaves which you intend to be set at design time), using the constructor won't work since the constructor will have already run by the time the user makes his/her choices from the design view; in this case, override the OnLoad method and put your logic in there (or do some variation of this, e.g., handle the Load event itself—there are plenty of ways to skin this cat).

Controls disappearing on timer event-vb.net

We have a question regarding VB.Net 2008.
We are used control array in vb.net and third party timer controls.
When handle received from external application to timer control event procedure,
after this form becomes blank and controls disappear.
What we have to do to persist the controls.
You asked what you have to do to persist the controls. It's not clear whether you mean winforms or webforms, but I can answer for both possibilities:
If it's the former, you have it backwards. The default behavior of all controls is that they are "persisted" until you tell them otherwise. If anything disappears, it's because you have code somewhere that tells it to. That's where you need to start looking.
If it's webforms/ASP.Net, the problem is that you don't understand the page lifecycle. Everything that raises server events, including your third party timer controls, causes a post back. That's how events work - the browser posts the form back to the same url in such a way that the server knows to call a your event code at the right time. The thing here is that as far your server is concerned, it's still just a new http request, and that means you're working with a brand new instance of your page class every time this happens. If you've previously added some controls to your page, it doesn't matter. That was an old instance that was discarded and probably disposed by the time the page was visible in the user's browser. If you want to keep those controls, you need to make sure you add them to the page on every postback.