getting code to run when returning from dialogbox AUTOMATICALLY

JamesBowtell

Member
Joined
Feb 26, 2009
Messages
16
Programming Experience
3-5
Hey

i got two forms

form1 (frmUserDetails)

form2 (frmSearchForm)

when i click the search button in form1, form2 appears.

i get the information i want from form 2 and store it in a variable.

when i close form2 and form1 is active again i want this code to run automatically:

VB.NET:
Me.userIDsearch = frmUserSearch.NuNameTB

        Try

            'TODO: This line of code loads data into the 'SoftwareLookupDataSet.tbl_software' table. You can move, or remove it, as needed.
            Me.Tbl_softwareTableAdapter.Fill(Me.SoftwareLookupDataSet.tbl_software)

            'TODO: This line of code loads data into the 'ITSoftwareLoansSDataSet.tbl_software_loan' table. You can move, or remove it, as needed.
            Me.Tbl_software_loanTableAdapter.Fill(Me.ITSoftwareLoansSDataSet.tbl_software_loan)

            'TODO: This line of code loads data into the 'ITSoftwareLoansSDataSet.tbl_user' table. You can move, or remove it, as needed.
            Me.Tbl_userTableAdapter.FillByUserID(Me.ITSoftwareLoansSDataSet.tbl_user, Me.userIDsearch)

            Label1.Text = frmUserSearch.NuNameTB

        Catch ex As System.Exception

            System.Windows.Forms.MessageBox.Show(ex.Message)

        End Try

how would i get the above code to run when the focus is back on form1?

thanks
 
Open the Searchform as a dialog box and then handle the DialogResult when it returns.

VB.NET:
If frmSearch.ShowDialog() = DialogResult.Cancel Then
     'Do something if the Search form was cancelled
Else
     'Do this if it returns something other than the Cancel option

End If

Alternately you can use a Select Case statement for this if you want to run different code for different results. You just have to remember on the Search form you need to set the DialogResult before closing it. (did this from memory so it may need some tweeking)
 
He just did.

On your frmSearch, make sure every button like OK, Cancel, has its DialogResult property set to soemthing sensible

Then make sure when you show the frmSearch as a dialog, you call ShowDialog()


Code will pause at that point and wait for the dialog search form to be closed, at which point you check the result (what the user clicked) and if it was cancel, well.. probably return. Else, process the search
 
Another example:

I have 2 forms:
Form3 is used to edit some data
Form4 is used to verify some of that information

Seeing as isOk = False, Form4 is going to be opened. Here we can check the information and the click on the OK or CANCEL button. Once the Me.DialogResult is set on Form4, it is automatically closed. Form3 will now compare the returned DialogResult to Windows.Forms.DialogResult.Cancel, if they are the same then it will do something and exit the sub, if not the it will continue to process as though it hadn't been interupted.


Form3:

VB.NET:
Public Class Form3
    Dim isOk As Boolean = False

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        'Something to note: the Form.ShowDialog() can also be used with Select Case

        If Not isOk Then
            'need to verify something 
            Dim frmVerify As Form = New Form4
            If frmVerify.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
                'do something in case it was cancel
                ' then exit the sub without running any of the rest of the code
                Exit Sub
            End If

            'if frmVerify returns anything other than Windows.Forms.DialogResult.Cancel then whatever
            ' code is below here will run

        End If
    End Sub
End Class


Form4:
VB.NET:
Public Class Form4

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class
 
Form4:
VB.NET:
Public Class Form4

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class
Except you would rather set this property for the buttons in Designer than write any code at all. Use the DialogResult property in code only when you need to conditionally set the result.
Dim frmVerify As Form = New Form4
Since there usually only can be one instance of a dialog form showing at one time, you can use the default instance here instead of creating a new instance yourself and dispose it (which you also forgot to do), saving you more coding. Though if the dialog is requiring much resources you may have to remember to keep it clean.
 
Ohhh i get it now!

didnt realise that when i used frmsearch.showdialog() that it paused at that point and waits for a result...Makes alot more sense now!

Cheers for all your help!

James
 
Indeed, it was only recently that I bothered to start using it too! When any button that has a DialogResult set, is clicked and the form was ShHowDialog()ed, it automatically closes and returns the appropriate value.. Great way of keeping code tidy

Re "code pauses at that point" -> conceptually yes, but in implementation the behaviour is subtly different.. The thread that encounters the ShowDialog() call actually enters ShowDialog and is blocked inside it, processing window messages for the dialog form. When the dialog is closed the thread returns to the calling point. While this is only subtly different to how you understand it, it is an important distinction because the thread running the code doesnt (really) "pause" at any point; if it did the app would become unresponsive. Knowing that it is the same thread allows you to understand how programs execute and could avoid you making a cross threading problem in the future..
 
Back
Top