【问题标题】:How to apply NumericUpDown's Acceleration feature to a regular Button如何将 NumericUpDown 的加速功能应用于常规按钮
【发布时间】:2017-11-02 14:10:02
【问题描述】:

如何将加速功能应用于常规 Button? (如NumericUpDown 控件的Acceleration 属性)。在下图中,您可以看到我有控件来操纵窗口的整体尺寸,并且我希望保持布局不变,而不是使用NumericUpDown 控件。

例如,在>>>按钮上(增量宽度1英寸)当用户按住按钮时,我希望加速相应TextBox中显示的数字。

我最初的想法是创建一个自定义Button,它继承和/或包装NumericUpDown 控件并利用后者的现有属性。但是,当按钮处于按下状态时,我仍然必须在底层 NumericUpDown 控件上触发适当的事件/属性。有任何想法吗?谢谢!

【问题讨论】:

  • 不,你不能轻易做到这一点。如果您需要有关如何自己实现此功能的灵感,请考虑从Reference Source 开始
  • 感谢您的参考-非常有用,不知道存在!

标签: vb.net winforms


【解决方案1】:

添加Timer 可能吗? VB.net:

Private mouseDownTimeElapsed As Integer = 0

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Increment the total time mouse has been depressed
    mouseDownTimeElapsed += 50
    'The following have the same effect as the NumericUpDownAcceleration objects
    If mouseDownTimeElapsed > 2000 Then
        IncrementOverallWidth(10)
    ElseIf mouseDownTimeElapsed > 1000 Then
        IncrementOverallWidth(3)
    ElseIf mouseDownTimeElapsed > 500 Then
        IncrementOverallWidth(1)
    End If
End Sub

Private Sub Button2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown
    'This check is necessary or a single click won't trigger an increment
    If mouseDownTimeElapsed = 0 Then
        IncrementOverallWidth(1)
    End If
    Timer1.Enabled = True
    Timer1.Interval = 50
    Timer1.Start()
End Sub

Private Sub Button2_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseUp
    Timer1.Stop()
    'Reset the mouse down time to zero so the process can start over on next click
    mouseDownTimeElapsed = 0
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多