Windows App crashes when minimised and then brought back to focus - vb.net

I've got a vb.net Windows form targeting .net 4.7.2 (but this problem seems to occur regardless of the .Net framework version. I've wanted to add a gradient colour to the form using LinearGradientBrush, but since doing that, the app crashes if it is minimised and then brought back into focus.
The error displayed is:
After some investigation I've found that it only occurs if there's also a textbox on the form. The error does not occur with labels, combo boxes, radio buttons etc. just textboxes.
To replicate this without any extra code, I've created a new Windows Form application, and added a single textbox to the form and the following code (which I copied from some website)
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim gradBrush As New LinearGradientBrush(Me.ClientRectangle, Color.Blue, Color.White, LinearGradientMode.BackwardDiagonal)
e.Graphics.FillRectangle(gradBrush, Me.ClientRectangle)
End Sub
There is no other code in the application at all.
If I run that, minimize the form, and then bring it back to focus the error occurs.
This is a link to a screen capture of the process occurring: https://www.screencast.com/t/9EFuez3hk.
I've tried various versions of the LinearGradientBrush code that I've found online, and I get the same each time.

If ClientRectangle.IsEmpty Then
Return
End If
That will abort mission if the rectangle has those properties.

Related

MouseEnter, MouseLeave, etc, Event Handlers on a second PictureBox Fail to Fire

I have a large WiseJ vb.net program which has multiple Picturebox controls. An image is loaded into the control on one event, the cursor changes on another etc. Each picturebox use four different events for my functionality. The first picturebox executes the events flawlessly. On the second identically specified picturebox the event handler fails to fire. It won't even execute the breakpoint. I have 8 such picture boxes but only one fires the event handler. I've searched for solutions and tried some but none really address my problem.
I had initially simply copied the working first picturebox code, pasted them, and edited the names to match the next picturebox. This has normally worked well to save time. I then deleted the second picturebox code and went to the page. I double-clicked the offending picture box which generated the click event. The new event still did not fire. I considered a solution which removed the event handler in each picturebox, but frankly, I've added events on pictureboxes so many times and they work fine. I'm guessing something is corrupted behind the scenes but in reviewing the project pages I see no reference to the pictureboxes and am not sure where to look. It's odd that one works fine with all the events but a second fails.
This even works
Private Sub PicFrontLR_Click(sender As Object, e As EventArgs) Handles PicFrontLR.Click
If SpkrDragged = False Then
Cursor = Cursors.Default
LoadSpkrinView(0, "System")
End If
End Sub
This event fails
Private Sub PicFrontC_Click(sender As Object, e As EventArgs) Handles PicFrontC.Click
If SpkrDragged = False Then
Cursor = Cursors.Default
LoadSpkrinView(1, "System")
End If
End Sub
I'm hoping I'm missing something, because one of the solutions involved recreating the page and systematically looking for a failure as I went. This page has a lot of code. Almost done with it and everything else seems to work perfectly.
Well, I figured this one out, mostly. It turns out that a picturebox "apparently" only triggers certain events if there's an image in it. Maybe basic knowledge but I hadn't run into this before. I simply added both a load picture event for the picture box and one for the container. If there's no image the container event fires. If there's an existing picture the picturebox event fires.
One unexplained issue is that the first picture box in a group of eight copied picture boxes, still fires the picturebox event even if there's no picture. As stated the picture box properties are all identical as are the containers. This is what confused me as one event worked and the others did not. Maybe someone can explain.

What causes Visual Studio To Change Control Names?

In one project, the names of my GUI controls are being changed at compile time.
Say, for example, I have a Label control named **lblRow0Col1".
I noticed my code was failing to find the control by name:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each ctrl As Control In Controls ' .Find("Label*", False)
If ctrl.Name = "lblRow0Col1" Then
ctrl.Text = DateTime.Now.ToShortDateString()
End If
Next
End Sub
So, I stepped through that routine and found the control I needed had been renamed to what looks like a GUI string.
lbl.Name = "07178f89-6fdd-47c7-9f84-d4d661df7554"
I created a test project to see what was going on, but this is not happening in the test project.
Is there a VS setting that tells the compiler to scramble the control names?
How do I stop this behavior?
OK, I’ve been looking at the code for that routine that populates a "details view" screen.
Before, I was filling the detail screen as soon as the Inventory Item variable changed.
I got to thinking that people rarely view the details screen, so why not just populate it after the screen was displayed? I had an event for “after displayed”, so I moved it there.
Well, I just moved it back ...and the odd behavior went away.
I don’t know what the issue was that caused that to happen, though.
The Label controls should have already been generated, but it acts like they were not until the tab screen they were on was selected.
So, there's an answer, but I'd rather someone explain to me what I did wrong.

