We help IT Professionals succeed at work.

Visual Basic Combo Boxes... using text file..

Hello experts please help...

Ill explain how it works first

Listbox1 has complete list of products

product 1
product 2
product 3


when i press add selected item will be shown in listbox2 (example i want product 2)

listbox2

product 2


after pressing button "Purchase" the program will do all calculations based on products that are in listbox2

The program then displays a message box telling me details of the customer, items from listbox2, their price, any discount, etc.

When i press Ok in messagebox the program will then use what is written in message box and display it in another textbox in another form that i can save or print as a receipt.

My problem is that i can't seem to pick up listbox2 items to display in message box or the textbox on another form.

Do you know how to properly tag listbox items and dispay them in message box?

by the way i am using visual studio 2008
Comment
Watch Question

Your description is not clear. For example, when you say "press add ", are you talking about clicking on a button labeled "add"? And what exactly do you mean by "how to properly tag listbox items "? Do you mean how do you detemine which item is selected?
Assuming I understand your questions, the item number currently selected in a list box is indicated by the property ListIndex. That is, the current item selected in LIst1 is List1.Listindex. The text of this item is List1.List(List1.Listindex). To display this in a messagebox, you would use
MsgBox List1.List(List1.Listindex)

Author

Commented:
forgot to include my code here it is.. thanks again..

cheers
Public Class frmCustomers
Dim Saveval As Integer = 1
Dim Listpath As String = "C:\Users\Silver\Desktop\silver\Customer-Purchase.txt"

Private Sub frmCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbCustomer.Enabled = False
btnProcess.Enabled = False
End Sub
'Creates a text logo on form using drawing
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Create a GraphicsPath.
Using graphics_path As New Drawing2D.GraphicsPath
' Two dimensial generated object.
graphics_path.AddString("BookStore", _
New FontFamily("Algerian"), _
CInt(FontStyle.Regular), _
60, New Point(20, 20), _
StringFormat.GenericTypographic)
' Font Generated, type, style and size.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.FillPath(Brushes.Silver, graphics_path)
Using thick_pen As New Pen(Color.Black, 2)
e.Graphics.DrawPath(thick_pen, graphics_path)
' Coloring
' When looking at the form in design mode the graphics generated will not be visible untill the program is started and the code executed.
End Using
End Using
End Sub
'Exit program command via menu strip
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
'Open file via menu strip
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

Dim strFileName As String
Dim TextLine As String = ""
Dim customers() As String
openFD.InitialDirectory = "C:\Users\Silver\Desktop\silver"
openFD.Title = "Open a File"
openFD.Filter = "Text Files(*.txt)|*.txt"
strFileName = openFD.FileName

If openFD.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Else
strFileName = openFD.FileName
End If

