在同一应用程序中,从一种形式获取信息并在另一种形式中使用它. [英] Take information from one form and use it in another, within the same application.

查看:75
本文介绍了在同一应用程序中,从一种形式获取信息并在另一种形式中使用它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个首选项"表单,每当关闭该表单时,都不会进行更改.在窗体2中选中一个复选框时,我希望更改窗体1的功能.但是,我想关闭第二个表格.并且当它关闭时,功能不会保存.因此,当我再次打开首选项窗口时,将设置默认值.我相信我需要将信息保存到某种文本文件中.并从文本文件中读取表格1.有帮助吗?

I have a Preference form in my application, and whenever it is closed, changes are not made. When a check box is checked in form 2, I want a feature of form 1 to be changed. However, I want this second form to be closed. And when it is closed, the features are not saved. So when I open the preference window again, the defaults are set. I believe I need to save the information into some sort of text file. And have form 1 read off of the text file. Any help?

推荐答案

您好
我也有这个问题

这是我的答案 [ http://www.codeproject.com/Questions/216424/How-run-a-method-in -parent-from-when-closesingchild ]
Hi
I too had this question

this my answer [http://www.codeproject.com/Questions/216424/How-run-a-method-in-parent-from-when-closing-child]


如果我正确阅读了您的问题,则需要一个单独的类来存储您的首选项,该类甚至可以静态的,这将有助于处理首选项.例如(伪代码):

If I read your question correctly, you would need a separate class where you store your preferences, This class could be even static which would help in handling the preferences. For example (pseudo code):

public static class Preferences {
   public int FontSize { get; set; }
   ...
}



现在,您可以直接从窗口中修改首选项.



Now you can modify the preferences directly from your window.

Preferences.FontSize = 123;



如果您想以另一种形式对更改做出反应,则可以使用事件.例如(再次为伪):



If you want to react on the changes in another form you can use events for that. For example (pseudo again):

public static class Preferences {
   public static EventHandler SomethingHasChanged;

   public int FontSize { 
      get {
         ...
      } 
      set {
         ...
         EventHandler theEvent = SomethingHasChanged;
         if (theEvent != null) {
            theEvent();        
         }
      }
   ...



只需记住,当您在实例(例如,表单)中订阅事件时,在关闭表单时也必须取消订阅该事件.否则,由于事件发布者是静态的,因此不会清除该引用.

如果愿意,可以将首选项中的信息保存到文件等中.一种方法是将其序列化为文件.一个很好的例子: http://www.switchonthecode.com/tutorials/csharp- tutorial-serialize-objects-to-a-file [



Just remember that when you subscribe the event in an instance (for example in a form), you must also unsubscribe it when the form is closed. Otherwise the reference isn''t cleaned because the event publisher is static.

If you wish, you can save the information in your preferences into a file etc. One way is to serialize it to a file. One good example: http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file[^]

Addition (example)
First add a new static class to the project:

public static class Preferences {
   public static event System.EventHandler PrefChanged;
   private static bool _someSetting = false;

   public static bool SomeSetting {
      get {
         return Preferences._someSetting;
      }
      set {
         Preferences._someSetting = value;
         if (PrefChanged != null) {
            PrefChanged(null, new System.EventArgs());
         }
      }
   }
}



现在,在要接收首选项更改的Form1中,向构造函数添加例如:



Now in the Form1 where you want to receive changes in the preferences add for example to constructor:

Preferences.PrefChanged += new System.EventHandler(Preferences_PrefChanged);



以及这种形式的方法:



And a method to this form:

public void Preferences_PrefChanged(object sender, System.EventArgs e) {
   this.TextBox1.Text = ("SomeSetting is now " + Preferences.SomeSetting.ToString());
}



在修改设置的form2中,将其添加到复选框的click事件中:



And in the form2 where you modify settings, add to the click event of the checkbox:

Preferences.SomeSetting = this.checkBox1.Value;



在关闭Form1的地方,您还应该添加:



Also somewhere where Form1 is closed you should add:

Preferences.PrefChanged -= new System.EventHandler(Preferences_PrefChanged);


此后,如果您运行该程序(代码块中可能有错字),则每当更改复选框的值时,文本框应显示设置的当前值.


After this if you run the program (there may be typos in the code blocks), whenever you change the value of the checkbox the textbox should show the current value of the setting.


这篇关于在同一应用程序中,从一种形式获取信息并在另一种形式中使用它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