c# - 如何交换 NumericUpDown 控件上的箭头按钮功能?

标签 c# .net winforms numericupdown

我想交换 NumericUpDown 控件上的向上箭头和向下箭头(只是功能,不是实际的箭头)。例如,单击向上箭头时,值应减少,类似地,单击向下箭头时,值应增加。

我试图从 NumericUpDown 类继承并覆盖 UpButton()DownButton()。我认为只需交换这两种方法之间的代码就可以了。但是在将 DownButton() 代码粘贴到覆盖的 UpButton() 中时,

public class MyCustomNumericUpDown : NumericUpDown
{
    public override void UpButton()
    {
        SetNextAcceleration();
        if (base.UserEdit)
        {
            ParseEditText();
        }

        decimal num = currentValue;
        try
        {
            num -= Increment;
            if (num < minimum)
            {
                num = minimum;
                if (Spinning)
                {
                    StopAcceleration();
                }
            }
        }
        catch (OverflowException)
        {
            num = minimum;
        }

        Value = num;
    }
}

上述程序抛出以下错误。

The name 'SetNextAcceleration' does not exist in the current context
The name 'currentValue' does not exist in the current context
The name 'minimum' does not exist in the current context
The name 'Spinning' does not exist in the current context
The name 'StopAcceleration' does not exist in the current context
The name 'minimum' does not exist in the current context

我看到所有这些方法和变量在基类中都设置为私有(private)。这可能会抛出这些“不存在”的错误。

有人知道怎么做吗? 我只想交换箭头按钮的功能。例如,单击向上箭头时,值应减少,类似地,单击向下箭头时,值应增加。

最佳答案

您可以从 NumericUpDown并覆盖 UpButtonDownButton像这样的方法(通过调用 base.TheOtherMethod 来交换功能):

public class CrazyNumericUpDown : NumericUpDown
{
    public override void UpButton()
    {
        base.DownButton();
    }
    public override void DownButton()
    {
        base.UpButton();
    }
}

关于c# - 如何交换 NumericUpDown 控件上的箭头按钮功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70827662/

相关文章:

c# - 在 C# 中将大型二进制文件读入 int 数组的最快方法

c# - 带有 VS 2010 Express 的简单 Outlook 2007(或更新版本)插件/插件

c# - Visual Studio 2019 中的 "ildasm"在哪里?

c# - 在 C# Windows 窗体中的标签页选项卡标题中添加关闭按钮

c# - 使用 HttpClient 的 HTTPS 请求失败

c# - 如何处理可能需要很长时间才能启动的启动/停止API接口(interface)

c# - 带有可选参数的接口(interface)实现

.net - IClrTypeMapping 与 ClrTypeMappingDescriptor

c# - 异步/等待模型 View 演示者winforms

c# - 如何从 CheckedListBox 中获取选中项的值?