954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help with passing data from one form to another form

Hello!
I have a project to where I am making a pizza order for customers. My forms consist of a form for the pizza quantity and size (the cost is displayed in a lblDisplay.Text). I also have another form which calculates the cost of beverages.

I am now on the third form which calculates the cost of side items. I need to make it to where when the user clicks the submit button that the total of the side items is displayed along with adding the total of the pizzas and beverages of the other two forms. I dont know how to grab data from other forms.

I can make the other forms load by using the

Dim XXXX as new frmXXX

If my question is not clear enough, please let me know.

Adam

wisorac
Newbie Poster
1 post since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

Hi Adam,

I will give the method , regardless about your project take the method then apply it with any project

There two common methods is used for passing data between forms

The first one is by creating a properties in the form that you want to pass the data on it

for example if you have two forms the the first one (name it Form1) include one textbox control and button , and when pressing button you want to pass the textbox text to the second form (name it Form2)

the property shold be like :

Note these codes in Form2

Private _passedText As String

Public Property [PassedText]() As String
            Get
                Return _passedText
            End Get
            Set(ByVal Value As String)
                _passedText= Value
            End Set
        End Property



Then , when you are declaring Object from Form2 you just set The Value before Showing the Form

in the Form1 and in Button1 Click event write these lines :

Dim Obj As New Form2
Obj.PassedText = TextBox1.Text
Obj.Show


by this way you have the text in the Form2 - variable _passedText

hope you get it , if you didnt I am here

The Second Method by Adding parameters to the Form that you need to pass the data on it in Sub New - I didnt like it , the Properties is better


Good Luck

Ammar Gaffar
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 1
 

hi


i want to pass a text from a textbox in form1 to a label in form2 , but the text in form1 is retrieved from a database. how could i do it??:rolleyes:



Edit ...... thanx i solve the problem

DATABASE
Light Poster
32 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Hi Ammar Gaffar,

If I want to pass around 10 data from form1 to form2, it iz I still can using this method? or got other method to pass more than 1 data from form to form?

khwo
Newbie Poster
12 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Hi KhWo ,

It dosnt matter how many Variables you need to Pass , just create a property for each variable , Object , or any other types

Good Luck

Ammar Gaffar
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 1
 

*Bump*
Do this also work in VB 6? I'm new to the language and tested out your code but I only experience alot of failures.

Thx

Zhoot
Light Poster
26 posts since Jan 2008
Reputation Points: 10
Solved Threads: 3
 

in vb 6 you can access it directly

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Thank you!

So if I create a variable in Form1, then it will automatically exist in Form2 once it opens? Cool!

Zhoot
Light Poster
26 posts since Jan 2008
Reputation Points: 10
Solved Threads: 3
 

nice to hear that.
happy coding friends...:)

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Hi Adam,

I will give the method , regardless about your project take the method then apply it with any project

There two common methods is used for passing data between forms

The first one is by creating a properties in the form that you want to pass the data on it

for example if you have two forms the the first one (name it Form1) include one textbox control and button , and when pressing button you want to pass the textbox text to the second form (name it Form2)

the property shold be like :

Note these codes in Form2

Private _passedText As String

Public Property [PassedText]() As String
            Get
                Return _passedText
            End Get
            Set(ByVal Value As String)
                _passedText= Value
            End Set
        End Property

Then , when you are declaring Object from Form2 you just set The Value before Showing the Form

in the Form1 and in Button1 Click event write these lines :

Dim Obj As New Form2
Obj.PassedText = TextBox1.Text
Obj.Show

by this way you have the text in the Form2 - variable _passedText

hope you get it , if you didnt I am here

The Second Method by Adding parameters to the Form that you need to pass the data on it in Sub New - I didnt like it , the Properties is better

Good Luck

Hi i tried this code and it really works.But without obj.Show it didnt.
Whats the function of obj.Show here?

fawadkhalil
Junior Poster in Training
88 posts since Dec 2008
Reputation Points: 10
Solved Threads: 2
 

Obj As New Form2
Obj.PassedText = TextBox1.Text
Obj.Show

Hi i tried this code and it really works.But without obj.Show it didnt.
Whats the function of this line of code?

fawadkhalil
Junior Poster in Training
88 posts since Dec 2008
Reputation Points: 10
Solved Threads: 2
 

obj.show
makes the text entered in form1 appear visibly in form2

sacarias40
Junior Poster
113 posts since Nov 2008
Reputation Points: 11
Solved Threads: 4
 

Hi there,

I am using VB.Net 2003
Thank you for your advice and suggestions. I tried it out and it's working. You are just awesome sharing information with newbie like me.

Have a Good Day and Thanks.

Cheers,
Lennie

LennieKuah
Light Poster
47 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks , Very Simple and Useful Code

KalaBandar
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I'm glad you got it helpful. If you want to ask a question, start your own thread.

Thread Closed.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You