3

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.

sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • 1
    The `static` keyword might be useful to you. – Jaroslav Jandek Mar 09 '11 at 10:19
  • What do you mean by "when the form is run again" - when the application is restarted? When a new instance of the form is created within the same process? – Jon Skeet Mar 09 '11 at 10:19
  • Yes, the application is run again – sqlchild Mar 09 '11 at 12:31
  • I have taken a look at your code and it seems fine, however is it just this code, or is there something else running the form? – Josef Van Zyl Mar 10 '11 at 06:30
  • @Josefvz : its this much only sir. but not working. when i close the application and run it again, then the value of the isloginlocked is lost, but i want that , if it was 3 when the application was closed last time, then the button is disabled this time. – sqlchild Mar 10 '11 at 06:34
  • Yes, you are saving the value only in memory, so it is not kept when the app is closed. Save the value to a file or something. – Josef Van Zyl Mar 10 '11 at 07:16
  • @Josefvz, ok , this would be one way, for this i have to export the value using bcp or is there something else also like filestream etc? also , what do you use to lock a login of sql server when a user tries to login using incorrect credentials via a winform – sqlchild Mar 10 '11 at 07:20
  • @Josefvz: so according to you , there's no way to keep a value in a variable , if the application is closed. – sqlchild Mar 10 '11 at 07:21
  • what do you use to lock a login of sql server when a user tries to login using incorrect credentials via a winform: Please ask a new question for this. – Josef Van Zyl Mar 10 '11 at 07:48
  • No you misunderstand me. If you would like to keep the value of a variable you can save it. There are a lot of ways to do that: SQL Flatfiles ect... just look at what would be best for you – Josef Van Zyl Mar 10 '11 at 07:50

5 Answers5

5

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

Community
  • 1
  • 1
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
3

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).

Jon
  • 428,835
  • 81
  • 738
  • 806
1

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

Josef Van Zyl
  • 915
  • 3
  • 19
  • 43
1

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)

Fadrian Sudaman
  • 6,405
  • 21
  • 29
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

harlie Brown
  • 15
  • 1
  • 9