Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 -->
 
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 -->
 
 private void button1_Click(object sender, EventArgs e)
        {
digitDisplay1.digitValue = 2;            
        }
 
Thanks in advance.....
Posted 14-Feb-13 5:56am
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Chris Reynolds (UK) at 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.
Jayanta Chatterjee at 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 at 14-Feb-13 11:29am
   
Debug
Jayanta Chatterjee at 14-Feb-13 11:32am
   
Thanks @Chris for your useful advice..
Now I'm changed digitValue public to private...
Jayanta Chatterjee at 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
0 Maciej Los 205
1 Richard MacCutchan 185
2 DamithSL 170
3 Tomas Takac 146
4 OriginalGriff 132
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 14 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