Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo box Borderstyle? 1

Status
Not open for further replies.

ookete

Programmer
Joined
Oct 5, 2004
Messages
180
Location
US
I am new to VB.NET and I notice I cannot change the borderstyle of the basic combo boxes. I prefer a flat outline over the crummy "3D effect". I know I can purchase software bundles out there that have a ton of extra controls, but how difficult would it be for me to create a new combo box that overrides the default 3D appearance? Is the borderstyle something that I can override at all, or would I have to start from scratch and make a brand new control (yuck).

Thanks for any advice.
 
You can do this but you must inherit the ComboBox control into your own control class.

This is code in C# but it ought to give you an idea of what to do:

Code:
	private BorderStyle m_bdsBorder = BorderStyle.Fixed3D;


	//This code was created to match the implementation by the Label class
	[Category("Appearance"),
	 DefaultValue(BorderStyle.Fixed3D), 
	 Description("Determines if the label has a visible border.")]
	public BorderStyle BorderStyle {
		get {
			return m_bdsBorder;
		}
		set {
			if (Enum.IsDefined(typeof(BorderStyle), value)) {
				m_bdsBorder = value;
				this.Invalidate();
			} else {
				throw new InvalidEnumArgumentException(
					"value", (int) value, typeof(BorderStyle));
			}
		}
	}

	protected override void WndProc(ref Message msg) {
		if (this.BorderStyle == BorderStyle.Fixed3D) {
			//The border does not need to be clipped so just pass on the method call
			base.WndProc(ref msg);
		} else if (msg.Msg == 15) {
			//This code is executed before the control painting is performed
			//The painting region must be clipped to get rid of the border before
			Rectangle rctBound = this.Bounds;

			if (this.BorderStyle == BorderStyle.FixedSingle) {
				//Take off one pixel the whole way around the border
				rctBound.Offset(1 - rctBound.X, 1 - rctBound.Y);
				rctBound.Size -= new Size(2, 2);
			} else {
				//Take off two pixels the whole way around the border
				rctBound.Offset(2 - rctBound.X, 2 - rctBound.Y);
				rctBound.Size -= new Size(3, 3);
			}

			//The ComboBox painting area is clipped to get rid of the 3D border
			this.Region = new Region(rctBound);

			base.WndProc(ref msg);

			if (this.BorderStyle == BorderStyle.FixedSingle) {
				//Draw the single line border after the call to the painting methods
				//An additional pixel must be trimmed off of the border size to accomodate
				//the way that the painting methods perform
				rctBound.Size -= new Size(1, 1);
				this.CreateGraphics().DrawRectangle(new Pen(Color.Black), rctBound);
			}
		} else {
			//Pass on all other messages
			base.WndProc(ref msg);
		}
	}

You only need one additional property but you must override WndProc because the ComboBox control does not use the Painting methods to draw itself.

Note that the size and location of the control will be a little off from the others. You may need to make it OwnerDraw so that it becomes sizable and tweak it using the property window.

It also will not draw correctly all the time in the IDE but will look fine at runtime.
 
Wow, good stuff. I will have to translate it to VB, but I think I get what's going on.

Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top