Overriding a ComboBox

R

RSH

I tried an implementation of overriding a ComboBox control. I am simply
trying to avoid it repainting, but I can't seem to get it to work. What am I
doing 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");

}

}

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

RSH,

If you keep it from repainting, then it will not display properly. Why
do you want to prevent it from repainting?
 
R

Rocky

comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawMode = DrawMode.OwnerDrawVariable;

You must handle the Draw Item event.
 
R

RSH

Well its all part of an elaborate plot...you see if you set the Enabled
property of a combobox to false it automatically sets the background to the
standard Windows color without the possibility of setting it to anything
else. And I have a form that uses a gradient background from deep grey to
light grey and the controls look like crap with their beige color when they
are disabled so I am trying to figure out a way to prevent it from
happening. it was suggested the I override the paint method of the control.

My problem is that nothing is happening. The messageboxes are never being
triggered. If I put a contructor in place with a message box the object is
definitately getting created because the messagebox shows when the
contructor is called...but still either of the paint methods are being
called.

Thanks,
Ron


Nicholas Paldino said:
RSH,

If you keep it from repainting, then it will not display properly. Why
do you want to prevent it from repainting?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

RSH said:
I tried an implementation of overriding a ComboBox control. I am simply
trying to avoid it repainting, but I can't seem to get it to work. What
am I doing 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");

}

}

}

}
 
R

RSH

This only effects the list items that are listed, not the colors when the
control is set to Enabled = False.
 

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