コントロールのプロパティを外部のファイルに保存する方法と、コントロールのプロパティをユーザーごとの設定ファイルに保存する方法を紹介します。
下図のUIを作成します。Buttonを2つ配置します。Panelを1つ配置します。PanelのBackColorプロパティをデフォルトから変更します。今回は(R,G,B)=(255,192,192)に設定しました。
panel1をクリックして選択しプロパティウィンドウを表示します。"(ApplicationSettings)"の"(PropertyBinding)"を選択します。
"(PropertyBinding)"の右側の[...]ボタンをクリックします。"panel1 のアプリケーション設定"ダイアログボックスが表示されます。
[プロパティをアプリケーション設定にバインドする:]から"BackColor"を選択し右側のドロップダウンボックスをクリックします。ドロップダウンメニューの一番下の[新規]をクリックします。
[新しいアプリケーション設定]ダイアログボックスが表示されます。
Name欄に名前を入力します。今回は"Panel1Color"としました。入力後ダイアログボックスの[OK]ボタンを押します。
"panel1 のアプリケーション設定"ダイアログボックスに戻ると"BackColor"の欄に先ほど設定したName欄の値"Panel1Color"が設定されています。[OK]ボタンを押しダイアログを閉じます。
プロパティウィンドウの"(ApplicationSettings)"の下に"BackColor"が追加されていることが確認できます。
以下のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SaveControInfoToExternalFile
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
panel1.BackColor = colorDialog1.Color;
SaveControInfoToExternalFile.Properties.Settings.Default.Save();
//global::SaveControInfoToExternalFile.Properties.Settings.Default.Save();
//上記でもOK
}
}
private void button2_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Reload();
Properties.Settings.Default.Reset();
}
}
}
if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
panel1.BackColor = colorDialog1.Color;
SaveControInfoToExternalFile.Properties.Settings.Default.Save();
}
カラーダイアログボックスを表示し、指定された色をPanelのBackColorに設定します。Properties.Settings.Default.Saveメソッドを呼び出しBackColorプロパティの値を設定ファイルに保存します。
Properties.Settings.Default.Reload();
Properties.Settings.Default.Reset();
Properties.Settings.Default.Reloadメソッドを呼び出し設定ファイルを再読み込みします。Properties.Settings.Default.Resetメソッドを呼び出し設定値を初期化します。
プロジェクトをビルドします。プロジェクトディレクトリの"bin\Debug"ディレクトリに"プロジェクト名.exe.config"ファイル(今回の場合は"SaveControlInfoToExternalFile.exe.config")が作成されています。
"SaveControlInfoToExternalFile.exe.config"ファイルをメモ帳で開きます。先ほどVisual Studioで設定したpanel1のBackColorプロパティの値がファイル内に記述されているのが確認できます。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SaveControInfoToExternalFile.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<SaveControInfoToExternalFile.Properties.Settings>
<setting name="Panel1Color" serializeAs="String">
<value>256, 192, 192</value>
</setting>
</SaveControInfoToExternalFile.Properties.Settings>
</userSettings>
</configuration>
プロジェクトを実行します。下図のウィンドウが表示されます。
いったんアプリケーションを終了し、実行ファイルと同じディレクトリにある、"SaveControlInfoToExternalFile.exe.config"ファイルを編集します。
<userSettings>
<SaveControInfoToExternalFile.Properties.Settings>
<setting name="Panel1Color" serializeAs="String">
<value>256, 192, 192</value>
</setting>
</SaveControInfoToExternalFile.Properties.Settings>
</userSettings>
<userSettings>
<SaveControInfoToExternalFile.Properties.Settings>
<setting name="Panel1Color" serializeAs="String">
<value>96, 192, 192</value>
</setting>
</SaveControInfoToExternalFile.Properties.Settings>
</userSettings>
編集後プログラムを起動するとパネルの色が変わります。(下図参照)
再度プロジェクトを実行します。下図のウィンドウが表示されます。
button1をクリックします。ColorDialogが表示されます。(下図参照)
ColorDialogで色を選択し確定するとPanelの色がColorDialogで選択した色に変更されます。
プログラムを終了させ、再度実行しても先に設定したカラーは保存され黄色のパネルが表示されます。
実行ファイルと同じ位置にある、"SaveControlInfoToExternalFile.exe.config"ファイルを開きます。中を確認しますが、ColorDialogで選択した色のカラーコードは保存されていません。
Properties.Settings.Default.Save()メソッドで保存したプロパティの値は、
ディレクトリの下に保存されます。
今回の場合は
に保存されています。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<SaveControInfoToExternalFile.Properties.Settings>
<setting name="Panel1Color" serializeAs="String">
<value>255, 255, 128</value>
</setting>
</SaveControInfoToExternalFile.Properties.Settings>
</userSettings>
</configuration>
button2をクリックするとユーザー設定の情報がリセットされ、最初のピンク色のパネルに戻ります。