Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1462
  • Last Modified:

How do I draw a line on the page of a tab control?

Here's what I've got so far...

PHASE 1: To draw a line on Form1 of a simple WinForm application, create a new WinForm project and insert the following code just after " public Form1 { InitializeComponent(); } "

protected override void OnPaint(PaintEventArgs e)
{
    //Draw diagonal green line
    Pen GreenPen = new Pen(Color.Green, 3);
    System.Drawing.Graphics formGraphics = this.CreateGraphics();
    formGraphics.DrawLine(GreenPen, 20, 20, 200, 200);
}

PHASE 2: Then drag a tab control onto the form and try to draw on either tabPage1 or tabPage2.

There is a curious property "tabControl1.GetTabRect(1)" where 0 or 1 is tabPage1 or tabPage2. However, the provided drawing rectangle is only for the "tab" itself ...and not for the rest of the tab page. Another interesting property is "tabPage1.DisplayRectangle". But nothing seems to help position a drawn line on the tab page.

So, I'm still wondering how to draw on the actual tab page itself.
0
chaffinsjc
Asked:
chaffinsjc
  • 3
  • 3
1 Solution
 
Roshan DavisCommented:
The problem in your code is, still you are taking the graphics of form1 instead of tab control.

Better way to draw on a tab control is, override the tab control class and do the painting on that class. Here are some samples.
http://www.codeproject.com/KB/cs/ColorTabControlExample.aspx
http://www.codeproject.com/KB/tabs/flattabcontrol.aspx
0
 
TribmosCommented:
System.Drawing.Graphics formGraphics = this.CreateGraphics();

Change that to..

System.Drawing.Graphics formGraphics = e.Graphics;

That way you will use the graphics object of the sender (part of the event)
0
 
chaffinsjcAuthor Commented:
In fact, the exact tab control I'm using is the 2nd item above, "A .NET Flat Tab Control (CustomDraw)" located at www codeproject com. But I still do not understand what the code is to draw a simple line on the tab pages.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chaffinsjcAuthor Commented:
The idea by Tribmos is interesting, but what is the "...graphics object of the sender (part of the event)".
What event? I only want to paint a line on the tab page. Yes, I been munging various kinds of code such as OnPaint and the DrawItem below, but I can't find anything that draws on a tab page.

For example, I set the Properties DrawItem to select the method below, =- but the line still does not show up.

       public void DrawItem(object sender, DrawItemEventArgs e)
        {  
            Rectangle rr = tabPage1.ClientRectangle;
            Point P1 = rr.Location;
            Point P2 = P1;
            P2.X += 50;
            P2.Y += 60;
            Pen pr = new Pen(Color.Red);
            System.Drawing.Graphics g = e.Graphics;
            g.DrawLine(pr, P1, P2);
        }

Actually, I don't think the DrawItem() is executing (NOT SURE WHY) because I just did a "...run to cursor" with the cursor in the code above, and it NEVER EXECUTED, yet the following line of code is in Form1.Designer.cs:
            this.flatTabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.DrawItem);

So whether the DrawItem code is right or not, I won't know until I learn what makes the event happen.
0
 
TribmosCommented:
The paint method has an event that is passed along
protected override void OnPaint(PaintEventArgs e)

e in this case contains information about the sender.  A sender is "who or what initiated the event"

Therefore when the tab page you want to draw on calls the OnPaint method you assign to it, it will perform that function.

With making things generic like this you can subscribe multiple items to the same event handler and since the control specific information is passed to the function you know where it came from.

Therefore when you create the tabpage  you will want to subscribe the OnPaint to the method that draws the green line.

The paint method for the form is incapable of drawing (well you can but its messy messy code) on the other control.
//after creation of tabpage
this.tabPage1.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintMyLine);

//elsewhere
public void PaintMyLine(object sender, PaintEventArgs e)
{
  System.Drawing.Graphics g = e.Graphics;
  g.DrawLine(Pens.Green, 20, 20, 200, 200);
}
            

Open in new window

0
 
TribmosCommented:
Additionally you can subscribe multiple tab pages to that same event and they will all get that green line.

For example if you have 3 tab pages (tabPage1, tabPage2, tabPage3).  Doing this will have the paint method draw the green line on all of them.
this.tabPage1.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintMyLine);
this.tabPage2.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintMyLine);
this.tabPage3.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintMyLine);

Open in new window

0
 
chaffinsjcAuthor Commented:
That little piece of magic "this.tabPage1.Paint..." is the thing that worked. Thank you for your detailed answer.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now