opendialog to show a file and save it with checkbox vb.net

116 views Asked by At

I'm trying to connect to a database (mdb file of my choice) in a login screen and i want to save it for faster logon next times i boot the software.

I click on choose database button, opendialog lets me choose the file, i click OK and the db location shows in a textbox. there's a checkbox beneath to save it before i connect to it.

But i can't manage to keep the checkbox checked, nor the textbox filled after i restart te program.

here's my current code:

    Public Class LoginScreen
    
        Private Sub Loginscreen_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ProgressBar2.Minimum = 0
            ProgressBar2.Maximum = 100
            ProgressBar2.Visible = False
            Panel1.Visible = False
    
        End Sub
        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Application.Exit()
        End Sub
        Private Sub btnExit2_Click(sender As Object, e As EventArgs) Handles btnExit2.Click
            Application.Exit()
        End Sub
    
        Private Sub tmrLogin_Tick(sender As Object, e As EventArgs) Handles tmrLogin.Tick
            ProgressBar2.Value = ProgressBar2.Value + 20
            lblLoginMessages.Text = ProgressBar2.Value & "%" & " Completed"
            If ProgressBar2.Value >= 100 Then
                tmrLogin.Enabled = False
                If txtUser.Text = "azert" And txtPassword.Text = "azert" Then
                    ProgressBar2.Value = 0
                Else
                    lblLoginMessages.Text = "Wrong credentials, Try again!"
                    pboxClosed.Visible = True
                    PboxOpen.Visible = False
    
                    ProgressBar2.Value = 0
                    txtPassword.Text = ""
                    txtUser.Text = ""
                End If
            End If
        End Sub
    
        Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
            ProgressBar2.Visible = True
            tmrLogin.Enabled = True
    
            pboxClosed.Visible = False
            PboxOpen.Visible = True
            ''navraag doen om dit correct in te stellen! ! ! ! !! 
            'If ProgressBar2.Value = 100 Then
            'lblLoginMessages.Text = "Logging in..."
            Me.Hide()
                Mainscreen.Show()
            'End If
            If chkSavePassword.Checked = True Then
                My.Settings.databaselocation = txtDatabaselocationshow.Text
                My.Settings.SaveLocation = True
    
    
            End If
        End Sub
    
        Private Sub btnDBConnect_Click(sender As Object, e As EventArgs) Handles btnDBConnect.Click
            If Panel1.Visible = False Then
                Panel1.Visible = True
            Else
                Application.Exit()
    
            End If
        End Sub
    
        Private Sub btnChoose_Click(sender As Object, e As EventArgs) Handles btnChoose.Click
            Dim strtext As String
            OpenFileDialog1.Filter = "Database Files | *.mdb"
            OpenFileDialog1.InitialDirectory = "F:\GoogleDrive\EINDWERK VBNET"
            OpenFileDialog1.Title = "Choose your Database"
            OpenFileDialog1.ShowDialog()
    
            strtext = OpenFileDialog1.FileName
            txtDatabaselocationshow.Text = strtext
            'If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            '    strtext = OpenFileDialog1.FileName
            '    txtDatabaselocationshow.Text = strtext
            'Else
            '    MsgBox("Error: the database file could not be read, try again.")
            'End If
    
        End Sub
    
        Private Sub tmrshowloginpanel_Tick(sender As Object, e As EventArgs) Handles tmrshowloginpanel.Tick
            Panel1.Width += 5
            If Panel1.Width >= 700 Then
                tmrshowloginpanel.Stop()
            End If
        End Sub
    
    End Class

i've scoured the net but can't really find what to do?

if you need more information, shoot!

1

There are 1 answers

2
Caius Jard On BEST ANSWER

Walk through this with me:

  • Make a new project, one form, add a textbox and a combobox.

  • Click the textbox:

enter image description here

  • In the properties grid at the top click (Application Settings), then the 3 dots next to Property Binding. Scroll to Text. Drop down the setting and choose New at the bottom.

enter image description here

  • Give it a name of ChosenDatabasePath or something useful and descriptive like that

enter image description here

  • Repeat from "Click the textbox" for the checkbox instead, and this time bind the Checked property not the Text property.. Call the checkbox's Checked binding SavePassword or similar

  • Close all the dialogs so you're back at the form

  • Click anywhere on the background of the form when switch to Events in the property grid, find FormClosing and double click it

enter image description here

  • Put My.Settings.Save() in the FormClosing event handler

  • Run the app, write something in the textbox, close the form (by the X, not by stopping debugging), then immediately open the app again (run it)