[Help]Saving settings[Solved]

Posts 16–30 of 37 · Page 2 of 3
Quote Originally Posted by Jason View Post
EDIT3: Okay it appears the only way to save the contents of a listbox is to save to a .txt file and get them from there upon form reload. I'm currently googling it at the moment but if anyone wants to provide a quick snippet to save time feel free

Still working on trying to call process to top :@ it's annoying haha, so close to being finished.
Code:
 Public Sub ListBox_Read(ByVal ListBox As ListBox, ByVal sFile As String)

        ' Save content
        Dim oStream As IO.StreamReader
        Dim sLine As String

        ' Clear listbox
        ListBox.Items.Clear()

        ' Check if file exists
        Dim oFile As New IO.FileInfo(sFile)

        'if the list.dat exists then read it...
        If oFile.Exists() = True Then
            oStream = New IO.StreamReader(sFile)
            ' read file
            Do
                'read each line
                sLine = oStream.ReadLine()
                If IsNothing(sLine) Then Exit Do
                'add items to the listbox
                ListBox.Items.Add(sLine)
                'loop until it's done
            Loop
            'close the stream
            oStream.Close()
        End If
    End Sub

    Private Sub savelb()
        'save a list.dat with listbox content
        sFile = Application.StartupPath & "\List.dat"
        ListBox_Save(ListBox1, sFile)
    End Sub

    Private Sub loadlb()
        'load a list.dat to fill in listbox content
        sFile = Application.StartupPath & "\List.dat"
        ListBox_Read(ListBox1, sFile)
    End Sub
now...to save / load your listbox:

Code:
loadlb()
Code:
savelb()
ENJOY!!!
Woot thanks. At the moment i'm just using a simple .txt storage. The only thing i need to tweak now is when i delete an item from the listbox, for it to highlight the next line straight away. Any ideas?

Like...

Code:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Accounts.Items.Remove(Accounts.SelectedItem)
'need to somehow make the next listbox item highlighted as well..

    End Sub
Thanks for all your help. I don't suppose you know how to call a process to the top?
Quote Originally Posted by Jason View Post
Woot thanks. At the moment i'm just using a simple .txt storage. The only thing i need to tweak now is when i delete an item from the listbox, for it to highlight the next line straight away. Any ideas?

Like...

Code:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Accounts.Items.Remove(Accounts.SelectedItem)
'need to somehow make the next listbox item highlighted as well..

    End Sub
Thanks for all your help. I don't suppose you know how to call a process to the top?
Code:
Accounts.SelectedIndex+=1
You can also check if the selected item is the last one, if so then select the first item on the list.

Code:
If Accounts.SelectedIndex=Accounts.Items.count-1 Then
Accounts.SelectedIndex=0
End If
Okay thanks, unfortunately it seems that wasn't my problem after all. There's something bad in my Account_SelectIndexChanged sub

Also I've hit another problem. When I hit "Save Details" it adds the items to the list and it's all fine and dandy and saves each new addition fine. But once i rerun the program it has added a new listbox entry which is blank...

This is my code involving the writer/reader..

Code:
        Dim reader As String
        reader = My.Computer.FileSystem.ReadAllText("C:\Users\Jason_2\Documents\Testa.txt")

        Dim strs() As String
        strs = Split(reader, Environment.NewLine)


        For Each s As String In strs
            Accounts.Items.Add(s)
        Next
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim writer As New IO.StreamWriter("C:\Users\Jason_2\Documents\Testa.txt")
        For i As Integer = 0 To Accounts.Items.Count - 1
            writer.WriteLine(Accounts.Items.Item(i))
        Next

        writer.Close()



    End Sub
So yeah, when i restart the program a new listbox entry has been made which is completely blank.


And now to the problem with deleting entries.

I've done something wrong here

Code:
    Private Sub Accounts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Accounts.SelectedIndexChanged

      
        UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)

        PassTxt.Text = GetSetting("AutoLogin.exe", "Pass", Accounts.SelectedItem)
Originally this worked fine as when i selected a new account name from the list, it's show the Username and ****'d out password in their respective textboxes. But when I delete a listbox entry it's also changing the index and it shuts down and gives me an error saying the path is empty. This only happens occasionally and I think I need to first check if the Path is empty, and if so change the selectedindex but i'm not 100% sure how i'd go about it :S...
Can you please explain the highlighting thingy a bit more?

Like

Items:

a
b
c
d
e
f
g


Now if I delete item "e" it should highlight item "f"? o_O?
Post your WHOLE code and specify every problem.
Okay this is my whole code.

Code:
 Option Explicit On
Imports System.IO
Imports System.Runtime.InteropServices

