Click here to Skip to main content
15,939,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 -:

C#
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... :-(
thanks in advance..
Posted
Updated 16-Feb-13 16:51pm
v4
Comments
Richard C Bishop 14-Feb-13 12:52pm    
Can you draw on it after resizing?
Sergey Alexandrovich Kryukov 14-Feb-13 13:15pm    
It need invalidation, that's all. I answered, please see.
—SA
JayantaChatterjee 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 14-Feb-13 13:15pm    
Of course. I answered, please see.
—SA

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
 
Share this answer
 
Comments
Espen Harlinn 14-Feb-13 16:27pm    
5'ed!
Sergey Alexandrovich Kryukov 14-Feb-13 16:27pm    
Thank you, Espen.
—SA
JayantaChatterjee 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 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
JayantaChatterjee 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......
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.
 
Share this answer
 
Comments
JayantaChatterjee 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 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.
JayantaChatterjee 17-Feb-13 14:08pm    
Thanks for your support,
I solved it myself, Really Its great..
Thanks Sir..
Thanks a lotttttttttttt.... :-)
Try putting
Invalidate();
after the
C#
showDigit();
.

Hopefull this will solve your problem.
 
Share this answer
 
Comments
JayantaChatterjee 15-Feb-13 9:04am    
Sir, same Problem...........
It doesn't redraw on the full area, after resize the contorl.... :-(
CS2011 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);

}
JayantaChatterjee 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)



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