保存对表单控件所做的更改? [英] Saving changes made to controls on form?

查看:24
本文介绍了保存对表单控件所做的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存设置"以便在应用程序关闭后再次使用它们?

How can I save "settings" so that they can be used again after the application has been closed?

当我说设置时,我指的是表单上控件的不同属性.

When I say settings I mean different properties of the controls on my form.

最简单、最合适的方法是什么?我读过你可以保存到系统注册表或 xml 文件吗?

What is the easiest and most appropriate method? I've read you can save to the system registry or a xml file?

我有一个表单可以在表中创建一行,该行会根据设置或更改的控件而变化.

I have a form that creates a row in a Table that changes depending on what control is set or change.

我希望能够保存不同的配置并将它们显示在组合框中以供重复使用.

I would like to be able to save different configurations and display them in a combobox for repeated use.

  1. 最终用户填写所有文本框并勾选复选框.
  2. 然后他们点击添加到收藏夹
  3. 他们添加一个喜欢的名字并保存
  4. 然后保存在收藏夹组合框中永久可见.

我的表格

推荐答案

保存数据的一种方法是将其写入注册表,位于 HKCU 节点下.这样,即使应用程序在同一台机器上,应用程序的不同用户也将拥有自己的设置.它还使文件系统更简洁,并且不需要数据库.但缺点是收藏夹只存在于机器上,不会与用户跨设备漫游.

One way you could save the data would be to write it to the registry, under the HKCU node. This way different users of your application will have their own settings even if the app is on the same machine. It also keeps the file system a little cleaner and doesn't require a database. But the downside is that the favorites only live on the machine, and don't roam with the user across devices.

实现这一点的一种方法是将您的表单设置包装在一个知道如何从注册表中保存和加载值的类中.这与注册表帮助程序类一起可以很容易地将收藏夹"功能添加到您的表单中.

A way to implement this would be to wrap your form settings in a class that knows how to save and load values from the registry. This, along with a registry helper class, could make it pretty easy to add "Favorites" functionality to your form.

例如,您可以首先创建一个注册表助手类,它将读取和写入 HKCU 节点的设置(因此这些设置特定于登录用户):

For example, you could first create a Registry helper class that will read and write settings to the HKCU node (so the settings are specific to the logged in user):

public class RegHelper
{
    private static readonly RegistryKey Root = Registry.CurrentUser
        .CreateSubKey(@"Software\CompanyName\ApplicationName");

    private readonly RegistryKey _thisKey = Root;

    public RegHelper() { }

    public RegHelper(string favoriteKey)
    {
        _thisKey = Root.CreateSubKey(favoriteKey);
    }

    public List<string> GetSubKeys()
    {
        return _thisKey.GetSubKeyNames().ToList();
    }

    public void SetProperty(string propertyName, string value)
    {
        _thisKey.SetValue(propertyName, value, RegistryValueKind.String);
    }

    public void SetProperty(string propertyName, bool value)
    {
        SetProperty(propertyName, value.ToString());
    }

    public string GetProperty(string propertyName)
    {
        return GetProperty(propertyName, string.Empty);
    }

    public string GetProperty(string propertyName, string defaultValue)
    {
        return _thisKey.GetValue(propertyName, defaultValue).ToString();
    }

    public bool GetPropertyAsBool(string propertyName)
    {
        return bool.Parse(GetProperty(propertyName, default(bool).ToString()));
    }
}

然后,您可以将表单的字段包装到一个类中,该类不仅具有与表单字段匹配的属性,而且还具有将值保存到注册表的方法和一些加载所有收藏夹或特定命名的静态方法最喜欢的.例如:

Then, you could wrap the fields of your form into a class that not only has properties that match your form fields, but also has methods to save the values to the registry and some static methods to load all Favorites or a specific named Favorite. For example:

public class Favorite
{
    public string Name { get; private set; }
    public string Notes { get; set; }
    public bool NotesFromPlanner { get; set; }
    public string Project { get; set; }
    public string DbLocation { get; set; }
    public string AssesmentToolVersion { get; set; }
    public string ProjectCodes { get; set; }
    public bool StraightToNew { get; set; }

