初めて質問します。よろしくお願いします。
C#の質問です。
4つのラジオボタンがあり、それを選択すると、そのラジオボタンによってコンボボックスのItem(コレクション)が変化するような仕様を作成したいと考えております。
具体的に言いますと、
ラジオボタン1を選択したらコンボボックスのItem(コレクション)が1,2,3となる
ラジオボタン2を選択したらコンボボックスのItem(コレクション)が10,20,30となる
ラジオボタン3を選択したらコンボボックスのItem(コレクション)が100,200,300となる
ラジオボタン4を選択したらコンボボックスのItem(コレクション)が1000,2000,3000となる
のような感じです。
やり方が分からないので、ソースを教えてくれると凄く助かります。
よろしくお願いします。
No.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()内部に隠蔽されるかの違いしかありませんが。
# 全角空白でインデントしているのでソースに適用するならその辺りはよろしく処理して下さいな。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
このQ&Aを見た人はこんなQ&Aも見ています
-
海外旅行から帰ってきたら、まず何を食べる?
帰国して1番食べたくなるもの、食べたくなるだろうなと思うもの、皆さんはありますか?
-
フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
あなたが普段思っている「これまだ誰も言ってなかったけど共感されるだろうな」というあるあるを教えてください
-
映画のエンドロール観る派?観ない派?
映画が終わった後、すぐに席を立って帰る方もちらほら見かけます。皆さんはエンドロールの最後まで観ていきますか?
-
海外旅行から帰ってきたら、まず何を食べる?
帰国して1番食べたくなるもの、食べたくなるだろうなと思うもの、皆さんはありますか?
-
天使と悪魔選手権
悪魔がこんなささやきをしていたら、天使のあなたはなんと言って止めますか?
-
コンボボックスでドロップダウンリストにしたときに・・・・
Visual Basic(VBA)
-
DataGridViewに空白がある場合はエラーにしたい
Visual Basic(VBA)
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
このQ&Aを見た人がよく見るQ&A
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
ORA-01858: 数値を指定する箇所...
-
配列にnullを代入すると、null...
-
BOOL値を逆にしたい
-
java DAO 日付指定フォーマット...
-
Safari ダウンロード時に日本語...
-
HashMapがおかしい
-
getStringの値がNULLの時の処理
-
Java配列でNullPointerExceptio...
-
eclipseでoracle接続時のエラー...
-
if文を通らない
-
JSPで複数のSQL実行につ...
-
postgresql接続時
-
C# ListBoxのインデックスの値...
-
ダブルクォーテーションのrepla...
-
System.err. printlnとSystem.o...
-
C言語のポインターに関する警告
-
Path型をString型へ変換する(Java)
-
ダブルクォーテーションを含む...
-
変数を動的に利用するには?
-
Log4jで機能毎に別ファイルへ出...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
ORA-01858: 数値を指定する箇所...
-
Java配列でNullPointerExceptio...
-
BOOL値を逆にしたい
-
配列にnullを代入すると、null...
-
getStringの値がNULLの時の処理
-
例外でBeanUtils.populateが、...
-
gas カレンダー祭日設定でエラー
-
C# ListBoxのインデックスの値...
-
eclipseでoracle接続時のエラー...
-
,で句切って部分一致をファイ...
-
C#でラジオボタンとコンボボッ...
-
逆コンパイルの見方について(...
-
逆コンパイルの見方について(...
-
Stringを返す getText()メソッ...
-
JSPによる画面間のパラメータの...
-
HashMapがおかしい
-
jsp/Servletの動的に増えるフォ...
-
MySQLのテーブル名を変数に置い...
-
データコンボのNullについて
-
Safari ダウンロード時に日本語...
おすすめ情報