Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8061659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:24:41+00:00 2026-06-05T10:24:41+00:00

I’m going to post my code first since it is short and easy to

  • 0

I’m going to post my code first since it is short and easy to understand, then i’ll ask my question.

public class BatteryLabel : Control
{
    private Color _captionColor = SystemColors.Control;
    private Color _textColor = SystemColors.Info;
    private Color _failColor = Color.Red;
    private Color _passColor = Color.Green;
    private string _caption;
    string text2;
    string text3;
    bool battery1Fail = false;
    bool battery2Fail = false;
    bool battery3Fail = false;

    public BatteryLabel()
    {

    }

    public Color BackgroundTextColor
    {
        get{ return _textColor;}
        set{_textColor = value; Invalidate();}
    }

    public string Caption
    {
        get
        {
            return _caption;
        }
        set
        {
            _caption = value;
            Invalidate();
        }
    }

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

    public string Text2
    {
        get { return text2; }
        set { text2 = value; Invalidate(); }
    }

    public string Text3
    {
        get { return text3; }
        set { text3 = value; Invalidate(); }
    }

    public bool Battery1Fail
    {
        get { return battery1Fail; }
        set { battery1Fail = value; Invalidate(); }
    }

    public bool Battery2Fail
    {
        get { return battery2Fail; }
        set { battery2Fail = value; Invalidate(); }
    }

    public bool Battery3Fail
    {
        get { return battery3Fail; }
        set { battery3Fail = value; Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.DrawRectangle(Pens.Black, 0,0, Width-1, Height-1);

        var x1 = 50;
        var x2 = 98;
        var x3 = 146;
        var color1 = battery1Fail?_failColor:BackgroundTextColor;
        var color2 = battery2Fail?_failColor:BackgroundTextColor;
        var color3 = battery3Fail?_failColor:BackgroundTextColor;

        e.Graphics.FillRectangle(new SolidBrush(color1),x1+1, 1, 47, Height-2);
        e.Graphics.FillRectangle(new SolidBrush(color2),x2+1, 1, 47, Height-2);
        e.Graphics.FillRectangle(new SolidBrush(color3),x3+1, 1, 47, Height-2);

        e.Graphics.DrawLine(Pens.Black, x1,0, x1, Height-1);
        e.Graphics.DrawLine(Pens.Black, x2,0, x2, Height-1);
        e.Graphics.DrawLine(Pens.Black, x3,0, x3, Height-1);

        var BoldFont = new Font(this.Font, FontStyle.Bold);

        e.Graphics.DrawString(Caption, BoldFont, new SolidBrush(ForeColor), 0,0);
        e.Graphics.DrawString(Text, this.Font, new SolidBrush(ForeColor), x1,0);
        e.Graphics.DrawString(Text2, this.Font, new SolidBrush(ForeColor), x2,0);
        e.Graphics.DrawString(Text3, this.Font, new SolidBrush(ForeColor), x3,0);

    }   
}

The controls size is meant to be 195,14 just in case you decide to try to use it. I have 8 of these in a panel that is 200,200 running on a 1.6Ghz atom processor. It is used to display values from up to 3 batteries on a computer. The labels get refreshed every 500ms. As you may have gathered there is a little bit of flickering, but it is tolerable. I’d just like to have even less if possible. So I started looking into using Update, and moving some of my code around such as the background bit I thought maybe i should move that to OnPaintBackground(), but in a test frame that i made up the Update method does not change anything, and when I use Invalidate method it runs both OnPaintBackground and OnPaint. Here is what I tried in that case.

public class InformationLabel : Control
{
    Random r = new Random();
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        base.OnPaintBackground(e);
        Color randomCOlor = Color.FromArgb(r.Next());
        e.Graphics.FillRectangle(new SolidBrush(randomCOlor),0,0, Width-1, Height-1);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Color randomCOlor = Color.FromArgb(r.Next());
        e.Graphics.FillPie(new SolidBrush(randomCOlor),15,15,15,15, 0.0f, 120.0f);
    }
}

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    void Button1Click(object sender, EventArgs e)
    {
        informationLabel1.Update();
    }

    void Button2Click(object sender, EventArgs e)
    {
        informationLabel1.Invalidate();
    }
}

I made that one usercontrol about 300,300 so i could be sure of what i was seeing. I forgot to mention that in the battery control in my 500ms timer i just renew text, text2, and text3. I’m thinking that if the value of that text is out of spec that I’ll set the battery fail flag and then invalidate.. but i’m not sure. So how should I go about updating only the text???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:24:44+00:00Added an answer on June 5, 2026 at 10:24 am

    I believe you are looking in the wrong place to eliminate flicker. I can use your BatteryLabel to get flickerless updating of the text with basically a single line. Change your constructor to look like this:

    public BatteryLabel()
    {
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
    }
    

    This tells the control to double-buffer its graphics, which makes the flicker go away.

    To test with a 100ms refresh interval:

    Timer t;
    public Form1()
    {
        InitializeComponent();
    
        t = new Timer();
        t.Interval = 100;
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }
    
    void t_Tick(object sender, EventArgs e)
    {
        string ticks = DateTime.Now.Ticks.ToString();
        string ticks1 = ticks.Substring(ticks.Length-4),
            ticks2 = ticks.Substring(ticks.Length - 5,4),
            ticks3 = ticks.Substring(ticks.Length - 6,4);
    
        batteryLabel1.Text = ticks1;
        batteryLabel1.Text2 = ticks2;
        batteryLabel1.Text3 = ticks3;
        batteryLabel1.Battery1Fail = ticks1.StartsWith("1");
        batteryLabel1.Battery2Fail = ticks2.StartsWith("1");
        batteryLabel1.Battery3Fail = ticks3.StartsWith("1");
    }
    

    Does this help, or have I misunderstood you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.