English 中文(简体)
我怎样才能找回 日期时间捕猎者的下降状态?
原标题:How can I retrieve the dropdown state of a DateTimePicker?

我需要确定日历下调目前是否在 WinForms DateTimePicker 中显示。 我有一个从 DateTimePicker 继承的自定义控制, 我将处理 KeyDown 事件, 以便使用导航键进行操作, 但是如果日历下调打开, 我愿意绕过该代码, 以便用户可以使用他们的导航键 。

在 ComboBox 控制下, 很容易使用 < code>. DroppedDown 属性来检查是否打开, 但 DateTimePicker 没有这样的属性 。

我目前正在做以下工作:

Private _isDroppedDown As Boolean = False

Private Sub MyDateTimePicker_CloseUp(sender As Object, e As EventArgs) Handles Me.CloseUp
    _isDroppedDown = False
End Sub

Private Sub MyDateTimePicker_DropDown(sender As Object, e As EventArgs) Handles Me.DropDown
    _isDroppedDown = True
End Sub

但是,我想知道,有比手动用变量跟踪更好的方法 来获得被丢弃的控制状态吗?

最佳回答

您所做的对一次性事件来说是好的, 但如果您的形式/ 类包含多个控件, 用变量跟踪它们 all 可能会变得不易操作, 并且难以跟踪 。

简单的替代方法是使用控件 s < code>. Tag 属性记录变量状态并测试该状态。 但是,一个更好的方法是创建您自己继承控件的类别, 并添加您想要的属性, 大致使用您已有的相同代码 。 因此, 在您的情况下, 您会添加一个名为“ MyDateTimePicker” 的类别 。

Public Class MyDateTimePicker
    Inherits DateTimePicker

    Dim _isDroppedDown As Boolean = False

    Public Property IsDroppedDown() As Boolean
        Get
            IsDroppedDown = _isDroppedDown
        End Get
        Set(value As Boolean)
            _isDroppedDown = value
        End Set
    End Property

    Private Sub MyDateTimePicker_CloseUp(sender As Object, e As System.EventArgs) Handles Me.CloseUp
        _isDroppedDown = False
    End Sub

    Private Sub MyDateTimePicker_DropDown(sender As Object, e As System.EventArgs) Handles Me.DropDown
        _isDroppedDown = True
    End Sub

End Class

下次构建后, 新的 MyDateTimePicker 类应该在工程组件标签下的工具框中显示。 它将包含与 DateTimePickers 相关的所有通常事件、 方法和属性, 加上您新的 < code>. IsDroppedDown 属性 。

哦,如果这是你经常使用的东西, 你可以把它建成一个新的类图书馆, 并且简单地把它包含在您的项目中, DLL 。

问题回答

实现这一点似乎没有更好的办法;目前的守则很好。

(只是张贴这个答案来结束问题。我从此不再试图在 DTP 类上发展。 )





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.