Hey there i like to use a nice designed Form with yes no Buttons to use it like the normal yes/no Messagebox. To be clear its not a Messagebox its a Form with a Background Picture and Label (for the Textbody) and 2 Buttons (yes / no).
But what must i do, that it opens the Form and send back the userĀ“s choice that i can work with.
Messagetext = "This is the Text that is shown in the custBox"
Dim custBox As New custBox
CustBox.ShowDialog()
Select Case DialogResult
Case Windows.Forms.DialogResult.Yes
'code if clicked yes
Case Windows.Forms.DialogResult.No
'Code if clicked no
End Select
But i think it does not Work like this.
The DialogResult is the RETURN value from ShowDialog(), so you can do:
Dim cb As New custBox
Dim result As DialogResult = cb.ShowDialog()
Select Case result
Case Windows.Forms.DialogResult.Yes
'code if clicked yes
Case Windows.Forms.DialogResult.No
'Code if clicked no
End Select
Hopefully you're actually setting DialogResult in your other form, when one of the buttons is clicked:
' ... in your "dialog" form, Yes/No Button Handler ...
Me.DialogResult = DialogResult.Yes ' or DialogResult.No
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.
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
I have a button that clears all text and combobox fields, I would like to give the user a yes or no option to have a yes clear all fields and no leave them the way they were when the button was clicked. I have the code for the yes option i just need the no part. Thanks.
Private Sub btnNewForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewForm.Click
MsgBox("Are you sure you would like to clear the form", MsgBoxStyle.YesNo, "Confirm Delete")
If MsgBoxResult.Yes Then
For Each ctrl In Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
ctrl.Text = String.Empty
End If
Next
ElseIf MsgBoxResult.No Then
??????????????????
End If
End Sub
You need to check the results of your MessageBox:
Dim results As DialogResult = MsgBox("Are you sure you would like to clear the form", MsgBoxStyle.YesNo, "Confirm Delete")
If results = DialogResult.Yes Then
For Each ctrl In Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
ctrl.Text = String.Empty
End If
Next
End If
There is no reason to do anything for "No" since you don't want to change anything in regards to that answer.
You don't use that correctly. What you are actually doing there is....: Compare MsgboxResult.Yes with True.
Note that MsgBox() is actually a function that returns the button the user has clicked.
So do
Dim res as MsgBoxResult = MsgBox(...)
If res = MsgBoxResult.Yes then
'Code for Yes
Else
'Code for No
Endif
The MsgBox() function is also legacy VB6 stuff. It would be better to use
Dim res as DialogResult = MessageBox.Show("Message", "Title", ...)
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
How to set a message box which has buttons like "ok" and "cancel"? Also can we name our own names for the buttons? And can we write source codes for those buttons, like
ok.click, form1.show() and if cancel.click form5.show()
msgbox()
You could use MessageBoxButtons enum to specify different kinds of buttons on messagebox and use DialogResult after the button is clicked to specify different actions for each type of messagebox button.
Or create your own MessageBox
Try this:
MessageBox.Show("put your text here", "here the title of you message", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand)
after the second comma [,] you can put the type of buttons and at the last you can put the icon you think it will best suit your message box
regards
This should work:
If (MsgBox("Your Text", MsgBoxStyle.OkCancel, "MessageBox Title") = MsgBoxResult.Ok) Then
Form1.Show()
Else
form5.show()
End If
This is a simple message box:
If MessageBox.Show("some msg", "caption", MessageBoxButtons.OKCancel) = DialogResult.Cancel Then
' you cancel it, so i will show another message box
EndIf