9 Replies - 1875 Views - Last Post: 23 April 2007 - 07:32 PM Rate Topic: -----

#1 1uniquegeek06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 07

Mutiple form & List boxes

Posted 25 March 2007 - 02:27 PM

Hi, I can't figure out how to add item in a list box to another list box on the main form. I'm doing a shopping cart application.

Here's my code on the main form:
  Private Sub mnuPrintBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrintBooks.Click
		Dim printbooksForm As New frmPrintBooks

		' Show the other form in model style.
		printbooksForm.ShowDialog()

		DisplayCharges()
	End Sub

	Private Sub mnuAudioBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAudioBooks.Click
		Dim audiobooksForm As New frmAudioBooks

		' Show the other form in model style.
		audiobooksForm.ShowDialog()

		DisplayCharges()
	End Sub


frmPrintBooks:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

		Select Case lstPrintBooks.SelectedItem
			Case "I Did It Your Way"
				Selection = lstPrintBooks.SelectedItem
			Case "The History of Scotland"
				Selection = lstPrintBooks.SelectedItem
			Case "Learning Calculus in One Day"
				Selection = lstPrintBooks.SelectedItem
			Case "Feel the Stress"
				Selection = lstPrintBooks.SelectedItem
		End Select

		' Let the user know the book has been added.
		MessageBox.Show("Print book added for " & lstPrintBooks.SelectedItem, "Book Added")
	End Sub


My module:
Module ShoppingCartSystemModule
	' This Module contains the CalcTax Function.

	Const taxRate As Single = 0.06 ' Sales Tax Rate

	Public Function CalcTax(ByVal cost As Decimal) As Decimal
		' This function calculates and returns the
		' sales tax on ticket sales. The book cost is
		' passed as an argument
		Return cost * taxRate
	End Function

	Public Selection As String = ""
End Module


There's a total of 3 forms but I'm showing only the main form and the other one because if I can figure out how to add the books from the lstPrintBooks (second form) to the lstProducts (Main) then the 3rd form should be similar. I hope I not confusing anyone. I'm a beginner and I've tried many ways even a selection method.

I hope someone can help me. I would really appreciate it a lot.

Thanks in advance,
Nicky

This post has been edited by 1uniquegeek06: 25 March 2007 - 06:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Mutiple form & List boxes

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Mutiple form & List boxes

Posted 25 March 2007 - 03:06 PM

The key to working with multiple forms is that if you want to share data across them, then you need to be able to receive the events of the other forms on your main form.

In order to do that you need to instantiate your forms WithEvents and you have to do it at the class level.

Public Class yourFormName
		Inherits.System.Windows.Forms.Form

Dim WithEvents audiobooksForm As New frmAudioBooks
Dim WithEvents printbooksForm As New frmPrintBooks

Private Sub mnuPrintBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrintBooks.Click


' Show the other form in model style.
printbooksForm.ShowDialog()

DisplayCharges()
End Sub

Private Sub mnuAudioBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAudioBooks.Click


' Show the other form in model style.
audiobooksForm.ShowDialog()

DisplayCharges()
End Sub


You will now find that when you use the the objects name in your code, when the context menu pops open you have access to the objects that are on the other form.

Example: This will retrieve the value that is selected in the Listbox that is on the printbookForm called ListBox1.
printbooksForm.ListBox1.SelectedItem

Was This Post Helpful? 0
  • +
  • -

#3 1uniquegeek06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 07

Re: Mutiple form & List boxes

Posted 25 March 2007 - 04:47 PM

Thanks for the your help.

Umm I'm still a bit stuck.

Quote

Example: This will retrieve the value that is selected in the Listbox that is on the printbookForm called ListBox1.
printbooksForm.ListBox1.SelectedItem


The code above do I add that in my Printbooksform?

This post has been edited by 1uniquegeek06: 25 March 2007 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Mutiple form & List boxes

Posted 25 March 2007 - 08:37 PM

No, that was just an example showing you were to place the declarations for your forms.

It was your code to begin with, I just moved two lines up below the Class level.

Declare these at the Class level.
Dim WithEvents audiobooksForm As New frmAudioBooks
Dim WithEvents printbooksForm As New frmPrintBooks

Was This Post Helpful? 0
  • +
  • -

#5 1uniquegeek06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 07

Re: Mutiple form & List boxes

Posted 28 March 2007 - 09:31 AM

I still haven't figure out how to display a selected item from one listbox in one form to the Main form. I use the withEvent that jayman advised. I'm still lost in do I add a Item.Add() in the listbox Printbooks form or is that add in the collections?

Do I add example
lstPrintBooks.ItemsAdd("I Did It Your Way")


It doesn't add the item to the Main form's listbox lstProducts
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Mutiple form & List boxes

Posted 28 March 2007 - 09:59 AM

You can add the selected item on your lstPrintBooks to the lstProducts in the following manner, this code would go in your main form after you ShowDialog for the printbooksForm.
lstProducts.Items.Add(printbooksForm.ListBox1.SelectedItem)


Was This Post Helpful? 0
  • +
  • -

#7 1uniquegeek06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 07

Re: Mutiple form & List boxes

Posted 31 March 2007 - 11:48 PM

View Postjayman9, on 28 Mar, 2007 - 09:59 AM, said:

You can add the selected item on your lstPrintBooks to the lstProducts in the following manner, this code would go in your main form after you ShowDialog for the printbooksForm.
lstProducts.Items.Add(printbooksForm.ListBox1.SelectedItem)



Thanks jayman9 ... I saw that you reply 3 days ago but I just had a chance to work on this program.

Its works!!! :D

I have a another problem ... I use the select case method on the other form to selected the items

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
		Dim lstProducts As String
		' This procedure adds the selected Audio Books
		' to the shopping cart
		' class-level variables.

		Select Case lstPrintBooks.SelectedItem
			Case "I Did It Your Way"
				Selection = lstPrintBooks.SelectedItem
			Case "The History of Scotland"
				Selection = lstPrintBooks.SelectedItem
			Case "Learning Calculus in One Day"
				Selection = lstPrintBooks.SelectedItem
			Case "Feel the Stress"
				Selection = lstPrintBooks.SelectedItem
		End Select

		' Let the user know the book has been added.
		MessageBox.Show("Audio book added for " & lstPrintBooks.SelectedItem, "Book Added")
		Me.Close()
	End Sub


How do I add the value cost with this code to display on the main form? Example "I Did It Your Way" when selected it should display $11.95 in the Subtotal

This post has been edited by 1uniquegeek06: 01 April 2007 - 12:43 AM

Was This Post Helpful? 0
  • +
  • -

#8 musiqdefunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 07

Re: Mutiple form & List boxes

Posted 12 April 2007 - 08:11 PM

where you able to get it work cause i am on the same program in a different college and i am not able to get it to work even though i followed some of the guidance in this forum.
Was This Post Helpful? 0
  • +
  • -

#9 1uniquegeek06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-February 07

Re: Mutiple form & List boxes

Posted 15 April 2007 - 09:32 PM

View Postmusiqdefunk, on 12 Apr, 2007 - 08:11 PM, said:

where you able to get it work cause i am on the same program in a different college and i am not able to get it to work even though i followed some of the guidance in this forum.


I got the items add to the main form list box but I cannot get it to calculate still.
Was This Post Helpful? 0
  • +
  • -

#10 musiqdefunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 07

Re: Mutiple form & List boxes

Posted 23 April 2007 - 07:32 PM

i am going to work on it tommorow and i will let you what i come up with
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1