Solved

Save combobox entries to textfile

Posted on 2014-10-22
1
97 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vba find the last empty column 10 89
Help in WHSCRIPT 9 47
Write data from SQL to CSV file - position 1 53
TT Auto Dashboard 13 85
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now