How to "hide" NumericUpDown arrow controls.

J

jtfeck

If you really wanted a NumericUpDown control without the arrow keys,
take it one step further:

In Form_Load():

//////////////////////////////////////////////////////////////////////////
// Local variables.
int nArrowKeyWidth = myNumericUpDown.Controls[0].Width;

// 1) Set the border style to none. Otherwise a cut off portion can be
seen.
myNumericUpDown.BorderStyle = BorderStyle.None;

// 2) Clip a new region which the user will use.
myNumericUpDown.Region = new System.Drawing.Region(new
System.Drawing.Rectangle(0, 0, myNumericUpDown.Width - nArrowKeyWidth
- 1, myNumericUpDown.Height));

// 3) Add an offset to the controls so that it will line up properly.
(Optional)
myNumericUpDown.Location = new System.Drawing.Point
(myNumericUpDown.Location.X + nArrowKeyWidth - 1,

myNumericUpDown.Location.Y);

// 4) Make a copy of the client rectangle for rendering a new border.
(Optional, only if you want the border back.)
m_CopyOfRectangle = new System.Drawing.Rectangle
(myNumericUpDown.Location.X - 2,

myNumericUpDown.Location.Y - 2,

myNumericUpDown.ClientRectangle.Width + 2 - nArrowKeyWidth,

myNumericUpDown.ClientRectangle.Height + 2);
// 5) Hide the arrows from being used.
myNumericUpDown.Controls[0].Hide();

// Store the graphics handle for rendering the border.
m_CopyOfGraphics = myForm.CreateGraphics();
//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// Override myNumericUpDown Paint Event to render the border.
private void myNumericUpDown_Paint(object sender, PaintEventArgs e)
{
m_CopyOfGraphics.DrawRectangle(System.Drawing.Pens.CadetBlue,
m_CopyOfRectangle);
}
//
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// Clean up!
private void Dispose()
{
m_CopyOfGraphics.Dispose();
}
//
//////////////////////////////////////////////////////////////////////////

Pros:
1) Less code to write.
2) No overriding key events.
3) No converting non-numeric keys.

Cons:
1) Requires storing a rectangle and/or graphics device for rendering a
new border.
2) A Marked Text Box, using only int values, would be easier.
3) Other programmers will think you're crazy using this method.

Hope this helps!
 

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