Welcome to Dream.In.Code
Become a VB Expert!

Join 137,262 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!




Adding a selected item in one listbox to another listbox in a differen

2 Pages V  1 2 >  
Reply to this topicStart new topic

Adding a selected item in one listbox to another listbox in a differen

ramper04
13 Dec, 2006 - 01:15 PM
Post #1

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 10


My Contributions
This is for a shopping cart homework assignment. When the user selects a 'book' to add it to the cart the price is calculated and the book is supposed to show up in the listbox on the main form, but it doesnt. Any help on this one to why its not working or what I am doing wrong??


CODE
Private Sub btnPrintBooksAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintBooksAdd.Click
        If lstPrintBooks.SelectedIndex > -1 Then
            If lstPrintBooks.SelectedItem = "I Did It Your Way (Print)" Then
                MessageBox.Show("I Did It Your Way (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 11.95
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping

            ElseIf lstPrintBooks.SelectedItem = "The History of Scotland (Print)" Then
                MessageBox.Show("The History of Scotland (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 14.5
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            ElseIf lstPrintBooks.SelectedItem = "Learn Calculus in One Day (Print)" Then
                MessageBox.Show("Learn Calculus in One Day (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 29.95
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            ElseIf lstPrintBooks.SelectedItem = "Feel the Stress (Print)" Then
                MessageBox.Show("Feel the Stress (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 18.5
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            End If
        End If
        Me.Close()
    End Sub

User is offlineProfile CardPM
+Quote Post

the_hangman
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 01:27 PM
Post #2

D.I.C Addict
Group Icon

Joined: 18 Jan, 2006
Posts: 593



Thanked: 1 times
Dream Kudos: 200
My Contributions
I assume "MessageBox" is what you named your ListBox.
If that is the case MessageBox.Show() is a funtion to make the ListBox visible

try MessageBox.Items.Add("Learn Calculus in One Day (Print) has been" & vbCrLf & "added to your shopping cart.")
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 02:06 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
@the_hangman
A MessageBox is just that, a window that pops up with a message to the user.

But you are on the right track.

@ ramper04:

What is the name of the listbox that you are adding your string to?

Follow the example posted by the_hangman but replace MessageBox with the name of the listbox that you want the text to show up inside.

So an example would be like this:
CODE

If lstPrintBooks.SelectedItem = "I Did It Your Way (Print)" Then
                yourListBox.Items.Add("I Did It Your Way (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 11.95
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping

User is offlineProfile CardPM
+Quote Post

ramper04
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 02:36 PM
Post #4

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 10


My Contributions
The lisbox that I am trying to add the selected item to is in a different form(frmShoppingCart) and the selected item is the form frmPrintBooks
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 06:53 PM
Post #5

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
like this?

CODE

frmWithoutFocus.AListBox.Add (frmWithFocus.AnotherlistBox.Text)


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 08:17 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
KeyWiz is correct just put the name of your other form in front of the listbox that is contained on that form.
CODE

yourForm.yourListBox.Items.Add("I Did It Your Way (Print) has been" & vbCrLf & "added to your shopping cart.")

User is offlineProfile CardPM
+Quote Post

ramper04
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 08:33 PM
Post #7

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 10


My Contributions
I tried that but is says that the form i am trying to add to is not declared.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 08:44 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
How are you instantiating your other form?

Did you declare an instance of a form using a variable declaration?

Like this:
CODE

Dim myForm As New Form2


Or did you just create two forms in the designer and your trying to communicate between them?
User is offlineProfile CardPM
+Quote Post

ramper04
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
13 Dec, 2006 - 08:49 PM
Post #9

New D.I.C Head
*

Joined: 13 Dec, 2006
Posts: 10


My Contributions
Here is the code for both the shopping cart form and the printbooks form.
maybe this will help.

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

    End Sub

    Private Sub lstProducts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub mnuFileReset_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileReset.Click
        'Resets the form.
        lstProducts.ClearSelected()
        lblSubtotal.Text = ""
        lblTax.Text = ""
        lblShipping.Text = ""
        lblTotalCost.Text = ""
        lstProducts.Focus()
    End Sub

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

    Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click
        MessageBox.Show("Shopping Cart Version 1.0")
    End Sub

    Private Sub mnuPrintBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrintBooks.Click
        'Displays the print books window
        Dim PrintBooks As New frmPrintBooks
        PrintBooks.ShowDialog()
        'Update the total cost
        lblSubtotal.Text = FormatCurrency(g_subtotal)
        lblTax.Text = FormatCurrency(g_tax)
        lblShipping.Text = FormatCurrency(g_shipping)
        lblTotalCost.Text = FormatCurrency(g_totalCost)
    End Sub

    Private Sub mnuAudioBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAudioBooks.Click
        'Displays the audio books widow
        Dim AudioBooks As New frmAudioBooks
        AudioBooks.ShowDialog()
        'Update the total cost
        lblSubtotal.Text = FormatCurrency(g_subtotal)
        lblTax.Text = FormatCurrency(g_tax)
        lblShipping.Text = FormatCurrency(g_shipping)
        lblTotalCost.Text = FormatCurrency(g_totalCost)
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        'Removes the selected item from the shopping cart list.
        If lstProducts.SelectedIndex > -1 Then
            If lstProducts.SelectedItem = "I Did It Your Way (Print)" Then
                g_subtotal -= 11.95
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("I Did It Your Way (Print)")
            ElseIf lstProducts.SelectedItem = "The History of Scotland (Print)" Then
                g_subtotal -= 14.5
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("The History of Scotland (Print)")
            ElseIf lstProducts.SelectedItem = "Learn Calculus in One Day (Print)" Then
                g_subtotal -= 29.95
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("Learn Calculus in One Day (Print)")
            ElseIf lstProducts.SelectedItem = "Feel the Stress (Print)" Then
                g_subtotal -= 18.5
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("Feel the Stress (Print)")
            ElseIf lstProducts.SelectedItem = "Learn Calculus in One Day (Audio)" Then
                g_subtotal -= 11.95
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("Learn Calculus in One Day (Audio)")
            ElseIf lstProducts.SelectedItem = "The History of Scotland (Audio)" Then
                g_subtotal -= 14.5
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("The History of Scotland (Audio)")
            ElseIf lstProducts.SelectedItem = "The Science of Body Language (Audio)" Then
                g_subtotal -= 29.95
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("The Science of Body Language (Audio)")
            ElseIf lstProducts.SelectedItem = "Relaxation Techniques (Audio)" Then
                g_subtotal -= 18.5
                g_tax = g_subtotal * 0.06
                g_shipping -= 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
                lstProducts.Items.Remove("Relaxation Techniques (Audio)")
            End If
        End If
    End Sub


    Private Sub txtSubtotal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub lstProducts_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstProducts.SelectedIndexChanged
        lstProducts.Items.Add(g_lstProducts)
    End Sub


CODE
Private Sub btnPrintBooksCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintBooksCancel.Click
        Me.Close()
    End Sub

    Private Sub btnPrintBooksAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintBooksAdd.Click
        If lstPrintBooks.SelectedIndex > -1 Then
            If lstPrintBooks.SelectedItem = "I Did It Your Way (Print)" Then
                MessageBox.Show("I Did It Your Way (Print) has been" & vbCrLf & "added to your shopping cart.")
                frmShoppingCart.lstProducts.Items.Add("I Did It Your Way (Print)")
                g_subtotal += 11.95
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            ElseIf lstPrintBooks.SelectedItem = "The History of Scotland (Print)" Then
                MessageBox.Show("The History of Scotland (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 14.5
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            ElseIf lstPrintBooks.SelectedItem = "Learn Calculus in One Day (Print)" Then
                MessageBox.Show("Learn Calculus in One Day (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 29.95
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            ElseIf lstPrintBooks.SelectedItem = "Feel the Stress (Print)" Then
                MessageBox.Show("Feel the Stress (Print) has been" & vbCrLf & "added to your shopping cart.")
                g_subtotal += 18.5
                g_tax = g_subtotal * 0.06
                g_shipping += 2.0
                g_totalCost = g_subtotal + g_tax + g_shipping
            End If
        End If
        Me.Close()
    End Sub

User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
14 Dec, 2006 - 05:59 PM
Post #10

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
Can you indicate the line which is causing trouble?
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
14 Dec, 2006 - 07:09 PM
Post #11

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
it looks from your code like jayman is correct. you need to instantiate the form before you try adding anything to a listbox on its form
User is offlineProfile CardPM
+Quote Post

the_hangman
RE: Adding A Selected Item In One Listbox To Another Listbox In A Differen
15 Dec, 2006 - 05:49 AM
Post #12

D.I.C Addict
Group Icon

Joined: 18 Jan, 2006
Posts: 593



Thanked: 1 times
Dream Kudos: 200
My Contributions
I thought MessageBox was the name of the list box. because he was adding a message to it.....

I've never used MessageBox.Show() before... I use MsgBox()
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:07PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month