954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Save/Open Visual Basic

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?

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

You may check out the manual below on how to work with arrays in VB.NET
http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx

catherine sea
Junior Poster
126 posts since Jan 2008
Reputation Points: 25
Solved Threads: 20
 

Try this link for better understanding array.....

http://www.startvbdotnet.com/language/arrays.aspx

kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

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...

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

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 .

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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?

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 
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?

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

.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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

where does that code save the file to?

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

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 :)

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

>>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.:)

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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.

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

>>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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

#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.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: