Solved

how can i reduce the below coding ??

Posted on 2011-03-01
4
272 Views
Last Modified: 2012-06-21
in my application i generate tab pages dynamically but my coding is too long ...

how can i reduce the below coding ??

//dynamically generate tab pages .....
                tbQuestionBank.SuspendLayout();
                for (int i = 0; i < objGeneral.TotalQuestions; i++)
                {
                    //string str1 = "Question11";
                    TabPage tabPage = new TabPage("Question" + Convert.ToString(i + 1));
                    tbQuestionBank.TabPages.Add(tabPage);
                    Panel panel = new Panel();
                    panel.Dock = DockStyle.Fill;

                    //add number lable
                    Label lblnumber = new Label();
                    lblnumber.Text = (i + 1).ToString();
                    lblnumber.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    lblnumber.ForeColor = System.Drawing.Color.Black;
                    lblnumber.Location = new Point(6, 24);
                    lblnumber.Size = new Size(24, 20);

                    //add question lable
                    Label lblRandomQuestion11 = new Label();
                    lblRandomQuestion11.Location = new Point(29, 20);
                    lblRandomQuestion11.Size = new Size(411, 45);
                    lblRandomQuestion11.Text = General.Questions[i].ToString();
                    lblRandomQuestion11.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    lblRandomQuestion11.ForeColor = System.Drawing.Color.Black;
                    //creating radio buttons

                    //Radio button 1
                    RadioButton rdbOption11a = new RadioButton();
                    rdbOption11a.Location = new Point(30, 65);
                    rdbOption11a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption11a.Text = "a";
                    rdbOption11a.Size = new Size(40, 24);

                    //Radio button 2
                    RadioButton rdbOption12a = new RadioButton();
                    rdbOption12a.Location = new Point(30, 95);
                    rdbOption12a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption12a.Text = "b";
                    rdbOption12a.Size = new Size(40, 24);

                    //Radio button 3
                    RadioButton rdbOption13a = new RadioButton();
                    rdbOption13a.Location = new Point(30, 125);
                    rdbOption13a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption13a.Text = "c";
                    rdbOption13a.Size = new Size(40, 24);

                    //Radio button 4
                    RadioButton rdbOption14a = new RadioButton();
                    rdbOption14a.Location = new Point(30, 155);
                    rdbOption14a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    //rdbOption11a.Text = char.ConvertFromUtf32(97 + i).ToString();
                    rdbOption14a.Text = "d";
                    rdbOption14a.Size = new Size(40, 24);


                    //creating lable options ...

                    //option 1 lable
                    Label lblOption11a = new Label();
                    lblOption11a.Location = new Point(70, 70);
                    lblOption11a.Size = new Size(350, 20);
                    lblOption11a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);



                    //option 2 lable
                    Label lblOption12a = new Label();
                    lblOption12a.Location = new Point(70, 100);
                    lblOption12a.Size = new Size(350, 20);
                    lblOption12a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);


                    //option 3 lable
                    Label lblOption13a = new Label();
                    lblOption13a.Location = new Point(70, 130);
                    lblOption13a.Size = new Size(350, 20);
                    lblOption13a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);


                    //option 4 lable
                    Label lblOption14a = new Label();
                    lblOption14a.Location = new Point(70, 160);
                    lblOption14a.Size = new Size(350, 20);
                    lblOption14a.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);


                    if (i == 0)
                    {
                        lblOption11a.Text = General.QuestionOptions[0].ToString();
                        lblOption12a.Text = General.QuestionOptions[1].ToString();
                        lblOption13a.Text = General.QuestionOptions[2].ToString();
                        lblOption14a.Text = General.QuestionOptions[3].ToString();
                    }
                    else if (i == 1)
                    {
                        lblOption11a.Text = General.QuestionOptions[4].ToString();
                        lblOption12a.Text = General.QuestionOptions[5].ToString();
                        lblOption13a.Text = General.QuestionOptions[6].ToString();
                        lblOption14a.Text = General.QuestionOptions[7].ToString();
                    }

                    else if (i == 2)
                    {
                        lblOption11a.Text = General.QuestionOptions[8].ToString();
                        lblOption12a.Text = General.QuestionOptions[9].ToString();
                        lblOption13a.Text = General.QuestionOptions[10].ToString();
                        lblOption14a.Text = General.QuestionOptions[11].ToString();
                    }

                    panel.Controls.Add(lblnumber);
                    panel.Controls.Add(lblRandomQuestion11);
                    panel.Controls.Add(rdbOption11a);
                    panel.Controls.Add(rdbOption12a);
                    panel.Controls.Add(rdbOption13a);
                    panel.Controls.Add(rdbOption14a);
                    panel.Controls.Add(lblOption11a);
                    panel.Controls.Add(lblOption12a);
                    panel.Controls.Add(lblOption13a);
                    panel.Controls.Add(lblOption14a);
                    tabPage.Controls.Add(panel);

                }
                tbQuestionBank.ResumeLayout();

