Solved

how can i reduce the below coding ??

Posted on 2011-03-01
4
271 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Deserialize issue 6 26
Finding the IIS version 5 21
SSIS Start 2 21
itextsharp with c# 3 8
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

837 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