我正在使用vb的旧项目。 Net。,在3.5框架中创建。 有一个日期时间选择器控件。
但是,现在我再次放弃了这个控制,但我没有找到它。用4.0控制,
这一年,它有向上/向下箭头。
当我点击下拉菜单时,我希望专注于年份选择。 如你所见,2015年10月 当我选择2015时,它会显示向下箭头以增加/减少年份。
帮助我弄清楚,我怎样才能找到事件,当控制显示时,选择/关注今年的控制。
答案 0 :(得分:1)
您应该知道DateTimePicker
的行为取决于操作系统,如果您只想选择年份,您可以在设计师或代码中执行此操作:
Format
设为DateTimePickerFormat.Custom
; CustomFormat
设为yyyy
; ShowUpDown
设为true
; 修改强>
我们可以通过编程方式模拟年份部分,但在不同操作系统上的表现会有所不同。
但这是对这种情况可能有用的主要想法。
在此代码中,我们将光标移动到合适的位置,然后使用代码执行单击:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
public void PerformClick()
{
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
//Don't forget to attach handler to DropDown event of your `DatePicker`
private void dateTimePicker1_DropDown(object sender, EventArgs e)
{
//wait to dropdown show
System.Threading.Thread.Sleep(500);
//Calculate the point that you want to click
//Change 125 and 5 to tune the location.
var p = new Point(
this.dateTimePicker1.Location.X + 125,
this.dateTimePicker1.Location.Y + this.dateTimePicker1.Height + 5
);
Cursor.Position = this.PointToScreen(p);
PerformClick();
}
这是我系统上的结果: