Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I created a custom label control, I try to draw lines on it.
after I draw it on form, it doesn't repaint the area after resize it...
 
My Code -:
 
using System.Drawing;
using System.Windows.Forms;
 
namespace SevenSegmentDigit
{
    public partial class DigitDisplay : Label
    {
        Graphics grx;
        Pen roundPen;
        public DigitDisplay()
        {
            InitializeComponent();            
            SetAutoSizeMode(AutoSizeMode.GrowAndShrink);
            this.ResizeRedraw = true;
            AutoSize = false;
            grx = this.CreateGraphics();
            roundPen = new Pen(Brushes.Black, 2);
            roundPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            roundPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;                       
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            showDigit();
            base.OnPaint(pe);            
        }
        protected override void OnSizeChanged(System.EventArgs e)
        {                       
            showDigit();
            Invalidate(); 
        }
        protected override void OnResize(System.EventArgs e)
        {                       
            showDigit();
            Invalidate();
        }                
        
        private void showDigit()
        {
            grx.Clear(this.BackColor);
            switch (digitValue)
            {
                case 1:
                    drawOne();
                    break;
                case 2:
                    drawTwo();
                    break;
            };
        }
        private int digitValue = 0;
        public int DisplayDigit
        {
            get { return digitValue; }
            set { digitValue = value; showDigit(); }
        }
        private void drawOne()
        {
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            grx.DrawLine(roundPen, 30, 26, 30, 40);
        }
 

        private void drawTwo()
        {
            grx.DrawLine(roundPen, 14, 10, 29, 10);
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            grx.DrawLine(roundPen, 14, 25, 29, 25);
            grx.DrawLine(roundPen, 14, 26, 14, 40);
            grx.DrawLine(roundPen, 14, 41, 29, 41);
        }
}
}
 
but it show the same result... Frown | :-(
thanks in advance..
Posted 14-Feb-13 7:51am
Edited 16-Feb-13 17:51pm
v4
Comments
richcb at 14-Feb-13 12:52pm
   
Can you draw on it after resizing?
Sergey Alexandrovich Kryukov at 14-Feb-13 13:15pm
   
It need invalidation, that's all. I answered, please see.
—SA
Jayanta Chatterjee at 14-Feb-13 12:57pm
   
I called the method to draw the line on my custom label, but it doesn't repaint all the area after resize it...
Sergey Alexandrovich Kryukov at 14-Feb-13 13:15pm
   
Of course. I answered, please see.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Call Invalidate on size changed. That will solve your problem.
 
Whenever you need re-rendering and ultimately calling you paint handler, you call one of the Invalidate methods. Pay attention that two of those method allows your to invalidate only a part of the scene (Rectangle or Region), which can improve performance if multiple invalidates is needed. Also, multiple invalidate do not cause re-rendering every time; rather, they "OR" together (make theory-set union) of invalidated point set. Not that this mechanism is the major way to develop any interactive and/or animated behavior; in such cases you usually also need to use double buffering.
 
Please also see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].
 
—SA
  Permalink  
Comments
Espen Harlinn at 14-Feb-13 16:27pm
   
5'ed!
Sergey Alexandrovich Kryukov at 14-Feb-13 16:27pm
   
Thank you, Espen.
—SA
Jayanta Chatterjee at 14-Feb-13 21:20pm
   
Sir, It doesn't work, can you tell me how override autoSize property in my custom control??
Sergey Alexandrovich Kryukov at 14-Feb-13 22:19pm
   
I see. You forgot to Invalidate in OnResize. By the way, you don't need to call base.OnResize and base.OnSizeChanged, unless you also handle related events, but you don't need to handle then either.
—SA
Jayanta Chatterjee at 15-Feb-13 1:43am
   
Sir, I make all the suggested changes in my custom control..
but same problems, It doesn't solved.... :-(
I also include my showDigit() method. I it will help U......
Sergey Alexandrovich Kryukov at 15-Feb-13 11:12am
   
I see. showDigit is incorrect. Move all rendering to OnPaint.
Look, you need to solve all the complex of the problem. Read my references answers where I explain the method. You are doing it wrong!
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you're trying to scale what you're drawing in the window when the window is resized, none of your drawing code takes into consideration the size of the drawing surface. You're drawing the exact same image at the exact same scale every time. Setting the AutoSizeMode will NOT rescale your drawing for you. You have to provide the code that will calculate the scale based on the size of the drawing surface and provide the code that will change the drawing coordinates appropriately based on the new scale values.
  Permalink  
Comments
Jayanta Chatterjee at 17-Feb-13 2:43am
   
That's good idea, but Sir I think that will be complex to me...
can you give me code of drawOne() method with calculate on drawing area(It will help me to calculate others)????
Description of drawOne() method -: I try to draw two lines, which is number one in seven segments display...
Dave Kreskowiak at 17-Feb-13 12:00pm
   
Change the control your inheriting from to a Panel. A label is a bit too much since you're doing nothing with label other than drawing on its surface. You're not using the Label functionality at all.
 
In order to do this, you just need the height and width of your control. This is easily found in the controls base.ClientRectangle.Height and base.ClientRectangle.Width properties. After that, it's just a matter of multiplying each coordinate by a scale value determined by the ratio of Height to Width.
 
You seriously want me to write code for you that multiplies two numbers?? I don't think so.
Jayanta Chatterjee at 17-Feb-13 14:08pm
   
Thanks for your support,
I solved it myself, Really Its great..
Thanks Sir..
Thanks a lotttttttttttt.... :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try putting
Invalidate();
after the
showDigit();
.
 
Hopefull this will solve your problem.
  Permalink  
Comments
Jayanta Chatterjee at 15-Feb-13 9:04am
   
Sir, same Problem...........
It doesn't redraw on the full area, after resize the contorl.... :-(
CS2011 at 16-Feb-13 2:43am
   
try this..if does not work pm me ur soulation. I will have a look
 
protected override void OnPaint(PaintEventArgs pe)
{
showDigit();//this function contained the drawing Lines.
base.OnPaint(pe);
 
}
Jayanta Chatterjee at 16-Feb-13 22:54pm
   
Sir, I added My solution in my Question....
I'm using custom control not user control..

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100