passing values between forms

  • Thread starter Thread starter jhoga
  • Start date Start date
J

jhoga

Guest
Im having trouble setting the value of fields in a user form based on a selection made in a second form. The first form brings up an SQLServer base datagrid. In form2 I have set the grid click event as follows:
Dim myRow As Integer
Dim myCol As Integer
Dim myVerification As String

myRow = myGD.CurrentRowIndex
strSelLastName = myGD.Item(myRow, 1)
strSelFirstname = myGD.Item(myRow, 2)
strSelMI = myGD.Item(myRow, 3)
strSelAddress = myGD.Item(myRow, 4)
strSelCity = myGD.Item(myRow, 5)
strSelZip = myGD.Item(myRow, 6)

Now i want to set form1.textbox1.text = strSelLastNAME
I have set strSelLastName as Public string in a module.
Ahelp on how to reference a control in anougher form would be great.
 
All you need is a reference to an instance of the other form, and provided the control(s) are declared public, you should be able to access them just fine.
 
I have tried
To reference the other form...

Public AFormInstance As New Receipts.frmReceipts()
.....

AFormInstance.txtReceivedFrom.Text = strSelLastName

does not work...

But, putting this code on the click event of a button on form1 works fine:

txtReceivedFrom.Text = strSelLastName
txtFName.Text = strSelFirstname
txtMI.Text = strSelMI
txtAddress.Text = strSelAddress
txtCity.Text = strSelCity
txtState.Text = strSelState
txtZip.Text = strSelZip

?????
 
You are really creating a new form...

I have had the same problem before
Your code:
Public AFormInstance As New Receipts.frmReceipts()

Creates a new instance of the frmReceipts form.
Try to add frmReceipts.show() after this line of code and you will see what I mean.
I have the same problem though. I dont know how to reference the already active instance of the form. :(
 
I got around this by declaring public references to the forms including the startup form in a module or in a class if you wish.

Module Module1
Public frm As New Form1()
Public Sub Main()
Application.Run(frm)
End Sub
End Module

This starts up my first form with a public scope. I can reference its controls now with ease but its better to make your controls private and expose public read only properties:-

Public ReadOnly Property t1text()
Get
Return TextBox1.Text
End Get
End Property

Now I cant directly affect the textbox on the original form but I can get the contents of it.

Note using a Sub Main like above requires you to change the project properties to startup on Sub Main rather than form1.
 
Just pass an instance of the first form to the second when you create it!

For crying out loud, this is really quite simple, if it werent for VB6 allowing this nobody would have a problem. I should write a tutorial on it or something.
 
If as you say it was simple then nobody would be asking. It might be simple for people at a certain level and ability. In any case posting example code would surely settle the matter.
 
simple?

divil,
this would be quite simple to people who havent dealt with earlier versions of VB.
This is a rather significant paradigm shift, and it is no surprising to see so many developers stumped by it...myself included...the main source of confusion for me was not figuring out how to get around it (after all Ive dealt with Java for most of my career), but grasping the fact that Microsoft didnt provide an adequate workaround for it in the first place.

ps There already are tutorials which explain this quite effectively:
http://msdn.microsoft.com/library/d...ltipleFormsInVisualBasicNETUpgradingToNET.asp
 
broken link

Sorry, just noticed that the link I posted previously was broken.
Here it is again:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp
 
Thanks for every ones help.
I got around the problem by changing the from2.show to form2.showdialog.

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
strSC = txtRecFrom.Text
If strSC <= "A " Then
MessageBox.Show("Must enter name into the last name box")
Return
End If
Dim findit As New findit()
findit.ShowDialog()
txtRecFrom.Text = strSelLastName
txtFName.Text = strSelFirstname
txtMI.Text = strSelMI
txtAddress.Text = strSelAddress
txtCity.Text = strSelCity
txtState.Text = strSelState
txtZip.Text = strSelZip

End Sub

Of course all the variables were set as public in a module.
divil,
I appreciate you comments, but sometimes a little bit of code would be easier to understand for us slower folk.
 
Ok, I think the best way would be to remember that forms are just classes, nothing special about them. If youre running a method on a class, and you want that class to be able to interact with the calling class, it must have a reference to the instance you called it from.

Personally I think the best way is to do it in the constructor of the secondary form:

Code:
Public Sub New(frmCreatedBy As Form)
    m_ParentForm = frmCreatedBy
    ...
End Sub

Modify your constructor by putting in that parameter, and declare a class-wide private member variable called m_ParentForm (as Form, of course).

Now, when you declare a new instance of your secondard form to show, from your primary, youll have to do this:

Code:
    Dim X As frmSecondary
    X = New frmSecondary(Me)
    X.ShowDialog()

And there you have it, since you passed the instance of the form you created it from (using the Me keyword) you now have a reference to the primary form in the secondary form.

Note that the type is just Form. If you want to access methods and properties specific to your form and not just every class which inherits Form, youll have to change it to the name of yours.

Hope this helps.
 
Back
Top