Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All
I have a issue with the remember me option.
When i use

Me.Hide()
Form2.Show()

next time when i open program it remembers the username and password

but if i use
Form2.Show()
Me.Close()

Next time when i open the program Session is = True but username and password are gone wtf??
Can someone check it out and say me how to fix it or where i make wrong .
Thank you

My Code:
VB
Imports MySql.Data.MySqlClient

Public Class Form1
    Dim MysqlConn As MySqlConnection
    Dim Command As MySqlCommand
    Private WithEvents tmr As New Timer With {.Interval = 15}
    Dim SAPI 'use for voice
    'Use for move screen
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    'end
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If My.Settings.Session = True Then
            txtuser.Text = My.Settings.Username
            txtpass.Text = My.Settings.Password
            CheckBox1.Checked = True
        Else
            'Sleep
            CheckBox1.Checked = False
        End If
    End Sub

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        ProgressBarEx1.Value += 1
        If ProgressBarEx1.Value = 100 Then
            tmr.Stop()
            ProgressBarEx1.ShowPercentage = False
            ProgressBarEx1.ShowText = True
            ProgressBarEx1.Text = "Finished"
            'Now make connection
            Dim conn As MySqlConnection
            Dim cmd As New MySqlCommand
            conn = New MySqlConnection
            conn.ConnectionString = "server=localhost;port=3306;userid=root;password=password21;database=ycdata"
            Dim Reader As MySqlDataReader
            Try
                conn.Open()
                Dim query As String
                query = "select ucode from users where uname= '" & txtuser.Text & "' and upassword = '" & txtpass.Text & "' "
                cmd = New MySqlCommand(query, conn)
                Reader = cmd.ExecuteReader

                Dim count As Integer
                count = 0
                While Reader.Read
                    count = count + 1
                End While

                If count = 1 Then

                    Dim usertype = Reader.GetString("ucode")
                    If usertype = "1" Then
                        SAPI = CreateObject("SAPI.spvoice")
                        SAPI.Speak("Welcome" & txtuser.Text)
                        'MsgBox("Welcome Admin")
                        For j = 0 To 500
                        Next
                        Me.Hide()
                        Form2.Show()
                        'Me.Close()

                        If CheckBox1.Checked = True Then
                            My.Settings.Session = True
                            My.Settings.Username = txtuser.Text
                            My.Settings.Password = txtpass.Text
                            My.Settings.Save()
                            My.Settings.Reload()
                        ElseIf CheckBox1.Checked = False Then
                            My.Settings.Session = False
                            My.Settings.Username = String.Empty
                            My.Settings.Password = String.Empty
                            My.Settings.Reset()
                            My.Settings.Save()
                            My.Settings.Reload()
                        End If
                        conn.Close()
                        conn.Dispose()
                    ElseIf usertype = "0" Then
                        For j = 0 To 500
                        Next
                        SAPI = CreateObject("SAPI.spvoice")
                        SAPI.Speak("Welcome" & txtuser.Text)
                        'MsgBox("Welcome Normal User")
                        'Me.Hide()
                        Form3.Show()
                        Me.Close()

                        If CheckBox1.Checked = True Then
                            My.Settings.Session = True
                            My.Settings.Username = txtuser.Text
                            My.Settings.Password = txtpass.Text
                            My.Settings.Save()
                            My.Settings.Reload()
                        ElseIf CheckBox1.Checked = False Then
                            My.Settings.Session = False
                            My.Settings.Username = String.Empty
                            My.Settings.Password = String.Empty
                            My.Settings.Reset()
                            My.Settings.Save()
                            My.Settings.Reload()
                        End If
                    ElseIf usertype > 1 Then
                        MsgBox("Error: User Details #1")
                    End If
                Else
                    'MsgBox("User does not exist!")
                    SAPI = CreateObject("SAPI.spvoice")
                    SAPI.Speak("Please Try Again")
                    txtuser.Text = ""
                    txtpass.Text = ""
                    txtuser.Focus()
                End If

                conn.Close()

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                txtuser.Text = ""
                txtpass.Text = ""
            End Try
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If txtpass.Text = "" Or txtuser.Text = "" Then
            MessageBox.Show("Please Fill in Empty TextBox!", "Error")
            txtuser.Focus()
        Else
            lblStatus.Visible = True
            ProgressBarEx1.Visible = True
            ProgressBarEx1.Value = 0
            ProgressBarEx1.ShowPercentage = True
            ProgressBarEx1.ShowText = False
            tmr.Start()
        End If
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        'executes when PictureBox1 is clicked
        If txtpass.UseSystemPasswordChar = True Then
            txtpass.UseSystemPasswordChar = False
        Else
            txtpass.UseSystemPasswordChar = True
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If txtuser.Text = "" And txtpass.Text = "" Then
            MsgBox("Sorry its already empty")
        Else
            CheckBox1.Checked = False
            My.Settings.Session = False
            My.Settings.Username = String.Empty
            My.Settings.Password = String.Empty
            My.Settings.Reset()
            My.Settings.Save()
            txtuser.Text = ""
            txtpass.Text = ""
            txtuser.Focus()
        End If
    End Sub

    Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
        Application.Exit()
    End Sub
    'Start Move Screen Code
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub
    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove

        If IsFormBeingDragged Then
            Dim newLocation As Point = New Point()

            newLocation.X = Me.Location.X + (e.X - MouseDownX)
            newLocation.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = newLocation
            newLocation = Nothing
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

    End Sub
    'End Move Screen Code
End Class


What I have tried:

I have tried options my.settings.save
my.setting.reload
my.setting.reset
none help of this, only if my form1.hide() then its working but i want to close form1 and go to form2 or form3 and next time when login to program to remember the name.
Posted
Updated 19-Oct-19 22:43pm

We can't tell without running your full code - and we don't have it all so we can't check.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Check exactly what you are saiving, and when. Check the values after you saved them. Check them again when you restart.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And bear in mind: if you call close on the main Form, the one the program starts with, all your other forms will also be closed as your app will shut down...
 
Share this answer
 
Hello, yes i know that some people need project to view it.
I manage to find out the problem.
I remove from button1 the save option and call it in Form1_Closed to save information if checkbox is true or false and everything works fine with me.close()
 
Share this answer
 
The problem is probably caused by
My.Settings.Save()
being only called in your Sub tmr_Tick() after a period of time.
Just use My.Settings.Save() before closing the form.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900