私は選択したラジオボタンを受け入れる(入力キー)簡単なコードを作った。答えと一致する場合はラジオボタンのテキストをチェックしてください。しかし、このコードは余りにも冗長です。簡単にする方法はありますか?C#ラジオボタンkeydownイベント
private void btn1_KeyDown(object sender, KeyEventArgs e)
{
var row = dTable.Rows[currentRow];
var ans = row["ANSWER"].ToString();
if (btn1.Text == ans)
{
scoreAdd();
MessageBox.Show("Correct");
}
else
{
MessageBox.Show(ans);
}
currentRow++;
nextRow();
}
private void btn3_KeyDown(object sender, KeyEventArgs e)
{
var row = dTable.Rows[currentRow];
var ans = row["ANSWER"].ToString();
if (btn3.Text == ans)
{
scoreAdd();
MessageBox.Show("Correct");
}
else
{
MessageBox.Show(ans);
}
currentRow++;
nextRow();
}
private void btn4_KeyDown(object sender, KeyEventArgs e)
{
var row = dTable.Rows[currentRow];
var ans = row["ANSWER"].ToString();
if (btn4.Text == ans)
{
scoreAdd();
MessageBox.Show("Correct");
}
else
{
MessageBox.Show(ans);
}
currentRow++;
nextRow();
}
[コードレビュー](HTTPS:/を/codereview.stackexchange.com)。 – Sinatr
'bender'を' btn4.Text'の代わりに '((Button)sender).Text'というテキストを得るために、' Button'(あるいはあなたが使っているコントロールの種類)にキャストすることができます。その時点でコードの残りの部分は同じなので、すべてのボタンに1つのハンドラを使用できます。 – David