开发者

holding the value of a variable in c#.net winforms

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.开发者_C百科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.

Model-View-Controller

Model-View-Presenter

Some discussion and examples


By making the variable static.

Of course, since you imply that a new form will be created several times in your program, you will have to set the enabled state of the button in the constructor (after InitializeComponent).


If your application closes when the form closes You Could save the variable to a flatfile or xml file. Or if your application is still running when you close the form, declare the variable somewhere else than the form


Create a static variable or a simple singleton like class with a counter. Instead of resetting the value, use Mod operator so locklogin % 3 == 0, you close the form. The lock login value can keep incrementing without losing value. You will need to deal with the first time the variable is use (such as locklogin != 0)


u can use setting class. A very good example can be found here- http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