If System.IO.File.Exists(strFileName) = True Then
Dim objReader As New System.IO.StreamReader(strFileName)
TextLine = objReader.ReadToEnd
MessageBox.Show("File has been Loaded.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
customers = TextLine.Split(",")
For Each Name As String In customers
cmbCustomer.Items.Add(Name)
Next
End If

cmbCustomer.Enabled = True
btnProcess.Enabled = True
End Sub
'Save file via menu strip
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim strFileName As String
saveDB.InitialDirectory = "C:\"
saveDB.Title = "Save a text File"
saveDB.Filter = "Text Files(*.txt)|*.txt"

If saveDB.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Else
strFileName = openFD.FileName
MessageBox.Show(strFileName)
End If
End Sub

Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click

lstResult.Items.Clear()

If String.IsNullOrEmpty(cmbCustomer.Text) Then
MessageBox.Show("Please load and/or select customer's list first!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Return
End If

Dim paymentform As New frmPurchase
paymentform.ShowDialog()

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

If Dir("C:\Users\Silver\Desktop\silver\Customer-Purchase.txt") <> "" Then
My.Computer.FileSystem.DeleteFile("C:\Users\Silver\Desktop\silver\Customer-Purchase.txt")
End If

Saveval = Saveval + 1
For i = 0 To lstResult.Items.Count - 1
lstResult.SetSelected(i, True)
Dim listwrite As System.IO.StreamWriter
listwrite = My.Computer.FileSystem.OpenTextFileWriter(Listpath, True)
listwrite.WriteLine(lstResult.Items(i))
listwrite.Close()
Next
MessageBox.Show("Invoice saved as text file.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class

Public Class frmPurchase
'Fiction books and prices in arrays
Dim Fiction() As String = {"Hold Tight by Harlan Coben", "Breath by Tim Winton", "The Spare Room by Helen Gamer", _
"The Sleeping Doll by Heffrey Deaver", "The Navigator by Clive Cussler"}
Dim fprice() As String = {"10.00", "12.00", "12.50", "14.00", "11.95"}
'Reference books and prices in arrays
Dim Reference() As String = {"PHP for Dummies", "VB 2008 Unleashed", "Learn Javasript in 24 Hours", "Beginner's Java"}
Dim rprice() As String = {"50", "85", "45", "82"}
Dim index As Integer
Dim index2 As Integer
Dim Totals(3) As String
Dim Saveval As Integer = 1
Dim Listpath As String = "C:\Users\Silver\Desktop\BrankoBrkljac\Bookomatic2010\temp.txt"
Dim booklist As String

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Create a GraphicsPath.
Using graphics_path As New Drawing2D.GraphicsPath
' Two dimensial generated object.
graphics_path.AddString("BookStore", _
New FontFamily("Algerian"), _
CInt(FontStyle.Regular), _
60, New Point(20, 20), _
StringFormat.GenericTypographic)
' Font Generated, type, style and size.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.FillPath(Brushes.Silver, graphics_path)
Using thick_pen As New Pen(Color.Black, 2)
e.Graphics.DrawPath(thick_pen, graphics_path)
' Coloring
' When looking at the form in design mode the graphics generated will not be visible untill the program is started and the code executed.
End Using
End Using
End Sub

Private Sub frmPurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'load customer name into next form
lblCustNameLoad.Text = frmCustomers.cmbCustomer.Text
ListBox1.SelectedIndex = ListBox2.SelectedIndex
If Dir("C:\Users\Silver\Desktop\BrankoBrkljac\Bookomatic2010\temp.txt") <> "" Then
My.Computer.FileSystem.DeleteFile("C:\Users\Silver\Desktop\BrankoBrkljac\Bookomatic2010\temp.txt")
End If
End Sub

Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
frmCustomers.Show()
Me.Close()
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioReference.CheckedChanged

ListBox1.Items.Clear()
ListBox2.Items.Clear()

ListBox1.Items.AddRange(Reference)
ListBox2.Items.AddRange(rprice)
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioFiction.CheckedChanged
'add Fiction Books and prices to listbox1 and listbox2
ListBox1.Items.Clear()
ListBox2.Items.Clear()

ListBox1.Items.AddRange(Fiction)
ListBox2.Items.AddRange(fprice)
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'adding items from listbox 1 and 2 to listbox 3 and 4 by just selecting item in listbox 1
index = ListBox1.SelectedIndex
ListBox3.Items.Add(ListBox1.SelectedItem)
ListBox4.Items.Add(ListBox2.Items(index))

End Sub
'drag and drop function
Private Sub Bookdrop_MouseDown(ByVal Sender As Object, ByVal e As EventArgs) Handles ListBox1.MouseDown
Dim Bookdrop As ListBox
Bookdrop = CType(Sender, ListBox)
Bookdrop.DoDragDrop(Bookdrop, DragDropEffects.Copy)
End Sub
'drag and drop function
Private Sub ListBox3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox3.DragEnter
Dim Bookdrop As ListBox
index = ListBox1.SelectedIndex
Bookdrop = e.Data.GetData(GetType(ListBox))
ListBox3.Items.Add(Bookdrop.Text)
ListBox4.Items.Add(ListBox2.Items(index))
End Sub

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
'removing items from listbox 3 and 4 by just selecting items in listbox 
index2 = ListBox3.SelectedIndex
ListBox3.Items.Remove(ListBox3.SelectedItem)
ListBox4.Items.Remove(ListBox4.Items(index2))
End Sub

Private Sub btnPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
'if no books are added for purchase error message will be shown
If ListBox3.Items.Count = 0 Then
MessageBox.Show("You must select at least one book to purchase", "buy", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

'if no payment type has been selected an error message will be shown
If Len(cmbPayType.Text) = 0 Then
MessageBox.Show("You must select payment type to proceed.", "type", MessageBoxButtons.OK, MessageBoxIcon.Error)
cmbPayType.Focus()
Return

End If

Dim click As DialogResult = MessageBox.Show("Do you wish to finalize the purchase?", "Purchasing", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)

If click = DialogResult.No Then
Return
End If
'declaring sum and discount value
Dim sum As Decimal
Dim discount As Double
'if there are 3 or more values in listbox4 (price), since price and title are related,
'a discount of 10% will be applied to total price
If ListBox4.Items.Count >= 3 Then
discount = 0.1
Else
discount = 0
End If

'calculating total amount for books selected
For Each decAdded As Decimal In Me.ListBox4.Items
sum += Decimal.Parse(decAdded)
Next

'display total and any discount in labels
lblDiscount.Text = FormatCurrency(sum * discount)
lblTotal.Text = FormatCurrency(sum - (sum * discount))

Saveval = Saveval + 1
For i = 0 To ListBox3.Items.Count - 1
ListBox3.SetSelected(i, True)
Dim listwrite As System.IO.StreamWriter
listwrite = My.Computer.FileSystem.OpenTextFileWriter(Listpath, True)
listwrite.WriteLine(ListBox3.Items(i))
listwrite.Close()
Next

Dim FILE_NAME As String = "C:\Users\Silver\Desktop\BrankoBrkljac\Bookomatic2010\temp.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
booklist = booklist & objReader.ReadLine() & vbNewLine
Loop

objReader.Close()
Else
MessageBox.Show("File Does not Exist")
End If

Me.Tag = Totals

Totals(0) = frmCustomers.cmbCustomer.Text
Totals(1) = lblDiscount.Text
Totals(2) = lblTotal.Text
Totals(3) = cmbPayType.Text

MessageBox.Show("Customer Name is: " & Totals(0) & vbCrLf & "List of books" & vbCrLf & booklist & _
vbCrLf & "Total price is: " & Totals(2) & vbCrLf & "Payment type is: " & Totals(3) _
& vbCrLf & "Discount of " & vbCrLf & Totals(1))

'MessageBox.Show(message)
frmCustomers.lstResult.Items.Add("Customer name: " & Totals(0))
frmCustomers.lstResult.Items.Add("List of Books: " & vbCrLf & booklist)
frmCustomers.lstResult.Items.Add("Total Price: " & vbCrLf & Totals(2))
frmCustomers.lstResult.Items.Add("Payment type is: " & Totals(3))
frmCustomers.lstResult.Items.Add("Discount of " & vbCrLf & Totals(1))

Me.Hide()
frmCustomers.Show()
End Sub
End Class

Open in new window

Author

Commented:
Hello Leithauser, thanks for the reply.. Basicly its like this...

Dim index As Integer
index = ListBox1.SelectedIndex
Listbox3.items.Add(ListBox2.items(index)


Problem for me was i couldnt store the listbox items into the array that i can use in messagebox insted i saved it into a text file.

Author

Commented:
the code works fine ive tested it tonnes, all i need to do is get the Listbox's outputed into a pop up box which can then be printed off once all list boxes have been selected a "receipt" so to speak.

Author

Commented:
when i press ok on message box it should take me back to form 1 and display same data in a listbox in that form...  it shows all data  except the list of books is wrongly dispayed

example   Book1Book2Book3 where it should be  Book1 & vbNewLine & Book2 & vbNewLine & Book3

 it is suppose to serve as a receipt.. that i can save as text file and print
You are saying
"when i press ok on message box it should take me back to form 1 and display same data in a listbox in that form...  it shows all data  except the list of books is wrongly dispayed

example   Book1Book2Book3 where it should be  Book1 & vbNewLine & Book2 & vbNewLine & Book3"
However, from your description, it sounds like you are talking about a text box, not a list box. You would add items to a listbox with
List2.Additem List1(List(List1.Listindex)
You would add items to a Text box with
Text1 = Item1$ & chr$(13) & chr$(10) & Item2$
However, to have it displayed in separate lines, you would need to set the text box Multiline property to True during design time.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
It sounds like the core problem is:

    "My problem is that i can't seem to pick up listbox2 items to display in message box"

Do something like:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sb As New System.Text.StringBuilder
        For Each itm As String In ListBox2.Items
            sb.AppendLine(itm)
        Next
        Dim items As String = sb.ToString
        MessageBox.Show(items)
    End Sub

Author

Commented:
cheers worked, i had it set up wrong box.