Solved

how can i reduce the below coding ??

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now