简体   繁体   English

Form1_FormClosing方法'从未使用',设置不保存

[英]Form1_FormClosing method 'never used', settings do not save

I'm saving the location of my application but it does not want to keep the value. 我正在保存我的应用程序的位置,但它不想保留该值。 What happens in the code now is that the _FormClosing is dimmed and "it is never used". 现在代码中发生的事情是_FormClosing变暗并且“它永远不会被使用”。 Is there anybody that can see where I go wrong with this code below? 有没有人可以看到我在下面的代码出错?

public Form1()
    {
        InitializeComponent();            
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (Settings.Default.WindowLocation != null)
            this.Location = Settings.Default.WindowLocation;

        this.txtInput60.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnterKeyPress);
    }

    private void Form1_FormClosing(object sender, FormClosedEventArgs e)
    {
        Settings.Default.WindowLocation = this.Location;
        Settings.Default.Save();
    }

    private void CheckEnterKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            decimal minutes;
            decimal.TryParse(txtInput60.Text, out minutes);

            if (minutes > 0)
            {
                var total = (int) (minutes/60*100);
                txtOutput100.Text = total.ToString();
                Clipboard.SetText(total.ToString());
            }
        }
    }

In my application properties I am setting the WindowLocation with WindowLocation, system.draw.point, user, 0;0 在我的应用程序属性中,我使用WindowLocation,system.draw.point,user,0; 0设置WindowLocation

Sounds like your Form1_FormClosing event is no longer hooked up to the FormClosing event of the form. 听起来像Form1_FormClosing事件不再连接到窗体的FormClosing事件。 You can check this by going to the properties of the form at design-time and selecting events pane, looks like this: 您可以通过在设计时转到表单的属性并选择事件窗格来检查这一点,如下所示:

属性窗格

Make sure your Form1_Closing method is hooked up to the FormClosing event, if not, drop down the list and select it. 确保您的Form1_Closing方法已连接到FormClosing事件,如果没有,请下拉列表并选择它。

You need to attach the event FormClosing to the method Form1_FormClosing. 您需要将事件FormClosing附加到Form1_FormClosing方法。

This can be done in code of the Form_Load method: 这可以在Form_Load方法的代码中完成:

this.FormClosing += Form1_FormClosing;

Or by setting the event in the designer 或者通过在设计器中设置事件

Change the type of the method parameter from FormClosedEventArgs to FormClosingEventArgs: 将方法参数的类型从FormClosedEventArgs更改为FormClosingEventArgs:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Settings.Default.WindowLocation = this.Location;
    Settings.Default.Save();
}

Check in Form1 in Designer. 在Designer中检入Form1。 Open the EventExplorer of Form1 and check the value for the Closing event. 打开Form1的EventExplorer并检查Closing事件的值。 Maybe it is not attached to your method. 也许它与您的方法无关。

您的设计器可能没有附加到FormClosed事件,尝试在构造函数中添加它,就在InitializeComponent();

this.FormClosing += Form1_FormClosing;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM