DateTimePicker获取焦点字段 [英] DateTimePicker get focused field

查看:145
本文介绍了DateTimePicker获取焦点字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定在 DateTimePicker (WinForms)应用程序中选择(突出显示)了哪个控件(天,月,年)。是否有一个属性,它指示选择了哪个控件?如果是这样,我可以通过编程方式从另一个控件中更改该控件的值吗?

I'm trying to determine which control (days, months, years) is selected (highlighted) in a DateTimePicker (WinForms) application. Is there a property, which indicates which control is selected? If so, can I programmatically change just that controls value from another control?

有没有办法获取 DateTimePicker的焦点控件

Is there a way to get the focused control of a DateTimePicker?

推荐答案

简短答案:不,不容易。

Short answer: No, not easily.

DateTimePicker本质上是SysDateTimePick32的包装,并且在ShowUpDown设置为true时,没有公开任何易于使用的属性来确定选择哪个子窗口。它甚至没有在其源代码中使用私有成员-它基本上只是将事物转发到基础com控件ala UnsafeNativeMethods.SendMessage

DateTimePicker is basically a wrapper around SysDateTimePick32 and doesn't expose any easy to use properties for determining which child window is selected when ShowUpDown is set to true. It doesn't even have private members in its source code that it uses - it's basically just forwarding things on to the underlying com control, ala UnsafeNativeMethods.SendMessage

一种方法是先发送上下然后检查哪个部分发生变化。这是一个示例。创建一个新的winforms应用程序,然后添加一个DateTimePicker,Label和Button。之后,在using语句之后,将以下代码复制到form1.cs中:

One way is to send an up then a down and check which part changes. Here's a sample. Create a new winforms application, then add a DateTimePicker, Label and Button. After that, copy the code below into your form1.cs after the using statements:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public enum DatePart
        {
            YEAR,
            MONTH,
            DAY
        }

        public DatePart part { get; set; }
        private DateTime previous { get; set; }
        private bool checkSelectedPart { get; set; }


        public Form1()
        {
            InitializeComponent();


            dateTimePicker1.ValueChanged += DateTimePicker1_ValueChanged;
            dateTimePicker1.KeyPress += DateTimePicker1_KeyPress;
            previous = dateTimePicker1.Value;

        }

        private void DateTimePicker1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (checkSelectedPart)
            {
                var dtp = sender as DateTimePicker;
                TimeSpan change = (dtp.Value - previous);
                var dayChange = Math.Abs(change.Days);
                if (dayChange == 1)
                    part = DatePart.DAY;
                else if (dayChange >= 365)
                    part = DatePart.YEAR;
                else
                    part = DatePart.MONTH;

                previous = dtp.Value;

                label1.Text = part.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkSelectedPart = true;
            dateTimePicker1.Focus();
            SendKeys.SendWait("{UP}");
            SendKeys.SendWait("{DOWN}");
            checkSelectedPart = false;
            button1.Focus();
        }
    }
}

这篇关于DateTimePicker获取焦点字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