3 Replies - 5435 Views - Last Post: 27 April 2012 - 05:46 PM Rate Topic: -----

#1 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

Shopping cart sending data from listbox to another on a diferent form

Posted 26 April 2012 - 04:28 PM

Ok i havent had problems in visual basic all the ay up to this point. I have been given a shopping cart program that I must use:

a MainForm, MP3From, AlbumForm, and a module

I finally got my program to send data from the listbox on my MP3Form to the listbox on my MainForm but something isnt right still. I need to be able to click an an item from the lstMP3(listbox on MP3Form) to lstShoppingCart(listbox on MainForm) multiple times without having to close the MP3Form everytime to add the items to lstShoppingCart. Here is what I got so far on my MainForm:
 Public Class MainForm
    Dim WithEvents frmMP3 As New MP3Form
    Dim WithEvents frmAlbum As New AlbumForm

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstShoppingCart.SelectedItem.remo()
    End Sub

    Private Sub MP3AlbumToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MP3AlbumToolStripMenuItem.Click

        frmMP3.ShowDialog()

        lstShoppingCart.Items.Add(selection)

        lblSubtotal.Text = dblSubTotal.ToString()


    End Sub

    Public Sub lstShoppingCart_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstShoppingCart.SelectedIndexChanged

    End Sub
End Class



here is what I got on MP3Form:
 Public Class MP3Form

    Private Sub btnAddBookToCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBookToCart.Click

        selection = lstMP3Album.SelectedItem

        If selection = strTresM Then
            dblSubTotal = dblSubTotal + decTres
        ElseIf selection = strSoulM Then
            dblSubTotal = dblSubTotal + decSoul
        ElseIf selection = strInstrumentalM Then
            dblSubTotal = dblSubTotal + decInstrumentalM
        ElseIf selection = strDarkSideM Then
            dblSubTotal = dblSubTotal + decDarkSideM
        ElseIf selection = strFandangoM Then
            dblSubTotal = dblSubTotal + decFandangoM
        ElseIf selection = strSeventhM Then
            dblSubTotal = dblSubTotal + decSeventhM
        ElseIf selection = strInSearchM Then
            dblSubTotal = dblSubTotal + decInSearchM
        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        Me.Close()

    End Sub
End Class


and here is my module:
 Module MusicModule
    ' This module contains information about the various albums available for purchase.
    ' Global constants
    Public Const dblTaxRate As Double = 0.0975
    Public Const dblShipping As Double = 10.0

    ' Names of the available MP3 Albums
    Public Const strTresM As String = "Tres Hombres ZZ TOP as string"
    Public Const strSoulM As String = "Soul Kiss Tom Duda"
    Public Const strInstrumentalM As String = "Instrumental Telepathy Tom Duda"
    Public Const strDarkSideM As String = "Dark Side of the Moon  Pink Floyd"
    Public Const strFandangoM As String = "Fandango!   ZZ TOP"
    Public Const strSeventhM As String = "Seventh Sojourn  Moody Blues"
    Public Const strInSearchM As String = "In Search of the lost Chord  Moody Blues"

    ' Prices of the avialable MP3 Albums
    Public Const decTresM As Double = 6.95
    Public Const decSoulM As Double = 7.95
    Public Const decInstrumentalM As Double = 7.95
    Public Const decDarkSideM As Double = 6.95
    Public Const decFandangoM As Double = 6.95
    Public Const decSeventhM As Double = 7.97
    Public Const decInSearchM As Double = 7.95

    ' Names of the avialable Vinyl Albums
    Public Const strTresV As String = "Tres Hombres ZZ TOP as string"
    Public Const strSoulV As String = "Soul Kiss Tom Duda"
    Public Const strInstrumentalV As String = "Instrumental Telepathy Tom Duda"
    Public Const strDarkSideV As String = "Dark Side of the Moon  Pink Floyd"
    Public Const strTheWallV As String = "The Wall Pink Floyd"
    Public Const strDaysV As String = "Days of the Future Past  Moody Blues"
    Public Const strSeventhV As String = "Seventh Sojourn  Moody Blues"
    Public Const strInSearchV As String = "In Search of the lost Chord  Moody Blues"


    ' Prices of the available  Vinyl Albums
    Public Const decTres As Double = 16.95
    Public Const decSoul As Double = 15.0
    Public Const decInstrumental As Double = 15.0
    Public Const decDarkSide As Double = 19.95
    Public Const decTheWall As Double = 19.95
    Public Const decstrDays As Double = 22.95
    Public Const decSeventh As Double = 22.95
    Public Const decInSearch As Double = 18.95


    ' Global variables
    Public dblSubTotal As Double
    Public dblTax As Double
    Public dblTotal As Double
    Public strList As String
    Public dblListCost As Double

    ' public functions

    Public selection As String


    


