[Help]Saving settings[Solved]

Posts 31–37 of 37 · Page 3 of 3
I fixxed it and send it to him already

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

Public Class Form1


    Inherits System.Windows.Forms.Form

    Dim sfile As String

    Public Sub ListBox_Save(ByVal ListBox As ListBox, ByVal sFile As String)

        ' Save content
        Dim oStream As IO.StreamWriter
        Dim i As Short

        'create a new streamwriter
        oStream = New IO.StreamWriter(sFile)

        'count each listbox item

        For i = 0 To ListBox.Items.Count - 1
            oStream.WriteLine(ListBox.Items(i))
        Next
        'close streamwriter
        oStream.Close()
    End Sub

    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(Accounts, sfile)
    End Sub

    Private Sub loadlb()
        'load a list.dat to fill in listbox content
        sFile = Application.StartupPath & "\List.dat"
        ListBox_Read(Accounts, sfile)
    End Sub

    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")
      
        Try
            loadlb()
        Catch
            MsgBox(ErrorToString)
        End Try



    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

        savelb()
    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
        If Not Accounts.Items.Contains(NewUserTxt.Text) Then
            Accounts.Items.Add(NewUserTxt.Text)
        Else
            MsgBox("Account is already in list.")
        End If

        If Not NewUserTxt.Text = "" Then
            SaveSetting("AutoLogin.exe", "Users", NewUserTxt.Text, NewUserTxt.Text)
        End If

        If Not NewPassTxt.Text = "" Then
            SaveSetting("AutoLogin.exe", "Pass", NewPassTxt.Text, NewPassTxt.Text)
        End If




        NewUserTxt.Clear()
        NewPassTxt.Clear()


        savelb()

    End Sub

    Private Sub Accounts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Accounts.SelectedIndexChanged
        Try
            UserTxt.Text = GetSetting("AutoLogin.exe", "Users", Accounts.SelectedItem)
        Catch
            MsgBox(ErrorToString)
        End Try
        Try
            PassTxt.Text = GetSetting("AutoLogin.exe", "Pass", Accounts.SelectedItem)
        Catch
            MsgBox(ErrorToString)

        End Try

    End Sub

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

        savelb()

    End Sub

End Class
Enjoy.
Okay Blubb's fix solved the problem of the rogue listbox entry. Now the only thing left to fix is the returning of null values when I delete a listbox entry, and to bring the searched-for process to the front if it is already running.

Code:
         ElseIf CheckProcess("Wow.exe") = True Then
 Somewhere here i need to bring "Wow.exe" to front. 
            SendKeys.Send(UserTxt.Text)
            SendKeys.Send("{TAB}")
            SendKeys.Send(PassTxt.Text)
            SendKeys.Send("{ENTER}")
Thank you so much for all your help guys, it's finally done! Everything is working like a charm now.

NextGen that code was just what i wanted although "Open(handle)" isn't needed. Thanks so much.

Cheers guys, everyone on this forum is so helpful

Until my next problem (soon hehe),

J-Deezy.
I wrote this up somewhere else in here, but here you go.

To get a External Application into focus.

First we need to create some Public Functions, Place Public Functions just below Public Class FormName (form1)

[php]
Private Declare Function Open Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
[/php]

Then whatever part of the code you want to fetch the keys, (on that event)


[php]

Dim handle As Long
handle = GetWindow(vbNullString, "Application Title Here")
Open(handle)
SetForegroundWindow(handle)
[/php]

Remember it is the Title Name, not the application name by EXE

What I suggest you do if you don't know the application name , is allow the user to select an application in list box and instead of hard coding the name allow then to click the exe and it will find the title name for you .

Also You could allow the user to luanch the application from a drop down menu

You can also get the name of a application by using the process code

[php]
Dim App1 as process = listbox1.selecteditem
Msgbox (app1.processname)
[/php]

Freehanded that, it's a concept, I don't know if you are displaying applications in a listbox or not.
Get all current processes and put each in a comBobox...

Code:
Dim Process As New Process()
    
        ComboBox1.Items.Clear()
        For Each Process In Process.GetProcesses(My.Computer.Name)
            On Error Resume Next
            ComboBox1.Items.Add(Process.ProcessName)
         
          
        Next
I did also send you a pm on how to maximize a process =D
Posts 31–37 of 37 · Page 3 of 3
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us