Click here to Skip to main content
15,902,635 members
Home / Discussions / C#
   

C#

 
QuestionClickOnce Pin
Member 391904918-Jun-08 7:07
Member 391904918-Jun-08 7:07 
AnswerRe: ClickOnce Pin
Gareth H18-Jun-08 8:15
Gareth H18-Jun-08 8:15 
Question.NET Remotable Types Pin
conor2018-Jun-08 5:39
conor2018-Jun-08 5:39 
QuestionType casting Pin
indian14318-Jun-08 5:04
indian14318-Jun-08 5:04 
AnswerRe: Type casting Pin
DaveyM6918-Jun-08 5:08
professionalDaveyM6918-Jun-08 5:08 
AnswerRe: Type casting Pin
led mike18-Jun-08 5:21
led mike18-Jun-08 5:21 
AnswerRe: Type casting Pin
Pete O'Hanlon18-Jun-08 8:37
mvePete O'Hanlon18-Jun-08 8:37 
QuestionCustom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
rcaciopp18-Jun-08 5:01
rcaciopp18-Jun-08 5:01 
I'm studying for the MCTS and have just started developing my first custom control. The control seems to work perfectly when implemented in design time when dragged from the Toolbox to a test form, but when I try to implement/create the control dynamically in code, the control's overridden OnPaint method is not being accessed and the control is not being drawn when I run the program and try to create a new instance of the control in the Form's Load event.

I think I'm missing something fundamental in the control's constructor, but from what I've read about SetStyle method, using:

this.SetStyle(ControlStyles.UserPaint, true);


should have forced the control to paint itself via OnPaint -- but, it's had no effect. Using an Invalidate() call in the constructor also has no effect. Here's the code for the control as it stands now. The Control itself is essentially a label with a 'progress bar' drawn to a certain proportion of the control's width, then the base text is drawn above the bar.

Any thoughts on what changes are needed to ensure the OnPaint method is invoked when the control is instantiated/added at runtime would be appreciated!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProgressBarLabel
{
    [ToolboxBitmap(typeof(ProgressBar))]
    public partial class ProgressBarLabel : Control
    {
        // unique properties/assessors
        private float progressBarWidthRatio;
        [Category("Appearance"), 
        Description("A number between 0.0 and 1.0 representing the % width of the ProgressBar in the Label.")]
        public float ProgressBarWidthRatio
        {
            get 
            {
                return this.progressBarWidthRatio; 
            }
            set 
            {
                this.progressBarWidthRatio = value;
                if (this.progressBarWidthRatio > 1.0)
                {
                    this.progressBarWidthRatio = 1.0F;
                }
                else if (this.progressBarWidthRatio < 0)
                {
                    this.progressBarWidthRatio = 0.0F;
                }
                Refresh();
            }
        }

        private Color progressBarBackColor;
        [Category("Appearance"), Description("The BackColor of the ProgressBar within the Label.")]
        public Color ProgressBarBackColor
        {
            get 
            {
                return this.progressBarBackColor; 
            }
            set 
            {
                this.progressBarBackColor = value;
                Refresh();
            }
        }

        // updated constructor initializes unique values
        public ProgressBarLabel()
        {
            //initialize unique properties
            this.progressBarWidthRatio = 0.0F;
            this.progressBarBackColor = this.BackColor;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        // overrides
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                this.Invalidate();
                base.Text = value;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // Calling the base class OnPaint
            base.OnPaint(pe);

            // custom paint event code
            Graphics g = pe.Graphics;
            Rectangle r = pe.ClipRectangle;
            Brush pbBrush = new SolidBrush(progressBarBackColor);

            // draw progress bar
            g.FillRectangle(pbBrush, r.X, r.Y, r.Width * progressBarWidthRatio, r.Height);

            // draw border rectangle = fixed2D
            g.DrawRectangle(Pens.Black, r.X, r.Y, r.Width - 1, r.Height - 1);

            // draw second border = fixed3D
            g.DrawRectangle(Pens.WhiteSmoke, r.X + 1, r.Y + 1, r.Width - 2, r.Height - 2);

            // get size of text for positioning
            SizeF timeSize = g.MeasureString(base.Text, base.Font);

            // draw string middle-centered in control at all times
            g.DrawString(base.Text, base.Font, new SolidBrush(base.ForeColor), 
                (r.Width - timeSize.Width) / 2, (r.Height - timeSize.Height) / 2);

            // dispose of graphics object
            g.Dispose();
        }
    }
}

AnswerRe: Custom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
led mike18-Jun-08 5:26
led mike18-Jun-08 5:26 
GeneralRe: Custom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
rcaciopp18-Jun-08 6:24
rcaciopp18-Jun-08 6:24 
GeneralRe: Custom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
darkelv18-Jun-08 6:31
darkelv18-Jun-08 6:31 
GeneralRe: Custom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
rcaciopp18-Jun-08 6:48
rcaciopp18-Jun-08 6:48 
GeneralRe: Custom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
darkelv18-Jun-08 6:58
darkelv18-Jun-08 6:58 
AnswerRe: Custom Control implementation at Runtime via Code vs. Designtime via Toolbox Pin
rcaciopp18-Jun-08 7:26
rcaciopp18-Jun-08 7:26 
QuestionCulture independant Dates? Pin
Megidolaon18-Jun-08 3:59
Megidolaon18-Jun-08 3:59 
AnswerRe: Culture independant Dates? Pin
Luc Pattyn18-Jun-08 4:17
sitebuilderLuc Pattyn18-Jun-08 4:17 
AnswerRe: Culture independant Dates? Pin
PIEBALDconsult18-Jun-08 4:45
mvePIEBALDconsult18-Jun-08 4:45 
GeneralRe: Culture independant Dates? Pin
Megidolaon19-Jun-08 21:01
Megidolaon19-Jun-08 21:01 
Questionxcopy in post build event Pin
Russell Jones18-Jun-08 3:29
Russell Jones18-Jun-08 3:29 
QuestionListViewItem image overlay [modified] Pin
ajtunbridge18-Jun-08 2:59
ajtunbridge18-Jun-08 2:59 
AnswerRe: ListViewItem image overlay Pin
led mike18-Jun-08 5:24
led mike18-Jun-08 5:24 
GeneralRe: ListViewItem image overlay Pin
ajtunbridge18-Jun-08 5:48
ajtunbridge18-Jun-08 5:48 
Questionhow to check the database connected is readonly? Pin
nicolus18-Jun-08 2:46
nicolus18-Jun-08 2:46 
AnswerRe: how to check the database connected is readonly? Pin
Bert delaVega18-Jun-08 16:27
Bert delaVega18-Jun-08 16:27 
Questionfloat representation Pin
George_George18-Jun-08 2:24
George_George18-Jun-08 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.