Solved

Save combobox entries to textfile

Posted on 2014-10-22
1
99 Views
Last Modified: 2014-10-23
I have a combobox that i want to save to a textfile when clicking a button, it should append all entries to a new line UNLESS it already exists. I have the below code, but it doesnt save, it doesnt do anything, but no errors.

   Dim content(TextBox1.Items.Count - 1) As String
        TextBox1.Items.CopyTo(content, 0)
        IO.File.WriteAllLines("C:\SWDEPOT\test.txt", content)

Open in new window


If a prepopulate the textfile with entries i am able to load it completely fine with the below code:

Dim load() As String = IO.File.ReadAllLines("C:\SWDEPOT\test.txt")
        Array.Reverse(load)
        TextBox1.Items.AddRange(load)

Open in new window

0
Comment
Question by:derek7467
1 Comment
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 40399767
Something like this should work:

    Const filePath As String = "C:\SWDEPOT\test.txt"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Adds new text
        If TextBox1.SelectedIndex = -1 AndAlso TextBox1.Text <> "" Then
            IO.File.AppendAllText(filePath, TextBox1.Text & vbNewLine)
        End If

        ' Clear previous information
        TextBox1.Items.Clear()

        ' Load the information from the text file
        Dim load() As String = IO.File.ReadAllLines(filePath)
        Array.Reverse(load)
        TextBox1.Items.AddRange(load)

    End Sub

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Date Picker 64 bit 32 bit issue 12 53
vb6 connector to mongodb 2 83
How to measure sizes and angles in scanned images ? 3 63
How to make an ADE file by code? 11 83
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question