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

初めて質問します。よろしくお願いします。

C#の質問です。

4つのラジオボタンがあり、それを選択すると、そのラジオボタンによってコンボボックスのItem(コレクション)が変化するような仕様を作成したいと考えております。


具体的に言いますと、

ラジオボタン1を選択したらコンボボックスのItem(コレクション)が1,2,3となる
ラジオボタン2を選択したらコンボボックスのItem(コレクション)が10,20,30となる
ラジオボタン3を選択したらコンボボックスのItem(コレクション)が100,200,300となる
ラジオボタン4を選択したらコンボボックスのItem(コレクション)が1000,2000,3000となる

のような感じです。

やり方が分からないので、ソースを教えてくれると凄く助かります。

よろしくお願いします。

A 回答 (1件)

いくつか方法はあるでしょうが……



 public Form1()
 {
  InitializeComponent();

  this.radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
  this.radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
  this.radioButton3.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
  this.radioButton4.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
 }

 private void radioButton_CheckedChanged(object sender, EventArgs e)
 {
  RadioButton Target = (RadioButton)sender;
  if (Target.Checked == true)
  {
   string[] Items = null;

   if (Target == this.radioButton1)
    Items = new string[] { "1", "2", "3"};
   else if(Target == this.radioButton2)
    Items = new string[] { "10", "20", "30" };
   else if (Target == this.radioButton3)
    Items = new string[] { "100", "200", "300" };
   else if (Target == this.radioButton4)
    Items = new string[] { "1000", "2000", "3000" };

   this.comboBox1.Items.Clear();
   if (Items != null)
    foreach (string Item in Items)
    this.comboBox1.Items.Add(Item);
  }
 }


こんな感じですかね?
コンストラクタで設定しているイベントハンドラの登録はデザイナ側でやっても問題ないと思います。
まぁ、コンストラクタで明示的にやっているかInitializeComponent()内部に隠蔽されるかの違いしかありませんが。
# 全角空白でインデントしているのでソースに適用するならその辺りはよろしく処理して下さいな。
    • good
    • 0
この回答へのお礼

やりたいことができました!ありがとうございました!

お礼日時:2014/01/23 17:37

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

このQ&Aを見た人はこんなQ&Aも見ています