Click here to Skip to main content
15,072,422 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello,

I want to optimize my below code. so it will work fast.

tabControlEx1.TabPages[k].Controls.Clear();
                        FlowLayoutPanel flp = new FlowLayoutPanel();
                        flp.Dock = DockStyle.Fill;

                        Button btnFirstSms = new Button();
                        btnFirstSms.Text = "First";
                        btnFirstSms.Click += new EventHandler(btnFirstSms_Click);
                        btnFirstSms.Font = new Font(btnFirstSms.Font, FontStyle.Bold);
                        btnFirstSms.Height = 32;

                        Button btnPrevSms = new Button();
                        btnPrevSms.Text = "Previous";
                        btnPrevSms.Click += new EventHandler(btnPrevSms_Click);
                        btnPrevSms.Font = new Font(btnPrevSms.Font, FontStyle.Bold);
                        btnPrevSms.Height = 32;

                        Button btnNextSms = new Button();
                        btnNextSms.Text = "Next";
                        btnNextSms.Click += new EventHandler(btnNextSms_Click);
                        btnNextSms.Font = new Font(btnNextSms.Font, FontStyle.Bold);
                        btnNextSms.Height = 32;

                        Button btnLastSms = new Button();
                        btnLastSms.Text = "Last";
                        btnLastSms.Click += new EventHandler(btnLastSms_Click);
                        btnLastSms.Font = new Font(btnLastSms.Font, FontStyle.Bold);
                        btnLastSms.Height = 32;

                        lblMessageSms = new Label();
                        lblMessageSms.Dock = DockStyle.Fill;
                        lblMessageSms.Font = new Font(lblMessageSms.Font, FontStyle.Bold);
                        lblMessageSms.ForeColor = Color.Black;
                        lblMessageSms.Width = 600;

                        SplitContainer sc = new SplitContainer();
                        sc.Orientation = Orientation.Horizontal;
                        sc.Dock = DockStyle.Fill;
                        sc.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
                        sc.IsSplitterFixed = true;
                        sc.SplitterDistance = 20;

                        sc.Panel1.Controls.Add(dgSMS);
                        flp.Controls.Add(lblMessageSms);
                        flp.SetFlowBreak(lblMessageSms, true);
                        flp.Controls.Add(btnFirstSms);
                        flp.Controls.Add(btnNextSms);
                        flp.Controls.Add(btnPrevSms);
                        flp.Controls.Add(btnLastSms);
                        sc.Panel2.Controls.Add(flp);
                        
                        CalculateTotalPagesSms(DataTable);
                        if (totalpagesSms > 1)
                        {
                            btnFirstSms.Visible = true;
                            btnNextSms.Visible = true;
                            btnPrevSms.Visible = true;
                            btnLastSms.Visible = true;
                            lblMessageSms.Visible = true;
                        }
                        else
                        {
                            btnFirstSms.Visible = false;
                            btnNextSms.Visible = false;
                            btnPrevSms.Visible = false;
                            btnLastSms.Visible = false;
                            lblMessageSms.Visible = false;
                        }
                        DataGridView.DataSource = GetCurrentRecordsSms(DataTable, cpisms, PageSize);

                        tabControlEx1.TabPages[k].Controls.Add(sc);
Posted
Updated 23-Aug-12 1:52am
v2

The only place I can see for optimisation is 'GetCurrentRecordsSms' Everything else is doing something pretty basic and should be fast.
   
Comments
VIPR@T 23-Aug-12 8:09am
   
@Christian
'GetCurrentRecordsSms' its already working fast. The main problem is that if i keep paging 10 hen its working perfect. but when i do paging 20 its working slow. it will take some time in 'sc.Panel1.Controls.Add(dgSMS);' where dgSMS is DataGridview. So is that any other solution for that?
Christian Graus 23-Aug-12 8:11am
   
Why are you doing it this way ? If this is called over and over, is it just adding the same controls again ? Why not just bind the new data to your control ?
VIPR@T 23-Aug-12 8:14am
   
this tab page is created at run time. So the controls which are required in that tab page, i have to bind that also at run time. so i had done this way.
Christian Graus 23-Aug-12 8:21am
   
But this code clears the controls on the tab page. Why does it do that ?
VIPR@T 23-Aug-12 8:37am
   
no need for that. i had removed that line.
Christian Graus 23-Aug-12 8:45am
   
Well, if you create an arbitrary number of forms on the fly, then you need to build them. The only thing I can think of, is to build a spare form in the background all the time, and just use it, so it actually exists when you need it. But I think the data binding is the bit slowing you down, and you can't do that until you know the source. How long is it taking ? What is the speed we're getting and what are we hoping for ?
You can reduce your code a bit.

C#
tabControlEx1.TabPages[k].Controls.Clear();
            FlowLayoutPanel flp = new FlowLayoutPanel();
            flp.Dock = DockStyle.Fill;

            CalculateTotalPagesSms(DataTable);

            Button btnFirstSms = CreateButtons(totalpagesSms,"First");
            btnFirstSms.Click += new EventHandler(btnFirstSms_Click);

            Button btnNextSms = CreateButtons(totalpagesSms, "Next");
            btnNextSms.Click += new EventHandler(btnNextSms_Click);

            Button btnLastSms = CreateButtons(totalpagesSms, "Last");
            btnLastSms.Click += new EventHandler(btnLastSms_Click);

            Button btnPrevSms = CreateButtons(totalpagesSms, "Previous");
            btnPrevSms.Click += new EventHandler(btnPrevSms_Click);

            lblMessageSms = new Label();
            lblMessageSms.Dock = DockStyle.Fill;
            lblMessageSms.Font = new Font(lblMessageSms.Font, FontStyle.Bold);
            lblMessageSms.ForeColor = Color.Black;
            lblMessageSms.Visible = totalpagesSms > 1;
            lblMessageSms.Width = 600;

            SplitContainer sc = new SplitContainer();
            sc.Orientation = Orientation.Horizontal;
            sc.Dock = DockStyle.Fill;
            sc.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
            sc.IsSplitterFixed = true;
            sc.SplitterDistance = 20;

            sc.Panel1.Controls.Add(dgSMS);
            flp.Controls.Add(lblMessageSms);
            flp.SetFlowBreak(lblMessageSms, true);

            flp.Controls.Add(btnFirstSms);
            flp.Controls.Add(btnNextSms);
            flp.Controls.Add(btnPrevSms);
            flp.Controls.Add(btnLastSms);

            sc.Panel2.Controls.Add(flp);                      
          
            DataGridView.DataSource = GetCurrentRecordsSms(DataTable, cpisms, PageSize);

            tabControlEx1.TabPages[k].Controls.Add(sc);


C#
private Button CreateButtons(int totalPageSms, string text)
       {
           Button btn = new Button();
           btn.Text = text;
           btn.Font = new Font(btn.Font, FontStyle.Bold);
           btn.Height = 32;
           btn.Visible = totalPageSms > 1;
           return btn;
       }
   
Comments
VIPR@T 23-Aug-12 8:23am
   
Ok..
i am trying this things...
Thanks..
Christian Graus 23-Aug-12 8:44am
   
It won't run any faster, actually it adds the overhead of a method call several times. You won't notice the speed drop, but it will be there.
VIPR@T 23-Aug-12 9:05am
   
yes its same time.....
Christian Graus 23-Aug-12 9:07am
   
That's right. The code is neater, and probably worth it for that reason, the extra time is a tiny, tiny amount. But, if you did this a lot in a hard loop, this would take longer, not less time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900