Avoid refresh event in a WebBrowser control? - vb.net

I am using the GEPluginControl in VB.NET (VS2010).
My app works fine. I am using the GEWebBrowser control (included on the GEPluginControl), and I am able to show the information in the Google Earth format.
However, I am having troubles when the GEWebBrowser control Refresh method is called. When this happens, the GEWebBrowser starts to fail (the earth disappears and I am not able to reload it again).
I am not calling the Refresh method explicitly. I think this method is called automatically in the following case: when the user close the window, I'm catching the event Form_Closing, in order to ask the user if really wants to exit. If the answer is No, the user will stay on the app, but the GEWebBrowser control appears blank!. I can hear the sound associated to the refresh method, so I think this method may be my trouble.
Here is the Form_Closing event code:
Private Sub frmPrincipal_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Quit", "Really want to exit?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'App end.
server.Stop()
Else
'User decides to stay.
e.Cancel = True
End If
End Sub
Does anybody knows how to avoid the WebBrowser (GEWebBrowser in this case) to execute the refresh method? If anyone knows how to solve this in any other way, I would still be very grateful.

You could try this (not guaranteed to work):
Private Sub StopRefresh(sender As System.Object, e As System.EventArgs) Handles WebBrowser1.ProgressChanged
WebBrowser1.Stop()
End Sub

Related

VB.net PrintForm Not Working in New Thread

