2011-12-19 123 views
0

我保存我的应用程序的位置,但它不想保留值。现在代码中发生的事情是_FormClosing变灰,并且“从未使用过”。有没有人可以看到我下面的代码出错了?Form1_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,用户,0设置WindowLocation; 0

+0

请更改标题到更合适的和描述性的东西。目前的标题太笼统了。 – 2011-12-19 13:07:56

回答

1

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

这可以在Form_Load方法的代码来完成:

this.FormClosing += Form1_FormClosing; 

或者通过setting the event in the designer

更改方法参数的从FormClosedEventArgs到FormClosingEventArgs类型:

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

这不起作用。它提供了有关form1_formclosing 没有重载“Form1_FormClosing”匹配委托“System.Windows.Forms.FormClosingEventHandler” – 2011-12-19 13:02:40

+0

从FormClosedEventArgs更改方法参数的类型FormClosingEventArgs – 2011-12-19 13:04:38

+0

好抓,感谢 – 2011-12-19 13:06:20

1

听起来像是你Form1_FormClosing事件不再挂接到FormClosing事件形成。您可以通过在设计时窗体的属性,然后选择活动窗格中检查这一点,是这样的:

Properties Pane

确保您Form1_Closing方法挂接到FormClosing事件,如果没有,下拉列表并选择它。

0

入住设计器中的Form1。打开Form1的EventExplorer并检查Closing事件的值。也许它不附在你的方法上。

0

你的设计者可能没有连接到FormClosed事件,尝试在构造函数中添加此,刚过InitializeComponent();

this.FormClosing += Form1_FormClosing; 
相关问题