المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : tying an event to dynamically created radio buttons [Solved]



C# Programming
02-05-2010, 11:23 PM
I have dynamically created sets of radio button in different group boxes to represent possible answers to various questions like below. Now i want after the user has selected the correct answers, and clicks the "Done" button,the application is able to know which answers where selected. I am suspecting that i need an event for this but i do not know how to make such to handle this situation. Please assist.

// Data source for questions and answers
string[] examsQuestions = { "What does OOP stand for?",
"Which control can be used in a layout for a form?",
"Which of the below languaages is not part of the .Net initiative" };
string[,] answers = { { "Object Oriented Prog",
"Only Other People",
"Oportunity Oriented Programming",
"Only Onntological Possiblities" },
{ "TableLayoutPanel",
"RowLayoutPanel",
"ColumnLayoutPanel",
"GridLayoutControl" },
{ "C#",
"J#",
"K#",
"F#" }
};
string[] correctAnswer = { "K#"};

// The dynamically created controls to receive the questions and answers:

private void LoadItems()
{


for (int i = 0; i < examsQuestions.Length; i++)
{

GroupBox gb = new GroupBox();
gb.AutoSize = true;
tbp.Controls.Add(gb); // Adding the groupbox to the main table panel added during design time.
TableLayoutPanel tbp2 = new TableLayoutPanel();// a panel for the controls inside the group box
tbp2.ColumnCount = 1; // ensure that there is only one column in the panel inside the group box

gb.Controls.Add(tbp2);// add the panel in the group box
// Make the controls (a label and four radiobuttons)i want to use ready
Label newLabel = new Label();

newLabel.Text = examsQuestions[i].ToString();
newLabel.AutoSize = true;

RadioButton rdb1 = new RadioButton();
rdb1.AutoSize = true;
rdb1.Text = answers[i, 0].ToString();

RadioButton rdb2 = new RadioButton();
rdb2.AutoSize = true;
rdb2.Text = answers[i, 1].ToString(); ;

RadioButton rdb3 = new RadioButton();
rdb3.AutoSize = true;
rdb3.Text = answers[i, 2].ToString(); ;

RadioButton rdb4 = new RadioButton();
rdb4.AutoSize = true;
rdb4.Text = answers[i, 3].ToString();
Control[] ctrl = { newLabel, rdb1, rdb2, rdb3, rdb4 };
tbp2.Controls.AddRange(ctrl);

}

}

Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.