拼吾爱程序人生

首页 » .Net编程 » C# » WinForm:应用程序设置(Application Settings)的应用
SystemPoster - 2007-8-11 3:54:00
示例:在应用程序中通过“选项”改变程序中的控制

选择CheckBox,为其Checked属性设置ApplicationSettings。

此操作会

1.自动在Properties中的Settings.settings中添加两个属性值:CloseDocumentPrompt和CloseBatchPrompt。

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CloseDocumentPrompt {
get {
return ((bool)(this["CloseDocumentPrompt"]));
}
set {
this["CloseDocumentPrompt"] = value;
}
}

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CloseBatchPrompt {
get {
return ((bool)(this["CloseBatchPrompt"]));
}
set {
this["CloseBatchPrompt"] = value;
}
}

2.会自动生成App.config文件,其内容为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Sample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Sample.Properties.Settings>
<setting name="CloseDocumentPrompt" serializeAs="String">
<value>True</value>
</setting>
<setting name="CloseBatchPrompt" serializeAs="String">
<value>True</value>
</setting>
</Sample.Properties.Settings>
</userSettings>
</configuration>
**************************************************************************************

Form代码frmOption.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Sample.Properties;

namespace Sample{
public partial class frmOption : Form {
private Settings settings;
internal Settings Settings {
get {
return settings;
}
set {
settings = value;
}
}

public frmOption() {
InitializeComponent();
}

private void butnCancel_Click(object sender, EventArgs e) {
this.Close();
}

private void butnApply_Click(object sender, EventArgs e) {
UpdateOptionSetting();
}

private void butnOK_Click(object sender, EventArgs e) {
UpdateOptionSetting();
this.Close();
}

public void UpdateOptionSetting() {
if (chkbCloseDocumentPrompt.Checked)
settings.CloseDocumentPrompt = true;
else
settings.CloseDocumentPrompt = false;

if (chkbCloseBatchPrompt.Checked)
settings.CloseBatchPrompt = true;
else
settings.CloseBatchPrompt = false;
}

private void frmOption_Load(object sender, EventArgs e) {
if (settings.CloseDocumentPrompt)
chkbCloseDocumentPrompt.Checked = true;
else
chkbCloseDocumentPrompt.Checked = false;

if (settings.CloseBatchPrompt)
chkbCloseBatchPrompt.Checked = true;
else
chkbCloseBatchPrompt.Checked = false;
}

private void frmOption_FormClosing(object sender, FormClosingEventArgs e) {
settings.Save();
}
}
}

**************************************************************************************

应用程序中的应用:

private Settings settings = new Settings();
internal Settings Settings {
get {
return settings;
}
}

public void InitializeTreeView() {
if (settings.CloseDocumentPrompt)
ctrvBatch.ShowSaveDocumentPrompt = true; //设置TreeView是否显示保存文档的提示的属性
else
ctrvBatch.ShowSaveDocumentPrompt = false;
settings.SettingChanging +=new System.Configuration.SettingChangingEventHandler(settings_SettingChanging);
}
}
private void settings_SettingChanging(object sender, SettingChangingEventArgs e) {
if (e.SettingName == "CloseDocumentPrompt") {
if ((bool)e.NewValue)
ctrvBatch.ShowSaveDocumentPrompt = true;
else
ctrvBatch.ShowSaveDocumentPrompt = false;
}
}
}

 您可能对 [C#] 的这些文章也感兴趣:

论C#变得越来越臃肿是不可避免的
查询IP所在区段(C#)
在C#中利用SharpZipLib进行文件的压缩和解压缩
C# - Append a host header by code in IIS
在C#中调用Microsoft.VisualBasic命名空间下的类型验证函数
使用泛型实现单例提供者(原创翻译)
C#2 anonymous methods
Master Pages: Tips, Tricks, and Traps
Microsoft .NET 框架资源基础
基于.Net平台应用程序唯一运行实例实现
.net Framework 2.0 专门提供了配置文件的操作
正确实现 IDisposable
1
查看完整版本: WinForm:应用程序设置(Application Settings)的应用
Modify by pin5i DZNT_ExpandPackage 2.1.3295 2007-2009 pin5i.com
 Total Unique Visitors: