簡體   English   中英

Form1_FormClosing方法'從未使用',設置不保存

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

我正在保存我的應用程序的位置,但它不想保留該值。 現在代碼中發生的事情是_FormClosing變暗並且“它永遠不會被使用”。 有沒有人可以看到我在下面的代碼出錯?

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

在我的應用程序屬性中,我使用WindowLocation,system.draw.point,user,0; 0設置WindowLocation

聽起來像Form1_FormClosing事件不再連接到窗體的FormClosing事件。 您可以通過在設計時轉到表單的屬性並選擇事件窗格來檢查這一點,如下所示:

屬性窗格

確保您的Form1_Closing方法已連接到FormClosing事件,如果沒有,請下拉列表並選擇它。

您需要將事件FormClosing附加到Form1_FormClosing方法。

這可以在Form_Load方法的代碼中完成:

this.FormClosing += Form1_FormClosing;

或者通過在設計器中設置事件

將方法參數的類型從FormClosedEventArgs更改為FormClosingEventArgs:

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

在Designer中檢入Form1。 打開Form1的EventExplorer並檢查Closing事件的值。 也許它與您的方法無關。

您的設計器可能沒有附加到FormClosed事件,嘗試在構造函數中添加它,就在InitializeComponent();

this.FormClosing += Form1_FormClosing;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM