Solved

Save combobox entries to textfile

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

20 Experts available now in Live!

Get 1:1 Help Now