0

I have a form (let's call it 1) that opens a new form (2) where you can choose a client from a datagridview. When the user clicks accept in form (2), I create an object and I pass it to form (1). This is code from the accept button in form (2):

Private objCreateOrder As FCreateOrder = New FCreateOrder

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
     If selectedRow Is Nothing Then
         MessageBox.Show("Error.")      
     Else
         Me.Close()
         objCreateOrder.getClientObject(objClient)
     End If
End Sub

This is the function from form (1) that was called above:

Public Sub getClientObject(client As CClient)
     Dim objClient As CClient = New CClient
     objClient = client
     txtClient.Text = objClient._name.ToString + " " + objClient._surname.ToString
End Sub

When I debug I see that the object was passed correctly, and that the textbox has exactly the strings it needs. Also, if I add something like MessageBox.Show() to show anything just to check if the code runs what is inside the method it works, but the textbox doesn't show anything. What am I missing?

winter
  • 207
  • 1
  • 4
  • 13
  • Is FCreateOrder the form's class name? If yes then you are referencing the [Default Form Instance](https://stackoverflow.com/questions/4698538/why-is-there-a-default-instance-of-every-form-in-vb-net-but-not-in-c) and not the instance that you have displayed. – Steve Apr 08 '20 at 11:33
  • @Steve yes, FCreateOrder is form (1) 's name. How can I fix it? – winter Apr 08 '20 at 11:36
  • In the second form pass a reference to the first form (for example in the form constructor) then use that instance instead of the default FCreateOrder. You really need to understand this problem if you want to write bug free winforms code in VB.NET. – Steve Apr 08 '20 at 11:38
  • @Steve thanks a lot for your help. I fixed it, but the textbox is still empty. – winter Apr 08 '20 at 11:42
  • Please add the fixed code to your question (without removing the old one) – Steve Apr 08 '20 at 11:47
  • @Steve I have updated the code – winter Apr 08 '20 at 11:54

1 Answers1

0

The current code creates an instance of FCreateOrder (together with its controls) and then uses that instance to set the value for txtClient. But this new instance is not the one that has called the form with the client selection. It is that instance that needs to be updated with the text choosen.

You could pass the correct instance inside the form with the client selection in the exact point where you create the client form.

....
Dim formClient as FClientSelection = new FClientSelection(Me)
formClient.ShowDialog()
...

The instance Me is passed to the FClientSelection constructor and you can save it inside a local variable

Dim currentOrder as FCreateOrder
Public Sub FClientSelect(fOrder As FCreateOrder)
    currentOrder = fOrder
End Sub

finally you use currentOrder to call the method that fixes the customer text

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
     If selectedRow Is Nothing Then
         MessageBox.Show("Error.")      
     Else
         Me.Close()
         currentOrder.getClientObject(objClient)
     End If
End Sub

Said that I want that you consider now how these two form classes are inexorably connected. This is not a good design. It is better to use a public property inside the formClient and set that property inside the form before closing it. Your calling form can read that property and do its works.

Steve
  • 213,761
  • 22
  • 232
  • 286