Open in new window


using for loop how can i reduce the above coding ??
0
Comment
Question by:Parth48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35005717
You can create your labels and radiobuttons dynamic in a loop
//dynamically generate tab pages .....
                tbQuestionBank.SuspendLayout();
                for (int i = 0; i < objGeneral.TotalQuestions; i++)
                {
                    //string str1 = "Question11";
                    TabPage tabPage = new TabPage("Question" + Convert.ToString(i + 1));
                    tbQuestionBank.TabPages.Add(tabPage);
                    Panel panel = new Panel();
                    panel.Dock = DockStyle.Fill;

                    //add number lable
                    Label lblnumber = new Label();
                    lblnumber.Text = (i + 1).ToString();
                    lblnumber.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    lblnumber.ForeColor = System.Drawing.Color.Black;
                    lblnumber.Location = new Point(6, 24);
                    lblnumber.Size = new Size(24, 20);

                    //add question lable
                    Label lblRandomQuestion11 = new Label();
                    lblRandomQuestion11.Location = new Point(29, 20);
                    lblRandomQuestion11.Size = new Size(411, 45);
                    lblRandomQuestion11.Text = General.Questions[i].ToString();
                    lblRandomQuestion11.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    lblRandomQuestion11.ForeColor = System.Drawing.Color.Black;


                  //creating radio buttons

                for (int j = 0;j<4;j++)
                {
                    RadioButton rbn = new RadioButton();
                    rbn.Name = "rdbOption1" + (j+1).ToString() + ((char)(90+j)).ToString();
                    rbn.Text = ((char)(90+j)).ToString();
                    rbn.Size = new Size(40, 24);
                    rbn.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                    rbn.Location = new Point(30, 65 + (j*30));
                    panel.Controls.Add(rbn);

                    Label lbl = new Label();
                    lbl.Name = "lblOption1" +  (j+1).ToString() + ((char)(90+j)).ToString();
                    lbl.Location = new Point(70, 70 + (j*30));
                    lbl.Size = new Size(350, 20);
                    lbl.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point);
                     panel.Controls.Add(lbl);
                }


                    if (i == 0)
                    {
                        lblOption11a.Text = General.QuestionOptions[0].ToString();
                        lblOption12a.Text = General.QuestionOptions[1].ToString();
                        lblOption13a.Text = General.QuestionOptions[2].ToString();
                        lblOption14a.Text = General.QuestionOptions[3].ToString();
                    }
                    else if (i == 1)
                    {
                        lblOption11a.Text = General.QuestionOptions[4].ToString();
                        lblOption12a.Text = General.QuestionOptions[5].ToString();
                        lblOption13a.Text = General.QuestionOptions[6].ToString();
                        lblOption14a.Text = General.QuestionOptions[7].ToString();
                    }

                    else if (i == 2)
                    {
                        lblOption11a.Text = General.QuestionOptions[8].ToString();
                        lblOption12a.Text = General.QuestionOptions[9].ToString();
                        lblOption13a.Text = General.QuestionOptions[10].ToString();
                        lblOption14a.Text = General.QuestionOptions[11].ToString();
                    }

                    tabPage.Controls.Add(panel);

                }
                tbQuestionBank.ResumeLayout();

Open in new window

0
 

Author Comment

by:Parth48
ID: 35005816
if (i == 0)
                    {
                        lblOption11a.Text = General.QuestionOptions[0].ToString();
                        lblOption12a.Text = General.QuestionOptions[1].ToString();
                        lblOption13a.Text = General.QuestionOptions[2].ToString();
                        lblOption14a.Text = General.QuestionOptions[3].ToString();
                    }
                    else if (i == 1)
                    {
                        lblOption11a.Text = General.QuestionOptions[4].ToString();
                        lblOption12a.Text = General.QuestionOptions[5].ToString();
                        lblOption13a.Text = General.QuestionOptions[6].ToString();
                        lblOption14a.Text = General.QuestionOptions[7].ToString();
                    }

                    else if (i == 2)
                    {
                        lblOption11a.Text = General.QuestionOptions[8].ToString();
                        lblOption12a.Text = General.QuestionOptions[9].ToString();
                        lblOption13a.Text = General.QuestionOptions[10].ToString();
                        lblOption14a.Text = General.QuestionOptions[11].ToString();
                    }

Open in new window


error in the above code ....
what can i do now ??
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35005995
what is the Error ??
0
 

Author Comment

by:Parth48
ID: 35015792
hi @Dhaest: Thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question