I am trying to make a program that randomly selects a name. i have a prompt that allows me to enter the number of names and the names themselves. but i dont know how to save or open the information into an array... can anyone help me with this?

Recommended Answers

All 14 Replies

the links helped but not really... i would like to know how to open and save files along with loading the information into an array. the values and names arent previously loaded in the code...

See if this helps to load.File into Array.

Imports System.IO
Public Class Form1
    Private myNamesFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "myNamesFile.txt"
    Private arFileLines() As String = Nothing

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If File.Exists(myNamesFile) Then
            arFileLines = File.ReadAllLines(myNamesFile)
            MsgBox(arFileLines(1)) '// get line.2 from File.
        End If
    End Sub
End Class

.make sure you have at least 2 lines in your myNamesFile .

That helped but can i change the

Private Sub Form1_Load

To

Private Sub Button1_Click

how would i write a Private Sub to save the info in the array?

commented: just.because:) +12
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myCoolSub()
    End Sub
    Private Sub myCoolSub()
        If File.Exists(myNamesFile) Then
            arFileLines = File.ReadAllLines(myNamesFile)
            MsgBox(arFileLines(1)) '// get line.2 from File.
        End If
    End Sub

>>how would i write a Private Sub to save the info in the array?
.save to.file? or just add values to the Array? andAlso from where/what controls?

.actually, see If this helps w/saving.

Imports System.IO
Public Class Form1
    Private myNamesFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myNamesFile.txt"
    Private arFileLines() As String = Nothing

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myCoolSaveSub()
    End Sub
    Private Sub myCoolSaveSub()
        arFileLines = New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text}
        File.WriteAllLines(myNamesFile, arFileLines)
        MsgBox("file.saved.")
    End Sub
End Class

where does that code save the file to?

Itried adding the code you gave me and i cant actually save the file to my computer... What are the 3 text boxes for? Also do you have any idea on what kind of file i want it saved as or how i could open this saved file?

I want you to know this is helping out a lot. im just slow to learn things... im sorry if i seem naggy or rude about this... i dont meant to at all im just confused and clueless. thanks much and please keep it up :)

>>where does that code save the file to?
Private myNamesFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myNamesFile.txt"
>>What are the 3 text boxes for?
I used them as an example to write content back to the file.
.reason was: I had no idea other than >>i have a prompt that allows me to enter the number of names and the names themselves.
.thus I used TextBoxes.

If you specify on how and what controls contain those "names" for you to save/load the names to/from, then I will have a better understanding towards supplying a possible solution.
>>im sorry if i seem naggy or rude about this
.never even crossed my mind.:)

I have a form with a menu strip that has 3 sub menus. open, save, and close. the open and save sub menus has 8 sub menus to open 8 different files without searching for them. i have 3 buttons. 1 to start the prompt, 1 to display the array, and 1 to select the name. I have 1 rich text box that the data gets displayed in.

>>I have 1 rich text box that the data gets displayed in.
That's what I was looking for, thanx.:)

Imports System.IO
Public Class Form1
    Private myNamesFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myNamesFile.txt"
    Private arFileLines() As String = Nothing

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myCoolSaveSub(myNamesFile, RichTextBox1)
    End Sub

#Region "-----===-----===-----===  ===-----===-----===-----"
    Private Sub myCoolSaveSub(ByVal selCoolFile As String, selCoolRichTextBox As RichTextBox)
        File.WriteAllLines(selCoolFile, selCoolRichTextBox.Lines)
        MsgBox("file.saved.")
    End Sub
#End Region
End Class

What does this do
#Region "-----===-----===-----=== ===-----===-----===-----"

#Region is to be able to collapse an entire "region" of code and minimize the area of your code window. Your little trigger happy finger should be happy to Not have to scroll as much also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.