Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to create a custom label control, which display one seven segment number.
when i set the digit value to display It does not works...
My code is -:
This is custom control code -->

C#
using System.Drawing;
using System.Windows.Forms;

namespace SevenSegmentDigit
{
    public partial class DigitDisplay : Label
    {
        Graphics grx;
        Pen roundPen;
        public DigitDisplay()
        {
            InitializeComponent();
            grx = this.CreateGraphics();
            roundPen = new Pen(Brushes.Black, 2);
            roundPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            roundPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            this.AutoSize=false;            
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            showDigit();
        }

        private void showDigit()
        {
            grx.Clear(this.BackColor);            
            switch (digitValue)
            {
                case 1:
                    drawOne();
                    break;
                case 2:
                    drawTwo();
                    break;
                case 3:
                    drawThree();
                    break;
                case 4:
                    drawFour();
                    break;
                case 5:
                    drawFive();
                    break;
                case 6:
                    drawSix();
                    break;
                case 7:
                    drawSeven();
                    break;
                case 8:
                    drawEight();
                    break;
                case 9:
                    drawNine();
                    break;
                case 0:
                    drawZero();
                    break;
            };
        }
        public 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);
        }

        private void drawThree()
        {
            grx.DrawLine(roundPen, 14, 10, 29, 10);
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            grx.DrawLine(roundPen, 14, 25, 29, 25);
            grx.DrawLine(roundPen, 30, 26, 30, 40);
            grx.DrawLine(roundPen, 14, 41, 29, 41);
        }
        private void drawFour()
        {
            grx.DrawLine(roundPen, 15, 11, 15, 24);
            grx.DrawLine(roundPen, 16, 25, 29, 25);
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            grx.DrawLine(roundPen, 30, 26, 30, 40);
        }
        private void drawFive()
        {
            grx.DrawLine(roundPen, 15, 10, 29, 10);
            grx.DrawLine(roundPen, 15, 11, 15, 24);
            grx.DrawLine(roundPen, 16, 25, 29, 25);
            grx.DrawLine(roundPen, 30, 26, 30, 40);
            grx.DrawLine(roundPen, 15, 41, 29, 41);
        }

        private void drawSix()
        {
            grx.DrawLine(roundPen, 15, 11, 15, 24);
            grx.DrawLine(roundPen, 15, 26, 15, 39);
            grx.DrawLine(roundPen, 16, 25, 29, 25);
            grx.DrawLine(roundPen, 30, 26, 30, 40);
            grx.DrawLine(roundPen, 15, 41, 29, 41);
        }
        private void drawSeven()
        {
            grx.DrawLine(roundPen, 15, 10, 29, 10);
            drawOne();
        }
        private void drawEight()
        {
            grx.DrawLine(roundPen, 15, 10, 29, 10);
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            drawSix();
        }
        private void drawNine()
        {
            grx.DrawLine(roundPen, 15, 10, 29, 10);
            grx.DrawLine(roundPen, 15, 11, 15, 24);
            grx.DrawLine(roundPen, 16, 25, 29, 25);
            drawOne();
        }
        private void drawZero()
        {
            grx.DrawLine(roundPen, 15, 11, 15, 24);
            grx.DrawLine(roundPen, 15, 26, 15, 39);
            grx.DrawLine(roundPen, 15, 41, 29, 41);
            drawSeven();
        }
       
    }
}


I draw it on my form and assign the value it does not display the number....

This is Form Code -->

C#
 private void button1_Click(object sender, EventArgs e)
        {
digitDisplay1.digitValue = 2;            
        }


Thanks in advance.....
Posted
Comments
Sergey Alexandrovich Kryukov 14-Feb-13 14:37pm    
Impossibly dirty and wasteful, unsupportable code. "case 1, case 2...". Pain to look at it. What, you don't know how to write methods, use parameters and other abstractions?
If you code in this style, nothing will work...
—SA

1 solution

Change digitDisplay1.digitValue = 2; to digitDisplay1.DisplayDigit = 2

If you use digitValue field instead of DisplayDigit property, showDigit() method is not executed.

BTW : you could have found that all by yourself if you had launched your program in debug mode and watched its execution.
 
Share this answer
 
Comments
Chris Reynolds (UK) 14-Feb-13 11:13am    
Good spot and that is why the digitValue variable should be private so that your calling code can only operate through DisplayDigit.
JayantaChatterjee 14-Feb-13 11:26am    
Sir can you tell me how to set custom label AutoSize property false in custom control??
and
when i assign the value it doesn't show the number on full area of the label, It shows only initial area(drawing size), How to fix It????
phil.o 14-Feb-13 11:29am    
Debug
JayantaChatterjee 14-Feb-13 11:32am    
Thanks @Chris for your useful advice..
Now I'm changed digitValue public to private...
JayantaChatterjee 14-Feb-13 12:19pm    
how to use resize event on custom control??

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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