在c#中保存在windows表单中的应用程序 [英] Saving in windows form application in c#

查看:102
本文介绍了在c#中保存在windows表单中的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,即form1和form2。 Form1是主窗体,form2是辅助窗体,包含2个复选框,即checkbox1和checkbox2。 checkbox1是假设禁用form1中的按钮。

我想要的是按钮被禁用后我希望form1保存,这样当我关闭并打开form1时我应该看到发生的变化

i have 2 forms,namely form1 and form2. Form1 is the main form and form2 is secondary form which contains 2 checkbox namely checkbox1 and checkbox2. checkbox1 is suppose to disable a button in form1.
what i want is that after the buttons are disable i want form1 is save so that when i close and open form1 i should see the changes has taken place

推荐答案

复选框很简单:在Form2处理的Form2中创建一个事件和一个属性。单击复选框时,发出事件信号,form1可以访问该属性以确定是应启用还是禁用该按钮。

在两种表格之间传递信息,第2部分:儿童到家长 [ ^ ]告诉你如何做。

保存更复杂:它取决于很多因素,例如你想如何保存它,保存多少等等。

最简单的方法是使用设置,然后是自己读取和写入的文件,然后是CSV,XML,数据库,最后是注册表 - 该列表位于我会开始寻找的顺序。



那么你想保存多少?
The check box is easy: create an event and a property in Form2 which Form2 handles. When the checkbox is clicked, signal the event and form1 can access the property to decide if the button should be enabled or disabled.
Transferring information between two forms, Part 2: Child to Parent[^] Shows you how to do it.
Saving is more complex: it depends on a huge number of factors, such as how you want to save it, how much there is to save, and so forth.
The easiest is to use Settings, then there is a file you read and write yourself, then CSV, XML, a database, and finally the registry - and that list is in the order in which I would start looking.

So how much do you want to save?






假设您在 Form1 上有2个按钮,其名称为: button1 button2 ,你需要为每个控件sta创建设置(1个链接对于开始是好的)你要保存。示例:

1.设置名称: Button1Enabled ,输入: Bool

2.设置名称: Button2Enabled ,键入: Bool



然后你应该处理你的Form1事件来存储/恢复按钮状态。

1.当form1关闭时,将您的设置保存到配置文件:

Hi,

Assuming that you have 2 buttons on Form1, and their names are: button1 and button2, you need to create settings (1 link will be good for start) for each control state you want to save. Example:
1. Setting name: Button1Enabled, Type: Bool
2. Setting name: Button2Enabled, Type: Bool

Then you should handle your Form1 events to store/restore buttons state.
1. When the form1 is closed save your settings to configuration file:
protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);
    Properties.Settings.Default.Button1Enabled = button1.Enabled;
    Properties.Settings.Default.Button2Enabled = button2.Enabled;
    Properties.Settings.Default.Save();
}



2.加载form1后,从配置文件加载你的设置:


2. Whe the form1 is loaded load your setting from configuration file:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    button1.Enabled = Properties.Settings.Default.Button1Enabled;
    button2.Enabled = Properties.Settings.Default.Button2Enabled;
}





如需进一步参考,请查看以下链接:

C#中的Windows窗体用户设置 [ ^ ]

http://msdn.microsoft.com/en-us/library/0zszyc6e% 28v = vs.110%29.aspx [ ^ ]



希望它可以帮助你:)



For further referrence you should take a look at this links:
Windows Forms User Settings in C#[^]
http://msdn.microsoft.com/en-us/library/0zszyc6e%28v=vs.110%29.aspx[^]

Hope it helps you :)


in general for work in this domain you should have some knowledge of reflextion (meta programming)
but for your problem you can use <b>control.controls</b> to coloect list of control
then save in an suitable data structure.
use code same as this :
<pre lang="c#">
        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach(Control c in this.Controls)
                if(c is CheckBox)
                    listBox1.Items.Add(c.Text+":"+(c as CheckBox).Checked);
        }


这篇关于在c#中保存在windows表单中的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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