Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET File I/O and Registry (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/)
-   -   Saving a Listbox orr adding a list box to a text file (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/327438-saving-listbox-orr-adding-list-box-text-file.html)

Inlee 12-14-2014 12:39 PM

Saving a Listbox orr adding a list box to a text file
 
Ok, I have been struggling with this for about a month now, have searched for the help could not find it. Maybe I am just not understanding it. I will post here what I have and what I am trying to accomplish and see if anyone has any suggestions on a direction to try.

I have a Listbox that takes all of the information from several textboxs and combines it into said listbox. From there I need to save that listbox into a text file and/or have it all transfer into a textbox so I can save it. I have my textbox set to save, works like it is supposed to so no problem there, just a matter of, for lack of better words, making the listbox copy itself to the textbox so I can save it.

listbox1 = lbxLoad
textbox1.text = txtLoad.Text

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo.Click
lbxLoad.Items.Add("[Load Information]")
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add(" ")
lbxLoad.Items.Add("Load Number: " & vbTab & txtLoadNumber.Text)
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add(" ")
lbxLoad.Items.Add("[Pickup Information] ")
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add("Pickup Date: " & vbTab & DateTimePicker1.Text)
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add("Shipper: " & vbTab & vbTab & txtShipper.Text)
lbxLoad.Items.Add("Address: " & vbTab & vbTab & txtShipperAddress.Text)
lbxLoad.Items.Add("City: " & vbTab & vbTab & txtShipperCity.Text)
lbxLoad.Items.Add("State: " & vbTab & vbTab & cmbShipperState.Text)
lbxLoad.Items.Add("Phone: " & vbTab & vbTab & mtbShipperPhone.Text)
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add(" ")
lbxLoad.Items.Add("[Delivery Information] ")
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add("Delivery Date: " & vbTab & DateTimePicker2.Text)
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add("Receiver: " & vbTab & txtReceiver.Text)
lbxLoad.Items.Add("Address: " & vbTab & vbTab & txtReceiverAddress.Text)
lbxLoad.Items.Add("City: " & vbTab & vbTab & txtReceiverCity.Text)
lbxLoad.Items.Add("State: " & vbTab & vbTab & cmbReceiverState.Text)
lbxLoad.Items.Add("Phone: " & vbTab & vbTab & mtbReceiverPhone.Text)
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add(" ")
lbxLoad.Items.Add("[Mileage Information] ")
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add("Starting Mileage: " & vbTab & txtStartMileage.Text)
lbxLoad.Items.Add("Ending Mileage: " & vbTab & txtEndMileage.Text)
lbxLoad.Items.Add("Empty Miles: " & vbTab & txtEmptyMiles.Text)
lbxLoad.Items.Add("Loaded Miles: " & vbTab & txtLoadedMiles.Text)
lbxLoad.Items.Add("Total Miles: " & vbTab & txtTotalMiles.Text)
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add(" ")
lbxLoad.Items.Add("[Pay Information] ")
lbxLoad.Items.Add("*************************************************** ********************")
lbxLoad.Items.Add("Base Rate: " & vbTab & vbTab & "$" & txtBasePay.Text)
lbxLoad.Items.Add("Miles at Base Rate:" & vbTab & vbTab & "$" & txtTotalBase.Text)
lbxLoad.Items.Add("Per Diem Rate: " & vbTab & vbTab & "$" & txtPerDiemPay.Text)
lbxLoad.Items.Add("Miles at Per Diem rate:" & vbTab & "$" & txtTotalPerDiem.Text)
lbxLoad.Items.Add("Detention: " & vbTab & vbTab & "$" & txtTotalDetention.Text)
lbxLoad.Items.Add("Stops: " & vbTab & vbTab & vbTab & "$" & txtTotalStops.Text)
lbxLoad.Items.Add("Advances: " & vbTab & vbTab & "$" & txtTotalAdvances.Text)
lbxLoad.Items.Add("Total pay for trip: " & vbTab & vbTab & "$" & txtTotalPay.Text)

If lbxLoad.SelectedItems.Count = 1 Then
txtLoad.Text = lbxLoad.SelectedItem
End If
End Sub

All of the information in lbxLoad needs to be transferred to txtLoad.text preferably with the simple press of a button. Any suggestions?

I am willing to send more of the code and or the program itself if more information is needed, just ask.

Gruff 12-18-2014 04:25 PM

Not sure why your wanting to use a textbox to write to a file. You do not need it to do so.
Code:
Imports System.IO Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'Add four sample items to a listbox. With ListBox1 .Items.AddRange(New String() {"Cat", "Rat", "Dog", "Zebra"}) End With End Sub Private Sub cmdWriteListbox_Click(sender As System.Object, e As System.EventArgs) Handles cmdWriteListbox.Click 'Write listbox contents to a file With ListBox1 Dim LinesOfText(.Items.Count - 1) As String .Items.CopyTo(LinesOfText, 0) File.WriteAllLines("C:\MyTextFile.txt", LinesOfText) End With End Sub Private Sub cmdClearListbox_Click(sender As System.Object, e As System.EventArgs) Handles cmdClearListbox.Click 'Clear listbox of items ListBox1.Items.Clear() End Sub Private Sub cmdReadListbox_Click(sender As System.Object, e As System.EventArgs) Handles cmdReadListbox.Click 'Read text from file and fill a listbox With ListBox1 Dim LinesOfText = File.ReadAllLines("C:\MyTextFile.txt") .Items.AddRange(LinesOfText) End With End Sub End Class

Gruff 12-18-2014 05:09 PM

Same thing but without the extra variables.
Code:
Imports System.IO Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'Add four sample items to a listbox. ListBox1.Items.AddRange(New String() {"Cat", "Rat", "Dog", "Zebra"}) End Sub Private Sub cmdWriteListbox_Click(sender As System.Object, e As System.EventArgs) Handles cmdWriteListbox.Click 'Write listbox contents to a file File.WriteAllLines("C:\MyTextFile.txt", ListBox1.Items.Cast(Of String).ToArray) End Sub Private Sub cmdClearListbox_Click(sender As System.Object, e As System.EventArgs) Handles cmdClearListbox.Click 'Clear listbox of items ListBox1.Items.Clear() End Sub Private Sub cmdReadListbox_Click(sender As System.Object, e As System.EventArgs) Handles cmdReadListbox.Click 'Read text from file and fill a listbox ListBox1.Items.AddRange(File.ReadAllLines("C:\MyTextFile.txt")) End Sub End Class


All times are GMT -6. The time now is 11:47 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.