Create chart
-
I'm doing this coding and its running properly. This coding is to create a chart that can be drag and drop. I create the Panel as the bar. The panel will be generated depend on input. But the problem is the Panel is generated too many as it like there is nonstop loop. Can anyone solve this problem...plz using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace CMSMS { public class mypanel:System.Windows.Forms.Panel { private bool isDragging=false; public int x,y; protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown (e); isDragging = true; x = e.X; y = e.Y; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp (e); isDragging = false; } protected override void OnMouseMove(MouseEventArgs e) { if (isDragging == true) { this.Left= e.X + this.Left - x; this.Top = e.Y + this.Top - y; } } } } using System; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CMSMS { /// /// Summary description for GranttChart. /// public class GranttChart : System.Windows.Forms.Form { ... private mypanel [] bar = new mypanel[60]; ... public void GenerateGranttChart() { GenerateChart(); } private void GenerateChart() { for (int i = 0; i < 10; i++) { int xPosition = 100; for (index = 0; index < 10; index++) { for (int j = 0; j < 10; j++) { DrawPanel( xPosition, yPosition,j,brush, length); xPosition += length; } yPosition -= range; } } } private void DrawPanel(int X,int Y,int j,SolidBrush brush, int l) { bar[j]=new mypanel(); this.bar[j].Size = new Size(l,20); this.bar[j].BackColor = brush.Color; this.bar[j].Location = new Point(X,Y); } rotected override void OnPaint(PaintEventArgs e) { ... GenerateChart(); ... }
-
I'm doing this coding and its running properly. This coding is to create a chart that can be drag and drop. I create the Panel as the bar. The panel will be generated depend on input. But the problem is the Panel is generated too many as it like there is nonstop loop. Can anyone solve this problem...plz using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace CMSMS { public class mypanel:System.Windows.Forms.Panel { private bool isDragging=false; public int x,y; protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown (e); isDragging = true; x = e.X; y = e.Y; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp (e); isDragging = false; } protected override void OnMouseMove(MouseEventArgs e) { if (isDragging == true) { this.Left= e.X + this.Left - x; this.Top = e.Y + this.Top - y; } } } } using System; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CMSMS { /// /// Summary description for GranttChart. /// public class GranttChart : System.Windows.Forms.Form { ... private mypanel [] bar = new mypanel[60]; ... public void GenerateGranttChart() { GenerateChart(); } private void GenerateChart() { for (int i = 0; i < 10; i++) { int xPosition = 100; for (index = 0; index < 10; index++) { for (int j = 0; j < 10; j++) { DrawPanel( xPosition, yPosition,j,brush, length); xPosition += length; } yPosition -= range; } } } private void DrawPanel(int X,int Y,int j,SolidBrush brush, int l) { bar[j]=new mypanel(); this.bar[j].Size = new Size(l,20); this.bar[j].BackColor = brush.Color; this.bar[j].Location = new Point(X,Y); } rotected override void OnPaint(PaintEventArgs e) { ... GenerateChart(); ... }
First thing that I see is that you have 1000 iterations of DrawPanel. You have three nested loops, so you get the following logic:
for each iteration of the outermost loop
for each iteration of the next loop
for each iteration of the inner loop
DrawPanel
end for
end for
end forBTW - you have declared an array with a fixed size of 60, so you will get an overflow. You would be much better off declaring this as
List<mypanel> bar = new List<mypanel>
and then using this instead. -- modified at 8:00 Friday 16th March, 2007Deja View - the feeling that you've seen this post before.