End Module



I have not started on my AlbumForm cause it should be the same as my MP3Form

This assignment makes me mad cause we have not even covered half the stuff I am having to use in this form at all this semester. If i can get a little explanation on instantiate, inheritance, and declaring and object variable and how its used to transfer data from one to the other.

Is This A Good Question/Topic? 0
  • +

Replies To: Shopping cart sending data from listbox to another on a diferent form

#2 tlhIn`toq  Icon User is offline

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

Reputation: 5578
  • View blog
  • Posts: 11,918
  • Joined: 02-June 10

Re: Shopping cart sending data from listbox to another on a diferent form

Posted 26 April 2012 - 07:26 PM

Quote

If i can get a little explanation on instantiate, inheritance, and declaring and object variable and how its used to transfer data from one to the other.


That's the first 3 chapters of a textbook. All of that is not going to be explained to you in a thread. I refuse to believe this stuff is not in your course textbook. You might have to read it and not rely on class lecture notes.

If what you really need is an explaination of the concepts of classes maybe this will help:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of a the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}


Quote

I finally got my program to send data from the listbox on my MP3Form to the listbox on my MainForm but something isnt right still.

See FAQ # 3. (Click the SHOW button below)


Quote

This assignment makes me mad cause we have not even covered half the stuff I am having to use in this form at all this semester.

See FAQ # 26. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

Re: Shopping cart sending data from listbox to another on a diferent form

Posted 27 April 2012 - 10:47 AM

I appreciatte the help and this is the last place I ask questions. I researched for quite awhile before i came here. and no objects come in at CH9 inheritance comes in at CH 12 and I havent find one word about instantiate in this whole book. and We just did CH6 and this was a handwritten assignment that he handed out so its not even out of the book. No one in my entire class got it. At this point it was already turned in. Now im just doing it just so i know what Im doing. So im gonna do some more research I guess and Ill use your explanation and get back to you if I still cant get it. Once again it is probably something obvious that just hasnt clicked yet. And I am doing all this with TBI that I got from a piece of shraphnel to my frontal lobe in Iraq. So be gentle Im doing the best I can

and the classes you are talking about dont come in til CH 13. I know how to do all this in c++ but the syntax is way diferent to me.
Was This Post Helpful? 0
  • +
  • -

#4 pla2pimp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 27-February 12

Re: Shopping cart sending data from listbox to another on a diferent form

Posted 27 April 2012 - 05:46 PM

Ok well Ive been working on it for quite a while now. My error now says "index is outside the bounds of the array" I have changed it dramtaically based off of research and examples I have seen. But so,ething about the array is wrong and I dont kno how it is.

here are the codes I got for the module:
 Module MusicModule
    ' This module contains information about the various albums available for purchase.

    ' Global constants

    'Sales tax on total cost
    Public Const decSALES_TAX As Decimal = 0.0975D
    'Shipping Cost on each item
    Public Const decSHIPPING_CHARGE As Decimal = 10.0

    ' Names of the available MP3 Albums
    Public mp3Albums() As String = {" Tres Hombres ZZ TOP ", " Fandango!   ZZ TOP ", " Soul Kiss Tom Duda ", " Instrumental Telepathy Tom Duda ", " Dark Side of the Moon  Pink Floyd ", " Seventh Sojourn  Moody Blues ", " In Search of the lost Chord  Moody Blues "}
    ' Prices of the avialable MP3 Albums
    Public mp3Prices() As Decimal = {6.95D, 6.95D, 7.95D, 7.95D, 6.95D, 7.97D, 7.95D}

    ' Names of the avialable Vinyl Albums
    Public vinylAlbum() As String = {" Tres Hombres ZZ TOP ", " Soul Kiss Tom Duda ", " Instrumental Telepathy Tom Duda ", " Dark Side of the Moon  Pink Floyd ", " The Wall Pink Floyd ", " Days of the Future Past  Moody Blues ", " Seventh Sojourn  Moody Blues ", " In Search of the lost Chord  Moody Blues "}

    ' Prices of the available  Vinyl Albums
    Public vinylPrice() As Decimal = {16.95D, 15D, 15D, 19.95D, 19.95D, 22.95D, 22.95D, 18.95D}



    ' Global variables
    Public decSubTotal As Decimal         'Subtotal of charges
    Public decTotal As Decimal            'Total Charges
    Public decShipping As Decimal         'Total Shipping
    Public decTax As Decimal              'Tax Percentage

    Public frmMain As New MainForm
    Public frmMP3 As New MP3Form
    Public frmAlbum As New AlbumForm

    Public Function CalcSubtotal(ByVal decCart_Subtotal As Decimal, ByVal decSubtotal As Decimal)

        Return decCart_Subtotal = decSubtotal

    End Function

    Public Function CalcTax(ByVal decSubtotal As Decimal) As Decimal 'ByVal decTax As Decimal,

        Return decSubtotal * decSALES_TAX

    End Function

    


    Public Function CalcTotal(ByVal decCart_Charges As Decimal) As Decimal
        Return decTotal + decSALES_TAX

    End Function

