2

I have a form with a text box, button and a public shared property. The button displays another form with a data grid view. When a row is clicked on in the data grid view it takes the value that was selected and assigns it to the public shared property of the original form, as well as closes the form

Private Sub dgvAllSku_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvAllSku.CellContentDoubleClick
    frmMain.Sku = dgvAllSku.Rows.Item(e.RowIndex).Cells(0).Value
    Me.Close()
End Sub

Now what I want to have happen is, as this form closes I want the value (frmMain.Sku) to fill the text box on the original form. I was hoping the GotFocus event would be fired again when the data grid view form closes and I could use that event to assign the text box the value of the public property, but that got focus event isn't firing when the form closes.

Private Sub frmMain_Activated(sender As Object, e As EventArgs) Handles Me.GotFocus
    txtSku.Text = frmMain.Sku
End Sub

How do I accomplish this?

Brandon
  • 915
  • 4
  • 23
  • 44

2 Answers2

4

If your secondary form is a modal then expose a public property on that form. In order to close that modal just call form1.DialogResult = DialogResult.OK. In your parent form do something like

Dim childModal = new ChildModal
Dim result childModal.ShowDialog() 
If result = OK then Me.Text1.Text = childModal.ResultValue

Good Luck...

jcwrequests
  • 1,132
  • 1
  • 7
  • 13
2

Probably a lot of ways, but the quickest way I can think of is just expose the txtSku.Text as a public property and let the other window just set it itself:

'Define in your main form
Public Property SkuText() As String
Get
    Return txtSku.Text
End Get
Set(ByVal value As String)
    txtSku.Text = value
End Set
End Property

Then just set the text from your other window:

Private Sub dgvAllSku_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvAllSku.CellContentDoubleClick
    frmMain.SkuText = dgvAllSku.Rows.Item(e.RowIndex).Cells(0).Value
    Me.Close()
End Sub  

You could also raise an event that the other form subscribes to, or if you don't have any reason to still be able to use both forms at once, just use ShowDialog and execution in the main form will stop and wait for the user to select a cell, then return and easily pull the selected value from the form it just showed.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138