Public Class Form1

    Inherits System.Windows.Forms.Form
    Function CheckProcess(ByVal strProcess)
        Dim Process, strObject
        CheckProcess = False
        strObject = "winmgmts://"
        For Each Process In GetObject(strObject).InstancesOf("win32_process")
            If UCase(Process.name) = UCase(strProcess) Then
                CheckProcess = True
                Exit Function
            End If
        Next
    End Function

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Declare Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr
    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TextBox5.Text = GetSetting("WorkingSettings.exe", "Settings", "Text")
        Dim reader As String
        reader = My.Computer.FileSystem.ReadAllText("C:\Users\*****\Documents\Testss.txt")

        Dim strs() As String
        strs = Split(reader, Environment.NewLine)


        For Each s As String In strs
            Accounts.Items.Add(s)
        Next

    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim OpenFileDialog1 As New OpenFileDialog

        OpenFileDialog1.InitialDirectory = "Desktop"
        OpenFileDialog1.Filter = "exe files (*.exe)|*.exe|All files (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.RestoreDirectory = True

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            TextBox5.Text = OpenFileDialog1.FileName
            SaveSetting("WorkingSettings.exe", "Settings", "Text", TextBox5.Text)

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CheckProcess("Wow.exe")
        If TextBox5.Text = "" Then
            MsgBox("Please select your .exe", vbCritical, "Error")


        ElseIf CheckProcess("Wow.exe") = True Then
            SendKeys.Send(UserTxt.Text)
            SendKeys.Send("{TAB}")
            SendKeys.Send(PassTxt.Text)
            SendKeys.Send("{ENTER}")
        ElseIf CheckProcess("Wow.exe") = False Then
            System.Diagnostics.Process.Start(TextBox5.Text)
            Sleep(5000)
            SendKeys.Send(UserTxt.Text)
            SendKeys.Send("{TAB}")
            SendKeys.Send(PassTxt.Text)
            SendKeys.Send("{ENTER}")

        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Accounts.Items.Add(NewUserTxt.Text)

        SaveSetting("AutoLogin.exe", "Users", NewUserTxt.Text, NewUserTxt.Text)

        SaveSetting("AutoLogin.exe", "Pass", NewUserTxt.Text, NewPassTxt.Text)

        NewUserTxt.Clear()
        NewPassTxt.Clear()

        Dim writer As New IO.StreamWriter("C:\Users\*****\Documents\Testss.txt")
        For i As Integer = 0 To Accounts.Items.Count - 1
            writer.WriteLine(Accounts.Items.Item(i))
        Next

        writer.Close()



    End Sub

    Private Sub Accounts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Accounts.SelectedIndexChanged

        UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)

        PassTxt.Text = GetSetting("AutoLogin.exe", "Pass", Accounts.SelectedItem)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Accounts.Items.Remove(Accounts.SelectedItem)

    End Sub

End Class
And the only problems I'm currently experiencing are:

1. After closing and re-opening the program, the saved listbox entries all work fine but there is always a new blank listbox entry created each startup.

Here's an SS of problem #1.
The error is occuring with this line of code:

Code:
    Private Sub Accounts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Accounts.SelectedIndexChanged

        UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)

        PassTxt.Text = GetSetting("AutoLogin.exe", "Pass", Accounts.SelectedItem)
    End Sub
Problem number 2. When I press button4 (To delete an account from the list) The same error occurs on the same line of code.

If you need more info just tell me i'll be up for a while.
Okay I'll upload it, do I just upload the .vbproj file or should i put the whole "autologin" file? Also be wary of where the .txt file is saving. I haven't made a filepath chooser for settings yet and I don't know how to set default location to the same location the program is saved in, woo for starting VB 4 days ago? haha.

The Try and Catch method works as a quick patch, keeps the program running at least.

And i've discovered another problem, the delete account from list works while the program is running, but it doesn't delete it from the .txt file so on program restart they're all back again...FML haha.


Quote Originally Posted by ilikewaterha
HAHA! I"MA LEECH IT NOW!
Lmao jk.
Nice GUi, and sorry i don't have a solution >.<
Thanks but good luck getting people to download an "Auto-logger" where they have to put in their username and password haha. I'm just making this for an IRL friend to make his computing life that little bit simpler, and it's teaching me a hell of a lot about VB in the process.
HAHA! I"MA LEECH IT NOW!
Lmao jk.
Nice GUi, and sorry i don't have a solution >.<
try
UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)
catch
msgbox(errortostring)
end try

> Catches a specific error message.

Can you please upload your project and send me the link via pm?
just upload the WHOLE project folder
Okay linking you it via PM now... it's a mega-upload link :S
its fine checking it now.
Hey NextGen, I know the reason the error is popping up. But the problem is that the program is auto generating this null path every time it loads, it creates a new blank entry into the listbox which causes errors. My ListBox sub isn't specific enough to check whether it's returning a null value before trying to GetSettings(. That's what needs to be tweaked as well as some other things.
try
UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)
catch
msgbox(errortostring)
end try
It's probably bypassing without error, we should catch the "whole exception"

try
UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)
catch Ex as exception
msgbox(errortostring)
MsgBox(ex)
end try
So instead of bypassing the error entirely we will get the actual exception, so instead of path is nothing , you should get a error saying path C:\whatever is invalid. then we can figure out the issue

This way it doesn't bypass, it actually tells us the error, in fact in all of my asp.net applications I have a list box on a settings page that catches every exception and stores them in a listbox, then to a database this way I can debug problems in applications.

The error path is nothing simply means the path is returning Null,
Null = 0 , 0 = invalid path specified.

A path must have a specific data type and a valid name.
So I will have to look at your code and see the line it is error(ing)on, but it isn't complex coding or a complex issue, you simply specified an invalid path

Add a valid path name and you are good to go.

I will look through the code and fix it

Feel free to send me the file as well if Blubb has issues, Ill read posts back and see what is going on
Posts 16–30 of 37 · Page 2 of 3
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us