VB.NET - Webrowser freezes when navigating from Form Load event - vb.net

I'm trying to load up a YouTube page using Visual Studio 2008 and a very simple Visual Basic project that contains just a WebBrowser. I want to load the page when the form loads:
Private Sub Form1_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
WebBrowser1.Navigate("http://code.google.com/apis/youtube/js_example_1.html")
End Sub
Every now & then (occurs once every 3-5 times, but varies), the form just shows a blank white box.
Could someone help me understand why this happens please.
Edit: This 'freeze' happens for any page, such as http://www.google.com. Is my WebBrowser control broken? Only seems to happen when i place the navigate code in the Form Load event, hmmm strange.

This is probably (almost certainly) happening because a form's Load event occurs before its first Paint event, so once in awhile the WebBrowser finishes navigating to Google (or wherever) before the form paints itself the first time, and thus the WebBrowser shows up as just a white box.
A very simple solution to this problem is to just put a Timer control on the form with a short Interval (say, 100 ms), set Enabled to true, and in its Tick event disable the Timer and then call Navigate(...) on your WebBrowser.

Related

Nothing works anymore after building project in vb.net

I was going to build my project when i noticed i didn't put an icon,
and since i couldn't access the icon value of the original form because i used a theme i C+X the container and access it from the grey form, change it, C+V the container, built the project.
Nothing changed, all the name of the buttons and stuff are the same, but i feel like nothing is connected to the code anymore, i don't know what happened, i just recently got re-interested into coding, and i have no idea what to do, i tried some things but nothing worked, so here i am, desperate (i spent 3 days on this, i'm REALLY starting from bottom)
link to the project: http://www.mediafire.com/file/2zrbe32lzpx2qhz/SchedulerProjectVBNET.rar
Thanks in advance
It sounds like you have lost all the Handles clauses off your event handlers. As an example, if you add a Button to your form and double-click it, you will get a Click event handler like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Note the Handles clause at the end, which is what connects the code to the event of the control. If you cut that Button from your form to the Clipboard then it no longer exists as part of the form, so that Handles clause will be automatically removed. If you paste the Button back onto the form, the Handles clause is not restored, so the method no longer handles the event.
If that's what has happened - which you can easily check just by looking at the code - then you need to restore all those Handles clauses. You can do that manually, i.e. write them all yourself in the code window, or you can have the designer regenerate them for you. To do the latter, select a control in the designer, open the Properties window, click the Events button, select the desired event and then select the appropriate method from the drop-down list.
Note that you can also double-click an event there to generate a new event handler, which can be useful for handling events other than the default event. You can generate a handler for the default event simply by double-clicking the control.

Opening a UserControl using multithreading to avoid UI Freeze VB.NET

I'm a beginner. I've done a lot of research about Backgroundworker/Multithreading, but I still don't know what to do. My problem is the following:
I have a MENU (Which contains just a collection of colored buttons). When one of them is called (Btn), the button opens a new "UserControl". But it takes a long time to load, so the UI freezes for many seconds. I just want to avoid that. I want to show a Loading CircularProgressBar on top of my MENU while the UserControl loads and keeping the UI responsive during the loading.
The button's code :
Private Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
MainForm.ShowModalPanel(New UserControl1, DevComponents.DotNetBar.Controls.eSlideSide.Bottom)
End Sub
Thank you in advance.

Why doesn't a MsgBox trigger a lost focus event VB.net?

I have a timer that each time displays a message box saying "Hello." I also have the code configured so whenever the window loses focus, it should stop the timer that keeps the boxes coming. However, they keep coming.
I have tried a similar thing in a similar program with way too long of code to post here, but what it did was it paused the first time, stop the timer, and when the timer was stopped again, it didn't work correctly. There was also some other code there that had a random element, that displayed a different prompt when a certain number was generated, but once it was generated, it kept using that same different prompt every time.
Is this a error of not enough time to process all the code and it "overlaps" some? I can delay the timer without that much different effects, but I think that my [lower end] CPU that it is running this program on, that with 1.6 GHz that it could handle a timer with a few message boxes. Though, VS is running at the same time, but I shouldn't have to export my code and close VS everytime that I need to test it.
If the problem is not enough time, is there a way that I can prevent my program from "multithreading" or whatever it is doing? It seems like a weird problem, but computers are very weird too. :P
Edit:
By "Focus" I mean the selected window that is the most apparent. For example, my browser is now "focused." I have been informed that the correct term is "selected." I must have been using the wrong type of event trigger... :P
It doesn't generate a lost-focus event because the form doesn't have the focus in the first place. A control on the form always gets the focus, like a Button or TextBox. You could use the Deactivate event instead.
Or just not display the message box when the Tick event fires again. Roughly:
Private ShowingMsgBox As Boolean
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'' Do stuff
''
If Not ShowingMsgBox Then
ShowingMsgBox = True
MsgBox("yada")
ShowingMsgBox = False
End If
End Sub
The underlying reason for this behavior is that MsgBox pumps a message loop. It keeps normal Windows messages getting delivered, like WM_PAINT that keeps the windows painted. And WM_TIMER, the one that generates the Tick event. The only kind of messages that it blocks are input events, mouse and keyboard messages. Otherwise the reason that Application.DoEvents() is so very dangerous. It does the same thing as MsgBox() does, without disabling input.
Create a new project with a Timer (Timer1) and write this code:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If (Me.Focused) Then
MessageBox.Show("Hello")
End If
End Sub
If you put the mouse over your form, you would see that a message box will popup after the given Interval is over. If you don't click on the accept button and keep the mouse on the form, you would see that no further messages appear: Me.Focus is False. If you click on the accept button, the messages would start poping up; you don't even need to select the form (the focus is transferred automatically from the MessageBox to the Form).
Summary: the MessageBox does make the Form to lose the focus, although it is a kind of a "tricky" lost as far as will automatically come back after clicking on the accept button.
UPDATE: the proposed configuration does trigger a LostFocus event of the form:
Private Sub Form1_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
MsgBox("lost")
End Sub
Unlikely the other answers/comments, what I understood from your question is that you want to know the reason and if this is a normal behaviour, rather than getting a working solution to make the form to lose the focus (you are not even describing the exact conditions under which you want this to happen).

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto

vb.net activated fires multiple times

I want to update a database based on the form that is currently activated. I had originally decided to use the GotFocus event. However I now understand that will not work as the form has controls on it. So I then thought I wouls use the activated event. This works but seems to fire multiple times. I put in the following code:
Private Sub frmNewTicket_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
MsgBox("Form Activated")
End Sub
I select the form and make it activated and the message box appears about 15 times.
Why does it do this? How should I handle this. I only want my code to execute once when the form is activated.
NOTE: There are several forms that the users will be changing between, incuding forms from other applications.
Each time you click OK on the messagebox, the form regains the focus and is activated again.
Put a static Boolean value in your frmNewTicket_Activated like someone has posted here:
Static HasRan As Boolean=False
If Not HasRan Then
HasRan=True
'put code here
End If
It sounds like you are wanting to do something everytime your form gets activated. The Form Activated event will work fine as long as what you are doing doesn't pull focus from the Form which will then trigger another Activation event when the Form gets focus again. Try using something other than a MessageBox for testing like Beep or changing the Form's Color