C#でラジオボタンの複数のグループボックスのチェック状態を全て取得したいです。
C#で複数のグループボックスに含まれるラジオボタンのチェック状態を全て取得したいです。
以下のプログラムの実行ファイルを実行すると、一つ目のグループボックスのラジオボタンしか出力されず、以降のグループボックスはデフォルト値が返ってきます。
C#超初心者で原因が分からず困っております。原因が分かる方教えていただきたいです。
該当のコード
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;
using System.IO;
namespace survey
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void groupBox3_Enter(object sender, EventArgs e)
{
}
private void groupBox4_Enter(object sender, EventArgs e)
{
}
private void groupBox6_Enter(object sender, EventArgs e)
{
}
private void groupBox8_Enter(object sender, EventArgs e)
{
}
private void groupBox10_Enter(object sender, EventArgs e)
{
}
private void groupBox11_Enter(object sender, EventArgs e)
{
}
private void groupBox12_Enter(object sender, EventArgs e)
{
}
private void groupBox13_Enter(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// 指定した複数のグループ内のラジオボタンでチェックされている物のTextを全部取り出す
var RadioButtonCheckedAll_InGroups = new[] { groupBox3, groupBox4, groupBox6, groupBox8, groupBox10, groupBox11, groupBox12, groupBox13, }
.SelectMany(g => g.Controls.OfType<RadioButton>()
.Where(r => r.Checked))
.Select(b => b.Text); // RadioButtonコントロールのTextプロパティだけ抜き出して配列化
// 結果
string tempString = String.Join(",", RadioButtonCheckedAll_InGroups); // 1行のCSVに変換
if (string.IsNullOrWhiteSpace(tempString))
{
return;
}
// ファイルを書き込みモード(追加)で開く
StreamWriter file = new StreamWriter("survey.csv", true, Encoding.UTF8);
// ファイルに書き込む
file.WriteLine(tempString);
// ファイルを閉じる
file.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 質問ダイアログを表示する
DialogResult result = MessageBox.Show("ウィンドウを閉じますか?", "質問", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
// はいボタンをクリックしたときはウィンドウを閉じる
e.Cancel = true;
}
}
private void groupBox12_Enter_1(object sender, EventArgs e)
{
}
private void groupBox6_Enter_1(object sender, EventArgs e)
{
}
private void groupBox1_Enter_1(object sender, EventArgs e)
{
}
private void groupBox8_Enter_1(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void radioButton58_CheckedChanged(object sender, EventArgs e)
{
}
private void groupBox11_Enter_1(object sender, EventArgs e)
{
}
private void radioButton45_CheckedChanged(object sender, EventArgs e)
{
}
}
}
該当のコード(追記)
namespace survey
{
partial class Form1
{
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
/// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows フォーム デザイナーで生成されたコード
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.radioButton57 = new System.Windows.Forms.RadioButton();
this.radioButton56 = new System.Windows.Forms.RadioButton();
this.radioButton55 = new System.Windows.Forms.RadioButton();
this.groupBox11 = new System.Windows.Forms.GroupBox();
this.radioButton58 = new System.Windows.Forms.RadioButton();
this.radioButton59 = new System.Windows.Forms.RadioButton();
this.radioButton60 = new System.Windows.Forms.RadioButton();
this.groupBox12 = new System.Windows.Forms.GroupBox();
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("メイリオ", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.label1.Location = new System.Drawing.Point(21, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(250, 24);
this.label1.TabIndex = 1;
this.label1.Text = "アンケートにご協力お願いします";
this.label1.Click += new System.EventHandler(this.label1_Click_1);
//
// radioButton57
//
this.radioButton57.AutoSize = true;
this.radioButton57.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
this.radioButton57.Font = new System.Drawing.Font("メイリオ", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.radioButton57.Location = new System.Drawing.Point(967, 36);
this.radioButton57.Name = "radioButton57";
this.radioButton57.Size = new System.Drawing.Size(34, 37);
this.radioButton57.TabIndex = 8;
this.radioButton57.Text = "4時";
this.radioButton57.UseVisualStyleBackColor = true;
//
// radioButton56
//
this.radioButton56.AutoSize = true;
this.radioButton56.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
this.radioButton56.Font = new System.Drawing.Font("メイリオ", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.radioButton56.Location = new System.Drawing.Point(855, 36);
this.radioButton56.Name = "radioButton56";
this.radioButton56.Size = new System.Drawing.Size(34, 37);
this.radioButton56.TabIndex = 7;
this.radioButton56.Text = "3時";
this.radioButton56.UseVisualStyleBackColor = true;
//
//
// groupBox11
//
this.groupBox11.Controls.Add(this.radioButton58);
this.groupBox11.Controls.Add(this.radioButton59);
this.groupBox11.Controls.Add(this.radioButton60);
this.groupBox11.Font = new System.Drawing.Font("メイリオ", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.groupBox11.Location = new System.Drawing.Point(36, 526);
this.groupBox11.Name = "groupBox11";
this.groupBox11.Size = new System.Drawing.Size(1034, 85);
this.groupBox11.TabIndex = 9;
this.groupBox11.TabStop = false;
this.groupBox11.Text = "Q3.起床時間に近い時刻を回答してください";
this.groupBox11.Enter += new System.EventHandler(this.groupBox11_Enter_1);
//
// radioButton58
//
this.radioButton58.AutoSize = true;
this.radioButton58.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
this.radioButton58.Font = new System.Drawing.Font("メイリオ", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.radioButton58.Location = new System.Drawing.Point(966, 27);
this.radioButton58.Name = "radioButton58";
this.radioButton58.Size = new System.Drawing.Size(34, 37);
this.radioButton58.TabIndex = 8;
this.radioButton58.Text = "9時";
this.radioButton58.UseVisualStyleBackColor = true;
this.radioButton58.CheckedChanged += new System.EventHandler(this.radioButton58_CheckedChanged);
//
// radioButton59
//
this.radioButton59.AutoSize = true;
this.radioButton59.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
this.radioButton59.Font = new System.Drawing.Font("メイリオ", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.radioButton59.Location = new System.Drawing.Point(811, 27);
this.radioButton59.Name = "radioButton59";
this.radioButton59.Size = new System.Drawing.Size(34, 37);
this.radioButton59.TabIndex = 7;
this.radioButton59.Text = "8時";
this.radioButton59.UseVisualStyleBackColor = true;
//
// radioButton60
//
this.radioButton60.AutoSize = true;
this.radioButton60.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
this.radioButton60.Font = new System.Drawing.Font("メイリオ", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.radioButton60.Location = new System.Drawing.Point(656, 27);
this.radioButton60.Name = "radioButton60";
this.radioButton60.Size = new System.Drawing.Size(34, 37);
this.radioButton60.TabIndex = 6;
this.radioButton60.Text = "7時";
this.radioButton60.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1088, 813);
this.Controls.Add(this.groupBox13);
this.Controls.Add(this.groupBox12);
this.Controls.Add(this.groupBox11);
this.Controls.Add(this.groupBox10);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox11.ResumeLayout(false);
this.groupBox11.PerformLayout();
this.groupBox12.ResumeLayout(false);
this.groupBox12.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RadioButton radioButton57;
private System.Windows.Forms.RadioButton radioButton56;
private System.Windows.Forms.RadioButton radioButton55;
private System.Windows.Forms.GroupBox groupBox11;
private System.Windows.Forms.RadioButton radioButton58;
private System.Windows.Forms.RadioButton radioButton59;
private System.Windows.Forms.RadioButton radioButton60;
}
}