在 c#.net winforms 中保存变量的值

发布于 2024-10-21 13:53:02 字数 1259 浏览 4 评论 0原文

我有一个带有按钮的表单,在按钮单击事件上,当 locklogin =3 时,变量 locklogin 增加 1

,然后表单按钮被禁用并且需要关闭表单。 关闭表单后,locklogin 就失去了它的价值。

但我想保持它的值,尽管表单被关闭,并且当表单再次运行时(整个应用程序再次执行),按钮仍然被禁用。我该怎么做?

 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;
        }
    }
}

我希望当表单/应用程序打开时,首先检查变量的值是否为 =3 ,如果为 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海螺姑娘 2024-10-28 13:53:02

我会开始考虑将逻辑与用户界面分开。有多种方法可以实现这一目标,我提供了一些入门链接。

单击按钮时,我将有一个 Controller 或 Presenter 对象侦听表单中的事件。该控制器对象维护计数器,并负责创建和销毁表单以及在构造期间设置初始值,例如是否禁用按钮。表单可以尽可能的愚蠢,而不必担心这样的业务逻辑。

模型-视图-控制器

模型-视图-Presenter

一些讨论和示例

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

眼前雾蒙蒙 2024-10-28 13:53:02

通过使变量静态

当然,由于您暗示将在程序中多次创建表单,因此您必须在构造函数中设置按钮的启用状态(在InitializeComponent之后) )。

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

聽兲甴掵 2024-10-28 13:53:02

如果您的应用程序在表单关闭时关闭,您可以将变量保存到平面文件或 xml 文件中。
或者,如果关闭表单时应用程序仍在运行,请在表单之外的其他位置声明变量

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

感情废物 2024-10-28 13:53:02

创建一个静态变量或一个带有计数器的简单单例类。不要重置该值,而是使用 Mod 运算符,这样 locklogin % 3 == 0 即可关闭表单。锁登录值可以不断递增而不失值。您将需要在第一次使用该变量时进行处理(例如locklogin != 0)

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)

落日海湾 2024-10-28 13:53:02

你可以使用设置类。可以在这里找到一个非常好的示例 - http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文