    public Favorite(string name)
    {
        this.Name = name;
    }

    public void Save()
    {
        var reg = new RegHelper(this.Name);
        reg.SetProperty("Name", Name);
        reg.SetProperty("Notes", Notes);
        reg.SetProperty("NotesFromPlanner", NotesFromPlanner);
        reg.SetProperty("Project", Project);
        reg.SetProperty("DbLocation", DbLocation);
        reg.SetProperty("AssesmentToolVersion", AssesmentToolVersion);
        reg.SetProperty("ProjectCodes", ProjectCodes);
        reg.SetProperty("StraightToNew", StraightToNew);
    }

    public static Favorite GetFavorite(string favoriteName)
    {
        var reg = new RegHelper(favoriteName);
        return new Favorite(favoriteName)
        {
            Notes = reg.GetProperty("Notes"),
            NotesFromPlanner = reg.GetPropertyAsBool("NotesFromPlanner"),
            Project = reg.GetProperty("Project"),
            DbLocation = reg.GetProperty("DbLocation"),
            AssesmentToolVersion = reg.GetProperty("AssesmentToolVersion"),
            ProjectCodes = reg.GetProperty("ProjectCodes"),
            StraightToNew = reg.GetPropertyAsBool("StraightToNew"),
        };
    }

    public static List<Favorite> GetFavorites()
    {
        return new RegHelper().GetSubKeys().Select(GetFavorite).ToList();
    }

    public override string ToString()
    {
        return this.Name;
    }
}

然后,您可以使用收藏夹"类来填充收藏夹"下拉列表:

Then, you could use the Favorite class to populate your Favorites drop down:

private void Form1_Load(object sender, EventArgs e)
{
    // Get all saved favorites and load them up in the combo box
    foreach (var favorite in Favorite.GetFavorites())
    {
        cboFavorites.Items.Add(favorite);
    }
}

现在,当从组合框中选择收藏夹时,我们想用详细信息填充我们的表单:

Now, when a favorite is picked from the combo box, we want to populate our form with the details:

private void cboFavorites_SelectedIndexChanged(object sender, EventArgs e)
{
    var favorite = (Favorite) cboFavorites.SelectedItem;

    txtNotes.Text = favorite.Notes;
    txtAssetToolVersion.Text = favorite.AssesmentToolVersion;
    txtDbLocation.Text = favorite.DbLocation;
    chkNotesFromPlanner.Checked = favorite.NotesFromPlanner;
    txtProjectCodes.Text = favorite.ProjectCodes;
    cboProjects.Text = favorite.Project;
    chkStraightToNew.Checked = favorite.StraightToNew;
}

当有人点击保存收藏"时,我们希望将收藏的详细信息添加(或更新)到注册表中:

And when someone clicks "Save Favorite", we want to add (or update) the favorite details to the registry:

private void btnAddFavorite_Click(object sender, EventArgs e)
{
    string favoriteName = cboFavorites.Text;

    if (string.IsNullOrEmpty(favoriteName))
    {
        MessageBox.Show("Please type a name for the favorite in the Favorites box.");
        return;
    }

    var favorite = new Favorite(favoriteName)
    {
        Notes = txtNotes.Text,
        AssesmentToolVersion = txtAssetToolVersion.Text,
        DbLocation = txtDbLocation.Text,
        NotesFromPlanner = chkNotesFromPlanner.Checked,
        ProjectCodes = txtProjectCodes.Text,
        Project = cboProjects.Text,
        StraightToNew = chkStraightToNew.Checked
    };

    favorite.Save();

    // When saving a favorite, add it to the combo box
    // (remove the old one first if it already existed)
    var existingFav = cboFavorites.Items.Cast<Favorite>()
        .FirstOrDefault(fav => fav.Name == favoriteName);
    if (existingFav != null)
    {
        cboFavorites.Items.Remove(existingFav);
    }

    cboFavorites.Items.Add(favorite);
    cboFavorites.Text = favoriteName;
}

如果您想走注册路线,这应该足以让您入门.

This should be enough to get you started, if you want to go the registry route.

这篇关于保存对表单控件所做的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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