I am developing a e-filing app and I need to print an adhesive label with some info to attach to the physical folder.
I already designed the label as a Form put the logo and everything that I need there. Then on the Form.Shown event I put the command to print:
Me.PrintLabelForm.Print() (This is VisualStudio PowerPack Control)
And here is where I bump into a problem. The print out is totally empty (I already changed margins setup the printer, etc). The issue is that the form is not actually fully loaded, I switch the method to the print preview and the controls are there but they are empty.
I tried several approaches but I have been not able to do this automatically. One solution that I found was to have a button to do the Me.PrintLabelForm.Print() then it works because the form is already fully loaded and displayed but this is not an option. I need the form to open automatically, print and close.
An option that I think it should work will be to have a new thread with a timer then printing so I did this:
Private Sub LabelPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LabelPrint_Shown(sender As Object, e As EventArgs) Handles Me.Shown
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Left = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Right = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Top = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Landscape = True
Dim PrintThread As New System.Threading.Thread(AddressOf PrintSub)
PrintThread.Start()
End Sub
Private Sub PrintSub()
Threading.Thread.Sleep(1000)
Me.PrintLabelForm.Print()
Me.Close()
End Sub
The idea was to have the PrintSub to give the app enough time to finish to render the whole thing then print but I am getting this error:
**An unhandled exception of type 'System.Exception' occurred in Microsoft.VisualBasic.PowerPacks.dll
Additional information: The window being printed must be visible and contain focus.**
So I wonder how to make this thread have the window form in focus in order to be able to print.
That is all. Thanks for all the help.
Always work with the form only from main thread.
You found it right – form printing will not run from new thread.
When you do any actions on forms, you must perform all the work from Dispatcher thread. It is the thread on which all event methods run. If you fail doing so, you can encounter many side effects. (Not only problem with printing. I've been there and this advice from senior Windows programmer helped me to get things back to normal.) So do not use form printing from any other thread.
If you want a workaround for this, print form to the image (in main thread) and then you can print the image using new thread.
This has nothing to do with .NET, this is related to internals of Windows Forms technology. Welcome to Windows programming.
I manage to solve it putting this line in the Form.Shown
PrintLabelForm.Print(Me, PrintForm.PrintOption.ClientAreaOnly)
I don't know why or how but it works.
Thanks to all of you guys for your help. Let's hope I don't find myself trying to do stuff when the form is fully displayed.
This is my full code let's hope it works for someone else:
Imports Microsoft.VisualBasic.PowerPacks.Printing
Public Class PrintAdhesiveLabel
Private Sub LabelPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LabelPrint_Shown(sender As Object, e As EventArgs) Handles Me.Shown
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Left = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Right = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Top = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Landscape = True
PrintLabelForm.Print(Me, PrintForm.PrintOption.ClientAreaOnly)
Me.Close()
End Sub
End Class
Perhaps this is relevant:
Only the form that currently has focus can be printed by using this
method. If you have set the Form property to another form before
calling this method, the image of the form may not be rendered as
expected. To avoid this, call the Focus method of the form before you
call Print.
So call Me.PrintLabelForm.Focus() before calling Me.PrintLabelForm.Print():
Private Sub PrintSub()
Threading.Thread.Sleep(1000)
Me.PrintLabelForm.Focus()
Me.PrintLabelForm.Print()
Me.Close()
End Sub

vb.net how to solve 2 issues with single and double click on notify icon

I have a vb.net (.NET 3.0) app which has a NotifyIcon in the system tray. I would like the single left-click and double left-click events to do different things; .Click should open the app's context menu, and .DoubleClick should take some default action. So this is my code at the moment:
Private Sub showMenu(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Tray.Click
Debug.Print("click")
If e.Button = MouseButtons.Left Then
Dim mi As MethodInfo = GetType(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance Or BindingFlags.NonPublic)
mi.Invoke(Tray, Nothing)
End If
End Sub
Private Sub defaultAction(ByVal sender As System.Windows.Forms.NotifyIcon, ByVal e As System.EventArgs) _
Handles Tray.DoubleClick
Debug.Print("double click")
doDefaultAction()
End Sub
The first problem is that the .Click handler is fired even for a double-click - it responds to the first click rather than waiting to see if it was actually a double-click. Maybe this is normal behaviour, but is there a 'best practice' way of trapping that occurrence without horrible kludges involving timers? From what I've read on SO, I suspect not. However, that's not the most serious problem...
The second, bigger, problem is that the doDefaultAction() code does various things, one of which is to download an xml file from a specific URL. I'm doing this with this line of code (note not the actual URL:):
Dim reader = XmlReader.Create("http://server.com/genxml.php")
As soon as execution reaches that line, another .Click event is fired, so the debug output looks like this:
click
double click
click
That second click event re-opens the context menu, and because doDefaultAction() goes on to show a modal MessageBox, the menu gets stuck open. I've stepped through in the debugger, and if I 'Step Into' that Dim reader line, I get taken straight to Sub showMenu() above. Very odd. Any ideas what could cause that?

Form_Closing event in vb.net launched twice

I'm developing a simple app in vb.net (vs2010). I'm using the Form_Closing event in the following way:
Private Sub frmPrincipal_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Quit", "Are you sure?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Me.Close()
Else
e.Cancel = True
End If
End Sub
This code shows a dialog asking the user if he really wants to leave the app.
I the answer is 'No', it works fine, but if the answer is 'Yes', it seems that the event Form_Closing is launched again, and the same dialog is showed again, asking if the user really wants to exit the app... The answer in this second dialog is not evaluated by the app, this means, the app will end after this second dialog.
My question is, how to avoid this second dialog when the user select 'Yes' on it?
Regards,
Daniel.
You shouldn't need to call Close() a second time.
You have already Closed the Form and again Calling the same pressing Yes so in place of
Me.Close
Use
End

Microsoft Office BeforeCloseHandler

I made a function in vb.net for desktop application that totally saved the documents before closed, I implemented it with the System.Threading.Timer.
I'm asking with favor on how you experienced it.
So, I made a timer to saved the documents before closed.
But, upon testing without the timer, just one call to the function or Handler, the documents are not saved before closed, because a dialog box appears asking to saved or not or cancel.
Is it really in need to have the timer? because we know a timer could delay the system even a single milliseconds.
I setup the timer with 100 milliseconds.
But, I don't want to use the timer, I want to saved the documents before closed for just one call of the function.
Is it really using the timer is the solution? or it can be done with just one call setup?
If it can be done in one function call, then I think I'm missing a code.
Thanks for your opinion and experienced.
You can handle the FormClosing event. In the handler you can perform actions before the form closes you can even cancel the closing event. Here's a an example:
Dim Saved As Boolean = False
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not Saved Then
Dim Result As DialogResult = MessageBox.Show("Do you want to save your text?", "Save Text?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Select Case Result
'Since pressing the No button will result in closing the form without
'saving the text we don't need to handle it here
Case Windows.Forms.DialogResult.Yes
SaveText_Click()
Case Windows.Forms.DialogResult.Cancel
e.Cancel = True
End Select
End If
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Saved = False
End Sub
'SaveText is a button for the user to manually save the text on demand.
'Since we don't need the sender or the eventargs, we can handle the click event without them.
'this way we can call this like any sub without having to worry about providing the right parameters.
Private Sub SaveText_Click() Handles SaveText.Click
'Add your procedure to save the text here
Saved = True
End Sub
If you don't want the option to close without saving just omit the messagebox and the select block and just call SaveText() and include the code to save your data in there.

Windows Form Cancel Button Not Working

I have a Visual Studio, Visual Basic form that includes an OK button and a Cancel button.
What I want to do is have the OK button save the options that the user chooses and of course the Cancel button discarding them and returning them to their previous values.
But what I'm noticing is that as I'm debugging the form, the values are being saved regardless of whichever button I'm choosing. On the form's properties, I have declared that indeed the CancelBtn is the CancelBtn and that the OK button is the OK button, but the values are still being saved regardless.
Is there a better way to do what I would like this form to do?
EDIT:
Here's the code so far for the two buttons, both are being set to close the window. AcceptOption should save the values and CancelOption should just close the form. I'm sorry if this isn't done well but the FAQ's that I found only mention changing the properties of each button and nothing about the code.:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles AcceptOptionBtn.Click
' Save the Options
Me.Close()
' Close the form
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles CancelOptionBtn.Click
' Close the form
Me.Close()
End Sub
Don't change "the values" until the user clicks the Save button.
The form should be preloaded with a copy of the values you would like to update.
The Cancel button should just close the form.
The Save button should cause "the values", not the forms copy, to be updated.
EDIT:-
In regard to this question, there is nothing wrong with the code you have posted. Are the right handlers being called for the right button clicks? Are the form's AcceptButton and CancelButton properties set to the right buttons?
What data are your editing controls bound to, if at all?
There's nothing magical about OK and Cancel buttons. They're just... buttons. If you save your data every time a change is made, the Cancel button won't magically "unsave" them. Though if you save changes in the OK button's Click event handler, then clicking the Cancel button obviously won't save your changes. To help you further we'd need to know how you save your data.
Edit:
From looking at your code, I think you're passing data directly to your form, without performing a copy of your objects. Therefore if you modify this data, it will also be changed in the parent form. By working with a copy of your data in this form, any changes which aren't saved will be correctly discarded.
Your event handler for the cancel button should look like this:
Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Your event handler for the OK button should look like this:
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
SaveSettings 'call a routine to save the settings the user has entered
Me.Close()
End Sub
It is as simple as that!
If you open your form like
myForm.showdialog()
you don't have to define the handler for the close button click event, it is automatically handled; just set the 'DialogResult' property for the button
btnCancel.DialogResult = DialogResult.Cancel
Also if you want to close the form when ESC is pressed then set the 'CancelButton' property for the form:
myForm.CancelButton = btnCancel
On the other hand if you open the form like
myForm.Show()
you do need to specify the action(s) to take on the close button click event as indicated here, ie:
Private Sub BtnCancelClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
Close()
End Sub
I was having the same issues. As soon as I use My.Settings.Blabla = Blabla.value, it gets saved even if I haven't used My.Settings.Save() which makes My.Settings.Save() completely pointless as far as I can tell.
I ended up taking up Jordell's advice: Don't change "the values" until the user clicks the Save button but it wasn't too clear for me how to go about it.
I ended up using temporary variables in all my settings subs instead of the user My.Settings.UserConfigs. Only when I was in the OK sub did I call
My.Settings.UserConfigSetting = temporary_UserCofigValue
Here is an example from the code I was working on:
Private Sub btnOptionsThemeLB_Back_Update_Click(sender As System.Object, e As System.EventArgs) Handles btnOptionsThemeLB_Back_Update.Click
If (tempOptionsThemeLB_Back = Nothing) Then
tempOptionsThemeLB_Back = Me.btnOptionsThemeLB_Back.BackColor
End If
tempOptionsThemeLB_Back = RGBToColor(txtbOptionsThemeLB_Back_Red.Text, txtbOptionsThemeLB_Back_Green.Text, txtbOptionsThemeLB_Back_Blue.Text, tempOptionsThemeLB_Back)
Me.btnOptionsThemeLB_Back.BackColor = tempOptionsThemeLB_Back
End Sub
And only withing the Ok sub did I call My.Settings.
'Theme Section
My.Settings.colorBtnBack = tempOptionsThemeLB_Back