4 Replies - 2764 Views - Last Post: 07 April 2011 - 06:38 PM Rate Topic: -----

#1 NewbieProgrammer88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-April 11

Bringing values from one form to another

Posted 07 April 2011 - 04:10 PM

How do you bring values from one form to another? Ive a car price which i need to carry over to another form as i have check boxes which if selected will change the price. How do i solve this?

On my 1st form this is the code i have when the user has selected a car:
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        Dim decCarCost As Decimal
        decCarCost = lblValue.Text
        If MessageBox.Show("Does the customer want to customise the car?", "Deals On Wheels", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
            Dim newFrmCust As New frmCustomise
            newFrmCust.Show()
            Me.Hide()
        ElseIf Windows.Forms.DialogResult.No Then
            Dim finishfrmFin As New frmFinal
            finishfrmFin.Show()
            Me.Hide()
        End If
    End Sub

On my next form where i can check boxes which update the price i have this code but it doesnt seem to be working:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim decCarCost As Decimal
        Dim decHandsfree As Decimal = 30D
        Dim decSatNav As Decimal = 90D
        Dim decMetallic As Decimal = 500D
        Dim decPlayer As Decimal = 65D

        If radHands.Checked Then
            decCarCost = decCarCost + decHandsfree
        ElseIf radPlayer.Checked Then
            decCarCost = decCarCost + decPlayer
        ElseIf radPaint.Checked Then
            decCarCost = decCarCost + decMetallic
        ElseIf radSat.Checked Then
            decCarCost = decCarCost + decSatNav
        End If

       
    End Sub




The price isnt being added for some reason?

Is This A Good Question/Topic? 0
  • +

Replies To: Bringing values from one form to another

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Bringing values from one form to another

Posted 07 April 2011 - 04:42 PM

First this is a VB code not C#. But you have two way to do it,
First; make your variable global by making it public outside any methode(or put it in module)
Second; use
formName.variableName

Was This Post Helpful? 0
  • +
  • -

#3 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Bringing values from one form to another

Posted 07 April 2011 - 04:50 PM

Pass it as an argument when you instantiate the other form. Or you can already use the object sender argument, cast it as Form1 or whatever the main form is, and then call the variable like that. Also this is not C# as the above post says -_-
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Bringing values from one form to another

Posted 07 April 2011 - 04:55 PM

Another thing I want to remind you is that, decCarCost is declared inside a procedure, so these are two different decCarCost. The one in Button add click event subroutine is different from the one in button select, so changing the one does not effect the other.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: Bringing values from one form to another

Posted 07 April 2011 - 06:38 PM

There are tutorials exactly for passing values between forms & classes.

These are in the C# area, but the concepts apply and reading C# and writing VB isn't tough.


Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes
Debugging tutorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1