Overriding a Control's OnPaint Method

R

RSH

How do I go about overriding a Control's OnPaint Method?

I would like to prevent a control's color from changing when it is disabled.

I have overridden the Form's OnPaint Method but I need to do it at the
control level.

Thanks,
Ron
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You define a new class that derive from it , ilke

class ButtonEX: Button
{
}

Advise, take a look at a very good article in opennetcf.org about
ownedDrawList a very good example
 
M

Michael H

-->
--> How do I go about overriding a Control's OnPaint Method?
-->
--> I would like to prevent a control's color from changing when it
is disabled.
-->
--> I have overridden the Form's OnPaint Method but I need to do it
at the
--> control level.
-->
--> Thanks,
--> Ron
-->



class MyCustomControl : Control
{

protected override void OnPaint(PaintEventArgs e)
{
// ...
}

protected override void OnPaintBackground(PaintEventArgs e)
{
// .. I suspect you may be more interested in
// .. override its background painting.
}

}
 
R

RSH

I tried an implementation of this idea but it never fires. Am I correctly
implementing this? Or are the overrides wrong?

Please help.

Thanks,
Ron











using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Text.RegularExpressions;

using System.Collections;

using System.Drawing.Drawing2D;

namespace GeneralTesting

{

public partial class frmMain : Form

{

MyCustomComboBox TB1 = new MyCustomComboBox();


public frmMain()

{

InitializeComponent();

TB1.FormattingEnabled = true;

TB1.Location = new System.Drawing.Point(13, 214);

TB1.Name = "TB1";

TB1.Size = new System.Drawing.Size(385, 21);

TB1.TabIndex = 1;

TB1.SelectedIndexChanged += new
System.EventHandler(this.TB1_SelectedIndexChanged_1);

this.Controls.Add(TB1);

DataClass DC = new

}

private void TB1_SelectedIndexChanged_1(object sender, EventArgs e)

{

richTextBox1.Text += ((Employee)TB1.SelectedItem).ToString() + "\n";

TB1.Items.Remove((Employee)TB1.SelectedItem);

}



private void button1_Click(object sender, EventArgs e)

{

if (TB1.Enabled == true)

{

TB1.Enabled = false;

}

else

{

TB1.Enabled = true;

}

}





class MyCustomComboBox : ComboBox

{

protected override void OnPaint(PaintEventArgs e)

{

// ...

MessageBox.Show("TRIGGERED OnPaint");

}

protected override void OnPaintBackground(PaintEventArgs e)

{

// .. I suspect you may be more interested in

// .. override its background painting.

MessageBox.Show("TRIGGERED OnPaintBackground");

}

}

}

}
 
M

Michael H

Did you ever figure this out?

I was engulfed by obsessive work demands the last two weeks.
 

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