Question : Visual Basic - populating a varying text box from another form

I have one form CallingForm with two textboxes and related command buttons beside each.  So txtCompany and cmdCompany, and txtState and cmdState.  Both command buttons, when clicked, show the same form SelectForm with a single DataGrid listing CompanyName, State, etc.  The user then selects a record from the DataGrid in SelectForm.   I want SelectForm to return information back to CallingForm, populating the correct text box, based on which command button (and therefore textbox) was clicked in CallingForm.  

If cmdCompany was clicked in CallingForm, I want SelectForm's DataGrid selection to populate CallingForm's txtCompany text box.
If cmdState was clicked in CallingForm, I want SelectForm's DataGrid selection to populate CallingFroms' txtState text box.  

I know how to reference the column I need in the data grid and knew how to populate the text box back in CallingForm when I was only dealing with one text box, but now I'm struggling with how to reference the text box in CallingForm, now that I am dealing with a variable of sorts as to which text box in CallingForm I want to populate.

Answer : Visual Basic - populating a varying text box from another form

Display SelectForm as a modal dialog with "vbModal" and then grab the info from it back in CallingForm:

    Private Sub cmdCompany_Click()
        Dim sf As New SelectForm
        sf.Show vbModal ' <--- code in CallingForm STOPS here until "sf" is hidden
        ' ...grab some info directly from the HIDDEN "sf" instance...
        Me.txtCompany.Text = sf.txtSomething.Text
    End Sub

    Private Sub cmdState_Click()
        Dim sf As New SelectForm
        sf.Show vbModal ' <--- code in CallingForm STOPS here until "sf" is hidden
        ' ...grab some info from "sf"...
        Me.txtState.Text = sf.txtSomething.Text
    End Sub

In SelectForm, you simpy HIDE the form in response to the "OK" button being clicked:

    Private Sub cmdOK_Click()
        Me.Hide ' <-- this causes the "modal" form to HIDE and execution to RETURN to CallingForm AFTER the .Show vbModal call line
    End Sub
Random Solutions  
 
programming4us programming4us