WinForms has a standard NumericUpDown control:

NumericUpDown

I want to get the same, but without the input field. Those. in essence, only up / down buttons are needed. It is clear that you can slap your own of two buttons and with the image of arrows, but I would like to unify with the standard one so that on different machines or when changing the user interface settings my control and standard look uniform.

What steps should be taken to achieve the desired effect?

  • UpDownButtons is responsible for drawing the buttons in this control. You can try to do something based on it. - kmv

1 answer 1

Try to inherit from the ancestor of NumericUpDown : UpDownBase and limit the size of the control to the size of the button area:

 using System.Windows.Forms; using System.Reflection; using System.Drawing; public class UpDownControl : UpDownBase { private readonly Control upDownButtons; public UpDownControl() { // 袛芯褋褌邪械屑 internal 褋胁芯泄褋褌胁芯 UpDownButtonsInternal var prop = GetType().GetProperty("UpDownButtonsInternal", BindingFlags.NonPublic | BindingFlags.Instance); if (prop != null) upDownButtons = (Control)prop.GetValue(this, null); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); int gapWidth = Width - upDownButtons.Right + 1; // 袥懈斜芯 const int gapWidth = 2; Size = new Size(upDownButtons.Width + gapWidth, upDownButtons.Height); } public override void DownButton() { } public override void UpButton() { } protected override void UpdateEditText() { } } 

Button press events will arrive in the DownButton and UpButton respectively.

  • Almost what is needed, but unfortunately almost. Cropped frame spoils the case. On the left side you can see this artifact. Well, the using pair should be added to the code for the convenience of copy-paste System.Reflection , System.Windows.Forms; . - 伪位蔚蠂慰位蠀蟿
  • @alexolut Yes, I know about the artifact. I deliberately did not complicate the code, You just need to add a couple of pixels to the width. Updated the answer. - Dmitry D.