Reputation: 230
So putting it as simply as possible, I have one class which opens a form from inside sub like so:
Public Sub LoadExtension()
'various code...
Dim form as new frmMain
frmMain.ShowDialog()
'various code...
End Sub
And inside this form I have two buttons, one will just close the form, so the LoadExtension() sub will continue. The other button I want to use to 'exit' the LoadExtension() sub inside the main class so that loading stops completely. The button event inside the form module is like so:
Private Sub btnStopLoad_click(sender as object, e as eventargs) handles btnStopLoad.click
'exit the LoadExtension sub somehow
End sub
What is the simplest way to achieve this? I thought I could do something like this in the LoadExtension() sub (after the .ShowDialog):
If frmMain.btnStopLoad.clicked then
Exit Sub
End If
But it won't let me do that from the class module, apparently I need to use 'raise event' or something? I'm not very familiar with the way that events work. Can someone offer me an easy solution? I'd be very grateful. I've been looking around the web for a solution but haven't had any success. Thanks for your time.
Upvotes: 1
Views: 1281
Reputation: 26434
DialogResult is what you are looking for - the standard way to do it.
You can, however, subscribe to events of another form's controls, like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim frm As New Form2
AddHandler frm.Button1.Click, AddressOf OtherFormButton_Click
frm.ShowDialog()
End Sub
Private Sub OtherFormButton_Click(sender As System.Object, e As System.EventArgs)
MessageBox.Show("hello")
End Sub
This is assuming Form2
has a button named Button1
.
Upvotes: 1
Reputation: 9991
You can achieve this by setting the DialogResult
of the frmMain
.
Public Class frmMain
Inherits Form
Private Sub _ButtonContinueClick(sender As Object, e As EventArgs) Handles ButtonContinue.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub ButtonExitClick(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
End Class
Then change the LoadExtension
method to something like this:
Public Sub LoadExtension()
'various code...
Using form As New frmMain()
If (form.ShowDialog() <> Windows.Forms.DialogResult.OK) Then
Exit Sub
End If
End Using
'various code...
End Sub
Upvotes: 6
Reputation: 863
The simplest way is to use dialogresult to let the LoadExtension function know which button was pressed.
Upvotes: 1