Flicker Problems when Drawing a Custom Radio Button

J

joshuaphillips

Hi All,

I have created a radio button that uses images instead of the silly
"circle" for a touch-screen kiosk application where users will have to
use their large fingers to select radio buttons. For some reason,
however, I can not get the damn thing to stop flickering! Would
anybody (with past experience in this) be able to look at my code and
see what they think? It would be greatly appreciated!

This is what I've got..

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

namespace System.Windows.Forms
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class KioskRadioButton : RadioButton
{
public Image ImageSelected
{
get
{
return myImageSelected;
}
set
{
myImageSelected = value;
}
}
private Image myImageSelected;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public KioskRadioButton()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.Paint += new PaintEventHandler(MyRadioButton_Paint);
}
#endregion

private void MyRadioButton_Paint(object sender, PaintEventArgs e)
{
if (this.Checked)
{
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
e.Graphics.Clear(this.BackColor);
e.Graphics.DrawString(this.Text, this.Font, new
SolidBrush(this.ForeColor), this.Image.Width + 5,
e.ClipRectangle.Height/2 - this.Font.Height/2);
e.Graphics.DrawImage(this.ImageSelected, 0,
e.ClipRectangle.Height/2 - this.ImageSelected.Height/2);
}
else
{
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
e.Graphics.Clear(this.BackColor);
e.Graphics.DrawString(this.Text, this.Font, new
SolidBrush(this.ForeColor), this.Image.Width + 5,
e.ClipRectangle.Height/2 - this.Font.Height/2);
e.Graphics.DrawImage(this.Image, 0, e.ClipRectangle.Height/2 -
this.Image.Height/2);

}
}
}
}


Thank you!!
 
S

SharpCoderMP

i'm not sure but this probably has something to do with putting your
control's painting inside other container. i didn't have time to check
your code in details but i had similar problem when i was trying to put
my own user drawn control inside panel. despite setting double buffering
it flickered too. but when i put it outside as a control droping it just
on a form with visual designer all worked well. maybe if container
doesn't want to double buffer you wont be able to force it to do so. i
didn't had time to check why this was happening. maybe someone wise will
clear this up :) but until then i'll sugest extending your code a bit
and instead inheriting from RadioButton inherit from Control and create
your control from scrach. this will give you more control over your
custom radio button. infact you have almost everything you need so this
shouldn't take too much time.
 
J

joshuaphillips

i actually figured it out. the main problem was that because i was
inheriting from Windows' RadioButton, I was actually using the base
class' OnMouseDown method. Unfortunately, inside MS's OnMouseDown, the
control "highlights" the radio button, which was causing the flicker.
So i just had to override that method and it stopped the flickering.
thank you for your help though. =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top