Matt
Matt

Reputation: 3812

What is the fastest way to draw multiple custom rectangle controls on a windows form

Background

I am building a windows application (winforms) in c#.net. The main form needs to display a dashboard control which is basically a resource scheduler. Time running across the bottom. Items running on the y-axis.

Im the past I have used the ComponentGo Resource Scheduler for this type of thing, but this time I need more customisation. So Im looking to build my own.

Here is a mockup of the desired layout:

desired form layout

Questions

  1. I created my control based on a Panel, is a good idea?
  2. How can I draw the text outside of the panel boundary? I can draw directly to Parent Canvas, but that feels wrong.
  3. How can I half fill my panel with colour?
  4. How do I ensure that my redraw is smooth and fast.

Currently I basically have:

public class MyControl : Panel
{
     public MyControl()
     {
          CalcXPosition();
          this.SetBounds(this.Left, this.Top, myWidth, myHeight);
     }

     //... code omitted
}

But I'm not sure I'm going about this in the best way.... any help or comments would be greatly appreciated.

Also, I need each rectangle block to have properties associated with it. Block_ID, Block_Name etc

Upvotes: 2

Views: 1386

Answers (1)

mbeckish
mbeckish

Reputation: 10579

1) I created my control based on a Panel, is a good idea?

You could use Panel, or just Control. Another option would be to not use a control per class, and instead just draw the rectangles right on the parent canvas. Either way has its pros and cons. If you don't need a lot of control over how to draw the rectangles, then using Controls will probably be easier than drawing the rectangles manually.

2) How can I draw the text outside of the panel boundary? I can draw directly to Parent Canvas, but that feels wrong.

Option 1: Make the rectangle controls a little bigger, so that you can fit the text within the control. Make the background of each rectangle control transparent (or the same shade of gray as the parent canvas), and draw the colored rectangle in the middle of the control.

Option 2: Make the text separate Label controls and place them above the rectangle controls.

3) How can I half fill my panel with colour?

Use the control's OnPaint event to draw the colors wherever you want on the control.

4) How do I ensure that my redraw is smooth and fast?

That will depend on a lot of factors. get something up and running, and if it is too slow, post another question with more specific details.

Upvotes: 1

Related Questions