Jump to content
Xtreme .Net Talk

passing values between forms


Recommended Posts

Guest jhoga
Posted

I'm 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.

Guest jhoga
Posted

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

 

?????

  • 2 weeks later...
Guest Andrejko
Posted

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 don't know how to reference the already active instance of the form. :(

Guest TheIrishCoder
Posted

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 it's controls now with ease but it's 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 can't 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.

  • *Gurus*
Posted

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 weren't for VB6 allowing this nobody would have a problem. I should write a tutorial on it or something.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Guest TheIrishCoder
Posted
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.
Guest Andrejko
Posted

simple?

 

divil,

this would be quite simple to people who haven't 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 I've dealt with Java for most of my career), but grasping the fact that Microsoft didn't 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/default.asp?url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp

Guest jhoga
Posted

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.

  • *Gurus*
Posted

Ok, I think the best way would be to remember that forms are just classes, nothing special about them. If you're 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:

 

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, you'll have to do this:

 

   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, you'll have to change it to the name of yours.

 

Hope this helps.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...