Solved

how can i reduce the below coding ??

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 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