Member Avatar

Hi there,

i have two froms all ready made in design mode. how do i transfer data between them.

the program starts in Form1, the user selects which option to use and then goto Form2 to calculate.
when finished go back to Form1, how do i get the result back to Form1.

Thank You

When you call form2 create a new instance of it, then when it closes you can still access the values in any of the properties and/or controls. A good way to tell when the form closes is by using the ShowDialog method which returns a dialog result when the form closes. Something like this:

Dim NewForm2 As New Form2
Dim result As DialogResult = NewForm2.ShowDialog
If result = DialogResult.OK Then
    'Access the controls like this:
    'txtResults.Text = NewForm2.txtCalculate.Text
    'Do other stuff if the form is cancelled
End If

you should clear out the some data that you want to send to form 2 so that we can clear with it so and help you...
Then Too I think this will help you
this is just an example as you don't mention anything so try to understand something from here...

Let in the form1: you are having the Combobox which having the language option...
English, spanish, hindi, french, UK English etc..

ok now the Form 2 having the label1 as language:
so when you select the English then type the code as:
Form2.label1.text = "English"
if spanish then
Form2.label1.text = "Spanish"

And you can close any form using me.close,
but be sure that you have select the application last closed... in projcet settings

Member Avatar
Public Class Form1
    Public Shared Frm1Month As Decimal

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        Dim ItemSelect As String
        Dim Var As Decimal

        ItemSelect = ComboBox1.Text

        Select Case ItemSelect
            Case "Calendars"
                'Goto Function "Date to Days"
                'Come back with Answer and Display on Form1
                Var = "Read about Calendars"

            Case "Converting the date to the Day Number"
                'Goto Form2 to Calculate
                'Come back with Answer and Display on Form1
                Form2.DATEGroupBox.Visible = True

            Case "Julian Day Numbers"
                'Goto Function "Converting Julain Day Number to Calendar Date
                'Come back with Answer and Dispaly on Form1
                Var = "What is the Julian Day"

            Case "Converting Julian Day Number to the Calendar Date"
                'Goto Function "Converting the Julian Day Number to the Calendar Date
                'Come back with Answer and Display on from1

            Case "Finding the Day of the Week"
                'Goto Function "Finding Day of the Week
                'Come back with Answer and Display on Form1

            Case "Converting HRS.MINS.Secs. to Decimal Hours"
                'Goto Function "Converting HRS.MINS.Secs. to Decimal Hours"
                'Come back with Answer and Display on Form1

            Case "Converting Decimal Hours to HRS.MINS.SECS."
                'Goto Function "Converting Decimal Hours to HRS.MINS.SECS."
                'Come back with Answer and Display on from1

        End Select

    End Sub

this is my code for Form1

Many thanks for all your replys.

i think you can use this type of data on same formso there will be no need to send any data...
then too.
The way what you using to calculate the data and then the answer you get in the form 2 will be on textbox of form2

so type the code on Form2_closing:

Form1.textbox1.text = textbox1.text

What we do is: Just send the answer to the form 1.

i don't know how to calculate the answer so I explained in short

Member Avatar

Thanks for you reply.
i am using a varible to calculate the answer and using another to pass the answer to Form1.(which is Frm1Month).Have been reading about "Module" method, so am using varables are now available on both forms.But, am having problems of seing it on Form1. I put a label on Form1, and have the following code...

label1.text = Frm1Month

But all i get is the number zero.
if i put

Frm1Month = Frm2Month

and assign it to a label on Form2, it shows the correct value.

not sure what is happening....

you should refresh:
add this refresh button code and timer (or only timer with enable to interval 1ms)
and add this code in the timer.

if i am not wrong then do first step :
form1month = form2month
label1.text = form1month
if the form1month is equal to form2month then
what there the difference in both whether you use label1.text = form1month or label1.text = form2month

if I am wrong then can you upload the project file?
and explain me with the comments

Member Avatar

Hi there,
i think that the comments explain them selfs. :-)

Yes these var.(Frm1Month,Frm2Month) carry the same value. they are named differant, so that i can tell from what Form they are from.

Form2 is where i do all the calculations.

so Frm2Month = for example 324
so i get Frm1Month = Frm2Month

Form1 is where i ask the user what calculations they want to do, and then place all the answers in a ListViewBox.


The problem i an having at the moment, it that Form2 gives me an answer as above Frm2Month=324

now i get Frm1Month = Frm2Month

if i put both of these var in an Label1.text = Frm2Month
it comes up with the answer which is 324.
But if i goto Form1 , and, put Frm1Month in an Label1.text = Frm1Month the answer comes up "Zero"

and i have no idea WHY?

I do not understand your last reply Deep Modi, what should i refresh.?

Member Avatar

Hi everyone,

Have figured a way around my problem... :-))
in Form2 i have put the following line of code...

Form1.TextBox1.Text = Frm2Month

and i get the correct answer in the Textbox1.Text....

Thanks for all your replys....
If anybody has another way to do this, i am allways willing to learn.. at times you might have to lead me step by step. hehehehe

I'm not a fan of tight coupling between forms such that you access internals of other forms directly. My preferred method is to communicate data with either constructor parameters, properties, or events. For example (unnecessary stuff omitted):

Public Class Form1
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Using dlg As New Form2
            ' Form2 handles all of its own UI stuff
            If dlg.ShowDialog() = DialogResult.OK Then
                ' Loose coupling, use the public interface without caring about internals
                Textbox1.Text = dlg.Month.ToString()
            End If
        End Using
    End Sub
End Class

Public Class Form2
    Private m_month As Decimal
    Property Month() As Decimal
            Return m_month
        End Get
    End Property
End Class

I'd also avoid using a TextBox for numeric values because then you have to worry about validation. A NumericUpDown handles all of that for you and should be preferred unless you have unusual formatting requirements.

Member Avatar

Thanks for your reply. :-)
can you explain your code a bit, so that i can understand it ....

as to NumericUpDown, it does not like decimal,double. as all my vars. are decimal......hmm

can you explain your code a bit, so that i can understand it ....

It's pretty straightforward, the month value is passed around using properties rather than having one form look at another form's controls or private variables. Is there any specific part you don't understand that I can explain in more detail?

as to NumericUpDown, it does not like decimal,double.

NumericUpDown can be configured for floating point precision with the DecimalPlaces property. The Value property evalutes to a decimal, which can be cast to double if need be.