プロが教えるわが家の防犯対策術!

すみません、どなたかご存知のかた教えてください。
C#でグループボックスの上に20個くらいのラジオボタンが並んでいて、どれかをチェックした時設定として保存し、プログラムを再起動した時チェック状態を復元したいのですが、どのようにすれば良いでしょうか?
Properties.Settings.Defaultで型をSystem.Windows.Forms.RadioButtonにして(RadioButton)senderを記憶するようにしてみましたが(プログラムが分かって無いからですが)だめでした。
現在はProperties.Settings.Defaultの型をstringにして、次のような形で動かしていますが、なんとかシンプル(スマート)にしたいのです。

private void Form1_Load(object sender, EventArgs e)
{
switch (Properties.Settings.Default.SelectedProgram)
{
case "Func1":
radioButtonFunc1.Checked = true;
break;
case "Func2":
radioButtonFunc2.Checked = true;
break;
case "Func3":
radioButtonFunc3.Checked = true;
break;
default:
break;
}
}

//各ラジオボタン共通
private void radioButtonSelect_Click(object sender, EventArgs e)
{
string RadioButtonText = null;
RadioButton _RadioButton = (RadioButton)sender;

if (_RadioButton.Checked == true)
{
RadioButtonText = _RadioButton.Text;
Properties.Settings.Default.SelectedProgram = RadioButtonText;

switch (RadioButtonText)
{
case "Func1":
textBox1.Text = "a";
break;
case "Func2":
textBox1.Text = "b";
break;
case "Func3":
textBox1.Text = "c";
break;
default:
break;
}

}
}

//プログラム開始ボタン
private void buttonStart_Click(object sender, EventArgs e)
{
string RadioButtonText = null;
foreach (RadioButton prgText in groupBox1.Controls)
{
if (prgText.Checked)
{
RadioButtonText = prgText.Text;
break;
}
}

switch (RadioButtonText)
{
case "Func1":
Func1();
break;
case "Func2":
Func2();
break;
case "Func3":
Func3();
break;
default:
break;
}
}

private void Func1() { }
private void Func2() { }
private void Func3() { }

A 回答 (2件)

>textBox1.Text = prgText[current.Text];


>で「指定されたキーはディレクトリ内に存在しませんでした。」と表示されて止まってしまいます(なぜでしょう?)。

質問に掲載されていたプログラムが

switch (RadioButtonText)
{
case "Func1":
Func1();
break;
case "Func2":
Func2();
break;
case "Func3":
Func3();
break;
default:
break;

のようになっていたので、ラジオボタンのテキストが

"Func1"、"Func2"、"Func3"

になっていると仮定して

prgText.Add("Func1", "a");
prgText.Add("Func2", "b");
prgText.Add("Func3", "c");

としたのですが、ラジオボタンのテキストがこのいずれにも一致していないのではないでしょうか。



ちなみに、Dictionary の使用例として、一つ ( prgText ) はキーを String とし、もう一つの方 ( func ) はキーを RadioButton にしましたが、

private Dictionary<String, FuncDelegate> func;

のようにすることもできますし、

private Dictionary<RadioButton, String> prgText;

のようにすることもできます。
( xxx = new Dictionary<...>(); 、XXX.Add(...); のところも合わせる必要あり )
    • good
    • 0
この回答へのお礼

見落としていました、たいへんお手数をおかけしてすみませんでした。
Dictionaryの使い方についても親切に説明していただき、たいへん助かりました、いろいろ応用してみます。
ありがとうございました!

お礼日時:2012/02/06 20:37

設定の保存と復元については、RadioButton の Name を使用すると簡単になると思います。


保存の時

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
foreach( RadioButton rb in groupBox1.Controls )
{
if( rb.Checked )
{
Properties.Settings.Default.RadioButtonName = rb.Name;
Properties.Settings.Default.Save();
break;
}
}
}

のようにすると、復元時は

string radioButtonName = Properties.Settings.Default.RadioButtonName;
RadioButton rb = (RadioButton)groupBox1.Controls[radioButtonName];
if( rb != null )
{
rb.Checked = true;
}

のようにできます。

ラジオボタンクリック時の処理に Dictionary を、プログラム開始ボタンクリック時の処理に Dictionary と delegate を使った例を載せておきます。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
delegate void FuncDelegate();

private RadioButton current;
private Dictionary<string, string> prgText;
private Dictionary<RadioButton, FuncDelegate> func;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
prgText = new Dictionary<string, string>();
prgText.Add("Func1", "a");
prgText.Add("Func2", "b");
prgText.Add("Func3", "c");

func = new Dictionary<RadioButton, FuncDelegate>();
func.Add(radioButtonFunc1, new FuncDelegate(Func1));
func.Add(radioButtonFunc2, new FuncDelegate(Func2));
func.Add(radioButtonFunc3, new FuncDelegate(Func3));

string radioButtonName = Properties.Settings.Default.RadioButtonName;
current = (RadioButton)groupBox1.Controls[radioButtonName];
if (current != null)
{
current.Checked = true;
textBox1.Text = prgText[current.Text];
}
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
foreach( RadioButton rb in groupBox1.Controls )
{
if( rb.Checked )
{
Properties.Settings.Default.RadioButtonName = rb.Name;
Properties.Settings.Default.Save();
break;
}
}
}

private void radioButtonSelect_Click(object sender, EventArgs e)
{
current = (RadioButton)sender;
textBox1.Text = prgText[current.Text];
}

private void buttonStart_Click(object sender, EventArgs e)
{
func[current]();
}

private void Func1()
{
MessageBox.Show("Func1");
}

private void Func2()
{
MessageBox.Show("Func2");
}

private void Func3()
{
MessageBox.Show("Func3");
}
}
}

この回答への補足

ご回答ありがとうございました!
おかげさまで設定の復元もdelegateによる起動もうまくいきました。

解決はしたのですが、1点だけ補足(?)させてください。
42行目と63行目付近にある
textBox1.Text = prgText[current.Text];
で「指定されたキーはディレクトリ内に存在しませんでした。」と表示されて止まってしまいます(なぜでしょう?)。
これらをコメントアウトすると、正しく動作します。
ちなみに操作したのは次の通りです。

新規にForm1を開き
groupBox1, radioButtonFunc1, radioButtonFunc2, radioButtonFunc3
buttonStart
textBox1
を配置

Settings.settingsにRadioButtonName(string)を登録

Form1のFormClosedにForm1_FormClosedを設定
各ラジオボタンにradioButtonSelect_Clickを設定
buttonStartのClickにradioButtonSelect_Clickを設定

補足日時:2012/02/06 18:53
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました!
おかげさまで設定の復元もdelegateによる起動もうまくいきました。

お礼日時:2012/02/06 19:01

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!