すみません、どなたかご存知のかた教えてください。
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() { }
No.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(...); のところも合わせる必要あり )
見落としていました、たいへんお手数をおかけしてすみませんでした。
Dictionaryの使い方についても親切に説明していただき、たいへん助かりました、いろいろ応用してみます。
ありがとうございました!
No.1
- 回答日時:
設定の保存と復元については、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を設定
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# カードシャッフルのブログラムを使ってc言語でブラックジャックをしたい 2 2022/04/12 15:13
- JavaScript jsで、switch文で書かれた分をif文にできませんか。 1 2022/07/28 15:10
- JavaScript switch文のswitch(n)の部分を複数の値にするか、if文に変えてほしいです。 1 2022/07/27 17:18
- JavaScript セレクトボックスを2つ設けて選択して初めてメッセを表示 1 2022/07/27 12:15
- C言語・C++・C# C#テキストボックスの文字を配列にいれてその後表示する 4 2022/07/17 04:47
- C言語・C++・C# Cのオブジェクトファイルの逆アセンブル 5 2023/05/13 01:51
- JavaScript セレクトボックスを2つ選択してメッセージなどを表示するには。~運賃検索プログラムを完成させたい~ 1 2022/07/22 11:10
- JavaScript 以前の質問だと、どの条件でも配列が表示されてしまいます。 1 2022/07/09 11:40
- C言語・C++・C# C# DatagridviewにExcelシートを反映するとエラーが出る 2 2023/05/06 17:12
- その他(プログラミング・Web制作) python flask から fastapiへの移行時のエラー対処 1 2023/02/05 12:26
このQ&Aを見た人はこんなQ&Aも見ています
関連するカテゴリからQ&Aを探す
おすすめ情報
このQ&Aを見た人がよく見るQ&A
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
戻り値を返す関数の前に(void)...
-
【gcc・cygwin】multiple defin...
-
Notepad++の関数リスト表示の変...
-
多重定義が起きている?--lnk20...
-
ArduinoでMouse関数を使用して...
-
ドラッグしたときにマウスカー...
-
C++にてtemplateで受け取った任...
-
C# KeyDownイベントでショート...
-
void*型の配列について
-
Arduinoを使ってMIDI機器のコン...
-
int main()、void main()、void...
-
H8マイコンのシリアル通信につ...
-
typedef unsigned int UINT;が...
-
PIC12F683を用いたCapture実験
-
DLLの関数呼び出しで引数がある...
-
critical error c0000005
-
H8 3664Fのプログラム
-
VC++でGetKeyboardStateがうま...
-
gcc: incompatible pointer type
-
マルチメディアタイマーの使用方法
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
【gcc・cygwin】multiple defin...
-
int main()、void main()、void...
-
戻り値を返す関数の前に(void)...
-
既定のコンストラクタがありま...
-
Notepad++の関数リスト表示の変...
-
ArduinoでMouse関数を使用して...
-
多重定義が起きている?--lnk20...
-
C++にてtemplateで受け取った任...
-
静的でないメンバ関数の呼び出...
-
C# Controls.Addで動的に配置し...
-
const_castのつかいどころを教...
-
(void)0 はどんな意味ですか
-
C# KeyDownイベントでショート...
-
gcc: incompatible pointer type
-
C#でラジオボタンを設定に記録...
-
VC++でGetKeyboardStateがうま...
-
C言語 ① 5秒間 1秒間隔で点滅を...
-
void*型の配列について
-
GDI+の使用方法について
-
DirectInput でのエラー
おすすめ情報