i have a Form which has a button, on the button click event, a variable locklogin is increased by 1
when locklogin =3 , then the form button gets disabled and the form needs to be closed. on closing the form , locklogin loses its value.
but i want to hold its value albeit the form being closed and when the form is run again(the whole application is executed again), then the button is still disabled. how do i do this?
public partial class Form1 : Form
{
static int loginlocked;
static int isloginlocked;
public Form1()
{
InitializeComponent();
if (isloginlocked == 3)
{
foreach (Control c in this.Controls)
{ c.Enabled = false; }
}
}
private void button1_Click(object sender, EventArgs e)
{
loginlocked++;
if (loginlocked == 3)
{
foreach (Control c in this.Controls)
{ c.Enabled = false; }
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
isloginlocked = loginlocked;
if (e.CloseReason == CloseReason.UserClosing)
{
if (MessageBox.Show(this, "Really?", "Closing...",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
== DialogResult.Cancel) e.Cancel = true;
}
}
}
i want that when the form/application is opened then first it checks whether the value of the variable is =3 , and if its 3 ,then it should disable the button on it.
I would start thinking about separating your logic from your UI. There are various ways of achieving this and I've included a few links to get started.
I would have a Controller or Presenter object listening for an event from your form when the button is clicked. This Controller object maintains the counter and is responsible for creating and destroying the form and setting initial values during construction such as whether or not a button is disabled. The form can be as dumb as possible and not have to worry about such business logic.