2015-10-14 39 views
-1

我正在与vb的旧项目。 Net。,在3.5框架中创建。 有一个日期时间选择器控制。控制不适用于vb。净winforms

enter image description here

不过,现在我已经再次下降这种控制,但我没有发现这一点。用4.0控制,

今年,它有上/下箭头。

我想在点击下拉菜单时关注年份选择。 正如你所看到的2015年10月 当我选择2015年时,它会显示向下箭头以增加/减少年份。

帮我弄清楚,我怎么找到事件,它选择/集中今年控制时显示的控件。

我正在修改这个问题,清除更多所需的功能。 enter image description here

+1

你想选择年只要? –

+3

DateTimePicker的行为取决于操作系统。 –

+0

不,这个控件没有问题,但我想说的是,在这个控件中,如果我们用鼠标点击“2015”的值,用户界面会发生变化,我可以看到上下箭头来操纵年份值,我想要的,是,而不是点击,当我点击日期时间选择器控制,通过代码我强制专注于年份区域,以便用户可以看到,有一个选项也可以在几年之间导航。 – DareDevil

回答

1

你应该知道的DateTimePicker该行为取决于操作系统,如果你想只有一年选择,您可以在设计或代码做到这一点:

  • 设置FormatDateTimePickerFormat.Custom;
  • Set CustomFormat to yyyy;
  • 设置ShowUpDowntrue;

enter image description here

编辑

我们可以模拟点击年份部分程序,但它会表现不同上是不希望的不同的操作系统。

但是这里的主要思想对于这种情况可能有用。

在这段代码中,我们将光标移动到合适的位置的使用代码进行点击:

[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(); 
} 

这里是我的系统上的结果:

enter image description here

+0

您是否看到我编辑的问题? – DareDevil

+0

@DareDevil是的,我现在看到它:)你应该知道'DateTimePicker'的行为取决于操作系统,例如在Windows 8中,如果你点击年份'DateTimePicker'行为不同,并显示一个monthes列表。所以虽然我认为我们可以通过编程模拟点击年,但它在不同的操作系统上表现不同,这是不需要的。 –

+0

@DareDevil作为一个建议,当你想以一种你从未见过的方式做一份工作时,你应该再想一想。 –