Looks like the only way to auto print without user pop up window is to have the line MessageBox.Show("print")
When this line of code is there and you click "ok" it avoids the pop up print window if you put Me.Close() after dispose line.
https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-print-with-a-webbrowser-control
My question is... is there a way to force the OK click on the MessageBox so that the auto print happens?
I tried doing the following with a blank Form2 to force the ok but didn't work:
Dim dr As DialogResult = Form2.ShowDialog(Me)
Me.DialogResult = DialogResult.OK
If Me.DialogResult = DialogResult.OK Then
Thread.Sleep(2000)
webBrowserForPrinting.Dispose()
Me.Close()
End If
Related
Using Message Box DialogResult.No Condition for closing the form doesnot perform as expected.
The formclosing event aske user whether to save the document or not before closing.
The following is my FormClosing event.
Private Sub PDFViewSimple_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
Handles Me.FormClosing
If doc.IsModified Then
Dim message As String = "The document is modified, would you like to save it?"
Dim caption As String = "File Not Saved"
Dim buttons As MessageBoxButtons = MessageBoxButtons.YesNo
Dim DefaultButton As MessageBoxDefaultButton = MessageBoxDefaultButton.Button1
Dim icon As MessageBoxIcon = MessageBoxIcon.Question
Dim result As DialogResult
' Displays A MessageBox.
result = MessageBox.Show(message, caption, buttons, icon, DefaultButton)
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
Me.Close()
ElseIf (result = DialogResult.No) Then
Me.Close() ''Should I replace with (Application.Exit)
End If
End If
End Sub
There's all sorts wrong with that code. Firstly, given that there are only two options, using ElseIf is pointless, although not strictly wrong. If it's not Yes then it must be No, so you'd only need an Else:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
Me.Close()
Else
Me.Close()
End If
Next, even an Else is pointless because you're calling Close regardless of the outcome. All you need to do is check for Yes, do anything specific to Yes and then call Close regardless:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
End If
Me.Close()
Finally, you shouldn't be calling Close at all. You're in the FormClosing event handler so the form is already closing. You only have to do something if you want the form to NOT close. So, all you need is this:
If (result = DialogResult.Yes) Then
Me.Save(Me.Text)
End If
If you wanted the form to NOT close then you would set e.Cancel to True.
I have a windows form that contains a SaveFileDialog as a component. This form is called from a library (.dll). When you click on btnExport you should see the save file dialog window.
My problem is that SaveFileDialog1.ShowDialog doesn't show any window to select the directory path.
This is my code:
Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
SaveFileDialog1.Filter = "XLS File|*.xls"
SaveFileDialog1.Title = "SaveFileDialog title"
Try
' this should open the dialog
If Me.SaveFileDialog1.ShowDialog() = DialogResult.OK Then
' Do something
Else
' This is a custom function to show messages
ShowCustomMessage("Error opening SaveFileDialog")
End If
Catch ex As Exception
' Show exception
End Try
End Sub
Your code works. You could try the following -
Try removing the Try-Catch-End Try as it could be hiding an error from you since you didn't implement anything to handle the Catch.
Try specifying the Owner object when calling ShowDialog() to ensure it is not going behind your main form.
Me.SaveFileDialog1.ShowDialog(Me) ...
I have a program that we use to run different reports. Based on the menu option chosen, I open the same form that lists the reports based on the menu option.
(There are also different options and functionalities in the program, not just one form).
When clicking a menu option, I have the following bit of code
Private Sub ReportsToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ReportsToolStripMenuItem1.Click
FormLocation = "F_Legal"
FormName = "Legal"
PrepareForm(F_Select_Report)
End Sub 'ReportsToolStripMenuItem1_Click
Where F_Select_Report the form is that is opened.
Private Sub PrepareForm(formName As Form)
Cursor = Cursors.WaitCursor
For Each Form In Me.MdiChildren
Form.Close()
Next
formName.MdiParent = Me
formName.Height = Me.Height
formName.Width = Me.Width
formName.Show()
Cursor = Cursors.Arrow
End Sub 'PrepareForm
This bit is called, closing all other opened forms, and then open the form that is called.
This works fine on the first time I try and open a form, but on the second try, I get an error message saying
Cannot access a disposed object.
And then on the third try, it opens the form again.
How would I fix this up?
Thanks a lot
Form.Close implicitly calls Form.Dispose
So if the formName is an MdiChild it gets disposed in the For Each loop.
Then, on the next line your code attempts to assign to its MdiParent property and there the error occurs.
So you need to skip it when closing MDI children like this:
For Each Form In Me.MdiChildren
If Not Form Is formName Then Form.Close
Next
Given your code I think it is better to close the children before showing the F_Select_Report form. I.e. move the For Each loop as is to the top of the ReportsToolStripMenuItem1_Click handler.
Not sure if it is the best/nicest solution, but found a solution to this.
Instead of 1 Sub that does both closes all open forms, and then opens the new one, I split it out over 2 Subs.
Close all open ones
Private Sub CloseAllForms()
For Each Form In Me.MdiChildren
Form.Close()
Next
End Sub 'CloseAllForms
And then open the new form
Private Sub PrepareForm(formName As Form)
Cursor = Cursors.WaitCursor
Try
formName.MdiParent = Me
formName.Height = Me.Height
formName.Width = Me.Width
formName.Show()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Cursor = Cursors.Arrow
End Sub 'PrepareForm
Now it works as needed.
So, I'm trying to close all forms in my app when the user reverts their settings via clicking a button, but it isn't going well, as for some reason when I try to close them nothing happens.
I have checked for output and didn't notice anything, and strangely, if I close my forms without an if statement, it works. What's up?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim result As DialogResult = MessageBox.Show("Are you sure you want to revert all your settings? There is no undo, and if you click yes, WikiFinder will restart.", "WikiFinder", MessageBoxButtons.YesNo)
If (result = DialogResult.OK) Then
My.Settings.MenuColor = System.Drawing.Color.FromArgb(248, 24, 0)
My.Settings.MenuTextColor = Color.White
My.Settings.Siggy = ""
My.Settings.Save()
WebForm.Close()
ForumSiggy.Close()
URLform.Close()
MenuForm.Close()
Me.Close()
Else
End If
End Sub
You are not entering the If block because your dialog is Yes/No not OK. Use
result= DialogResult.Yes
This would be easy to determine by debugging.
frmMain
DoSomething()
My.Forms.frmMessage.ShowDialog(Me)
If AcceptButtonClicked Then
' Do Code
DoCode()
Else
' Cancel Button Pressed
DoOtherCode()
End If
DoMore()
frmMessage
My.Forms.frmMain.AcceptButtonClicked = True
Is there a way to pass a value from a Dialog window back to a paused thread on the main window? I want to know if they pressed the Ok or Cancel Button after filling out a form that pops up.
You can use the DialogResult property on your form. This value will be returned by the ShowDialog function you call. You can also set this property on your buttons so WinForms will handle the setting of the form property.
In your frmMessage you'll have to set the property accordingly (pick the one you need, OK and Cancel). Then you can check the return value easily:
If My.Forms.frmMessage.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
' OK button pressed
DoCode()
Else
' Cancel button pressed
DoOtherCode()
End If
Don't forget that the user might be able to close the form in another way than closing it with your buttons (e.g. by closing it with the close button).
You should set the AcceptButton and the CancelButton property on the Form, but also, the AcceptButton should have its property DialogResult set to OK and the CancelButton to Cancel.
In this way, when your user presses one of these buttons the ShowDialog call returns and you can check the return value using the predefined values in the enum DialogResult
DoSomething()
Dim result = My.Forms.frmMessage.ShowDialog(Me)
If result = DialogResult.OK Then
' Do Code
DoCode()
Else
' Cancel Button Pressed
DoOtherCode()
End If
DoMore()
The answers by Styxxy and Steve both work for handling the DialogResult in the main window. However the DialogResult property for the Accept button should not be set in the properties window, it should be set in your code after validation occurs. This way if the user inputs bad data in the form they can get an error message and fix it without losing any work, instead of starting over.
'code in Dialog Form
Private Sub btnAccept_Click(sender As System.Object, e As System.EventArgs) Handles btnAccept.Click
If IsValid() = True Then
DialogResult = Windows.Forms.DialogResult.OK
End If
End Sub
Me.DialogResult = Windows.Forms.DialogResult.Abort Me.Close()
and it will return result Abort