End Module



Main:
 Imports System.Windows.Forms

Public Class MainForm
    
   

    Private Sub mnuReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReset.Click
        'Reset the form.
        lblSubtotal.Text = String.Empty
        lblTax.Text = String.Empty
        lblShipping.Text = String.Empty
        lblTotal.Text = String.Empty
        lstShoppingCart.Items.Clear()
    End Sub
    Private Sub mnuMP3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMP3.Click

        frmMP3.Show()

    End Sub
    Private Sub mnuVinyl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuVinyl.Click

        frmAlbum.Show()

    End Sub
    Public Sub DisplayCharges()
        'This procedure calculates the tax, shipping charge and total cart purchase.
        Dim decAlbumsTotal As Decimal = 0
       
        decAlbumsTotal += vinylPrice(frmMP3.lstMP3Album.SelectedIndex)

        decAlbumsTotal += mp3Prices(frmAlbum.lstVinylAlbum.SelectedIndex)
           

        'Dim albumCount As Integer = lstShoppingCart.Items.Count
        'Display the Data.
        lblSubtotal.Text = decAlbumsTotal.ToString("c")
        lblTax.Text = (decAlbumsTotal * decSALES_TAX).ToString("c")
        decAlbumsTotal += decAlbumsTotal * decSALES_TAX
        lblShipping.Text = decSHIPPING_CHARGE.ToString("c")
        decAlbumsTotal += decSHIPPING_CHARGE
        lblTotal.Text = decAlbumsTotal.ToString("c")
    End Sub
    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        'End the application
        Me.Close()
    End Sub
    Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        'Displays an about box.
        MessageBox.Show("The Shopping Cart allows customers to shop for both Vinyl and MP3 Albums.")
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        'Remove Items from List Box
        If lstShoppingCart.SelectedIndex <> -1 Then
            lstShoppingCart.Items.RemoveAt(lstShoppingCart.SelectedIndex)
        End If
        DisplayCharges()
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class



MP3:
 Public Class MP3Form


   
    Private Sub btnAddMP3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMP3.Click
       

        'frmMain.lstShoppingCart.Items.Add(mp3Albums(intChoice))
        frmMain.lstShoppingCart.Items.Add(lstMP3Album.SelectedItem.ToString)
        frmMain.DisplayCharges()

    End Sub

    Private Sub btnCloseMP3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseMP3.Click
        'close this form
        Me.Close()
    End Sub

    Private Sub MP3Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x = 0 To UBound(mp3Albums)
            lstMP3Album.Items.Add(mp3Albums(x) & " " & String.Format("{0:C}", mp3Prices(x)))
        Next x
    End Sub
End Class


vinyl:
Public Class AlbumForm

    Dim frmMain As New MainForm

    Private Sub btnCloseAlbums_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseAlbums.Click
        'Close this form
        Me.Close()
    End Sub
    Private Sub AlbumForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x = 0 To UBound(vinylAlbum)
            lstVinylAlbum.Items.Add(vinylAlbum(x) & " " & String.Format("{0:C}", vinylPrice(x)))
        Next x
    End Sub
    Private Sub btnAddAlbum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddAlbum.Click
        frmMain.lstShoppingCart.Items.Add("A" & lstVinylAlbum.SelectedIndex & ": " & lstVinylAlbum.SelectedItem.ToString)
        frmMain.DisplayCharges()
    End Sub

   
End Class 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1