Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB
0
for every question their will be four radio buttons and labels by programming itself (dynamic) we must create radio buttons and labels. is it possible to do in window form but not creating from window design(form)

C#
for (int i = 0, r = 0; i < dt.Rows.Count; i++, r++)
           {
             if (r == 0)
              {
                Label line = new Label();
                label20.MaximumSize = new Size(600, 50);
                label20.AutoSize = true;
                label20.Text += (r + 1).ToString() + " " + dt.Rows[r]                                      ["Question"].ToString() + " ";
         //#region Answer Type
         if (Convert.ToInt32(dt.Rows[r]["AnswerType"]) == 0)
         {
             checkedListBox1.Visible = false;
             radioButton1.Enabled = false;
             radioButton2.Enabled = false;
             radioButton3.Enabled = false;
             radioButton4.Enabled = false;
             if (dt.Rows[r]["Option1"].ToString() != null)
             {
                 label21.Text = dt.Rows[r]["Option1"].ToString();
             }
             if (dt.Rows[r]["Option2"].ToString() != null)
             {
                 label22.Text = dt.Rows[r]["Option2"].ToString();
             }
             if (dt.Rows[r]["Option3"].ToString() != null && dt.Rows[r]["Option3"].ToString() != "")
             {
                 label23.Text = dt.Rows[r]["Option3"].ToString();
             }
             else
             {
                 radioButton3.Visible = false;
             }
             if (dt.Rows[r]["Option4"].ToString() != null && dt.Rows[["Option4"].ToString() != "")
             {
                 label24.Text = dt.Rows[r]["Option4"].ToString();
             }
             else
             {
                 radioButton4.Visible = false;
             }
Posted

1 solution

Yes: it is possible to do.
The way I would do it is to create a UserControl which handled the question and possible answers, and then create those dynamically in a loop:
C#
Dictionary<string, List<string>> qAndA = new Dictionary<string, List<string>>()
   {
       {"1 + 1 ?", new List<string>() { "2", "1", "3", "4"}},
       {"2 + 2 ?", new List<string>() { "4", "2", "3", "1"}}
   };
int x = 10;
int y = 100;
foreach (string key in qAndA.Keys)
    {
    QAndA qa = new QAndA(key, qAndA[key]);
    qa.Location = new Point(x, y);
    Controls.Add(qa);
    y += 50;
    }

The UserControl then uses the two parameters to set the question and answers.
 
Share this answer
 
Comments
Member 10994771 4-Sep-14 8:35am    
Thanks for your reply i can't change the code is it possible to do with present code.
OriginalGriff 4-Sep-14 11:00am    
Why can't you change the code?
You wrote it, didn't you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900