Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how can i reduce the below coding ??

Posted on 2011-03-01
4
Medium Priority
?
277 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

782 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