0

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
Attachments 1.png 5.09 KB 1.png 5.09 KB 2.png 5.13 KB
3
Contributors
8
Replies
26
Views
4 Months
Discussion Span
Last Post by Reverend Jim
0

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

0

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
Attachments 1.png 21.74 KB 3.png 5.94 KB 4.png 5 KB
0

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.

Edited by Reverend Jim

0

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

2

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

Edited by Reverend Jim

Votes + Comments
Classy.
2

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.

Votes + Comments
The old "what does this do?" problem.
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.