dear sir
i have one check box and one button.when i open form above button is hide. if i tick check box and close the form and when i re open form i want to display above button please help me how to do it.
thank you
wansa

Public Class Form1
    Dim settings As New My.MySettings
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.check = True Then
            Button1.Show()

        Else
            Button1.Hide()
        End If
        My.Settings.Save()

    End Sub
End Class

Recommended Answers

All 8 Replies

Hi,

You have to save settings on Checkbox checkstate change event or in form close event too. Then only it work as per your expectation.

 Private Sub CheckBox1_CheckStateChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckStateChanged
        If CheckBox1.CheckState = CheckState.Checked Then
            My.Settings.Check = True
        Else
            My.Settings.Check = False
        End If
        My.Settings.Save()
    End Sub

Try this and update the status.

Regards,
Shailaja Manoharan

dear sir
thank you for your reply .i try but nothing happend .any way i send you fuly details.if you have any freetime please check which area missed place
thanks again
wansa

Public Class Form1
    Dim settings As New My.MySettings
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.check = True Then
            Button1.Show()
        Else
            Button1.Hide()
        End If
        My.Settings.Save()
    End Sub
    Private Sub CheckBox1_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckStateChanged
        If CheckBox1.CheckState = CheckState.Checked Then
            My.Settings.check = True
        Else
            My.Settings.check = False
        End If
        My.Settings.Save()
    End Sub
End Class

Assuming that your settings variable is a Boolean you can just do

CheckBox1.Checked = My.Settings.check
Button1.Visible = My.Settings.check

in your form load event handler. You'll want to restore the Checkbox state as well so the two stay in sync. Just make that in the form closing handler you do

My.Settings.check = Button1.Visible

to save the last state.

dear sir
i cant get your idea.please explan me where i am need to edit.
sorry for inconvence of cituation .
thank you
wansa

You don't need the line

Dim settings As New My.MySettings

Delete it. Go to

Project -> Properties

then select the Settings tab. Create a variable named ButtonVisible. You should use a name like that rather than checked because it more clearly indicates what state you are trying to preserve. Make ButtonVisible a Boolean. Set the Value field to False.

Your code will look like

Public Class Form1

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

        Button1.Visible = My.Settings.ButtonVisible
        CheckBox1.Checked = My.Settings.ButtonVisible

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        My.Settings.ButtonVisible = Button1.Visible

    End Sub

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

        Dim chk As CheckBox = sender
        Button1.Visible = chk.Checked

    End Sub

End Class
commented: Classy. +0

I strongly urge you to get into the habit of naming your controls for their purpose. For demo purposes, Button1 and Checkbox1 are fine but in a larger program with many controls it will become impossible to follow your code. A control named btnSubmitForm is a lot clearer than Button17.

commented: The old "what does this do?" problem. +12

Dear Sir
Thank you very much my problem was solved.it is work 100%. i don't know how to thank you. Thank you for your time sir. Really appreciate your help.
thank you again
wansa.

Glad to help. Come on back if you have more questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.