VB.NET 2012, how to make ErrorProvider's message stay visible?

Does anyone know how to make the ErrorProvider's message stay visible when the mouse is over the ErrorProvider's icon? The message goes away or becomes not visible after a few seconds. People are complaining that there isn't enough time to read the message and they have to move the mouse away then back onto to the icon to see the message again.
Edit:
Tried Steve's suggestion (below) but the message still goes away
ErrorProvider1.BlinkRate = ErrorBlinkStyle.NeverBlink
ErrorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink
ErrorProvider1.SetError(Me.TextBox1, "Error")
This link Set ErrorProvider ToolTip Duration asks the exact same question I'm trying to ask here, unfortunately the question in the link is unanswered too.
VB 2012, Framework 3.5, Windows Forms Application. For reasons unknown to me, if one sets the BlinkRate to something between 10 and approx 4500 and sets the BlinkStyle = ErrorBlinkStyle.AlwaysBlink the ErrorProvider's tooltip / message will stay visible indefinitely as long as the mouse is hovered over the ErrorProvider's icon.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ErrorProvider.BlinkRate = 250
ErrorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
End Sub

Problems with message boxes in VB.NET

Windows 7 Home Premium X64
Visual Basic 2010 Express SP1
All Visual Studio updates installed except for a Visual C++ security update
The problem:
Sometimes message boxes in a console app close after clicking the ok button on one before all the messages are displayed. It is just supposed to display one message box after another.
Same code as above, A message box in a console app will sometimes minimize to the taskbar before displaying all the messages after the OK button is clicked on
The same code as the above 2 problem but in a Windows form app will display all of the message boxes, But.sometimes won't show the form at the end like it should.
The bugs I know happen when I run the programs from VB itself, don't know about the EXE outside of VB.
I know it isn't my code because others have tried it and had no problems.
What can I try to fix it?
EDIT: Code for the one with the button and form:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim anInteger As Integer = 42
Dim aSingle As Single = 39.345677653
Dim aString As String = "I like pancakes"
Dim aBoolean As Boolean = True
MsgBox(anInteger)
MsgBox(aSingle)
MsgBox(aString)
MsgBox(aBoolean)
End Sub
End Class
Code for the console ones:
Module Module1
Sub Main()
Dim anInteger As Integer = 42
Dim aSingle As Single = 39.345677653
Dim aString As String = "I like candy"
Dim aBoolean As Boolean = True
MsgBox(anInteger)
MsgBox(aSingle)
MsgBox(aString)
MsgBox(aBoolean)
End Sub
End Module
If you've run the exact code you posted above, and you're getting the symptoms you describe... I would say that it is probably a hardware problem, specifically a problem with either your mouse or your keyboard.
You believe you are clicking the mouse button once (to close the OK button). But just at the moment that the system is trying to show you the next message box, it thinks you hit the mouse button a second time - or maybe it thinks you hit the SPACE bar - either way, it closes the next message box instantly.
Prove (or disprove) my theory:
First, wait until after hours, or until you're the only one in the building. (Hint: One of your "friends" might be playing a joke on you, using some sort of remote-access technology.)
Remove your mouse and keyboard. Borrow the mouse and keyboard from some other computer (at least temporarily) and plug them in instead.
Reboot. (Shouldn't need this, but try it anyway -- just to be sure.)
Start your program. When the first message box appears, wait to make sure it doesn't go away on it's own. Then carefully click the mouse ONCE on the OK button. Repeat for the other 3 message boxes.
If the problem still happens, try it again, except don't use the mouse at all - use your keyboard's SPACE bar to dismiss the message boxes.
If needed, try it one more time, this time with the ENTER key.
If the problem still hasn't gone away, try booting in SAFE MODE -- perhaps some other software is interfering! Also try doing a virus scan, even if you're fairly certain you don't have a virus.

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

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.