日期时间选择器作为C#Windows应用程序中的图标 [英] Datetime picker as a icon in C# Windows application

查看:85
本文介绍了日期时间选择器作为C#Windows应用程序中的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#创建一个Windows应用程序.
在我的一种形式中,只有一个类型为日期时间的字段.

我创建了一个文本框,而其中的一侧创建了一个日期时间选择器控件.但是我希望DTP变为显示为图标,当我单击该图标时,它应该打开日期时间选择器选项.

请让我知道如何设置DTP属性以显示为图标.
或者,如果没有属性,那么我将使用图标图像,但是如何在该图像的click事件上调用日期时间选择器,并且还希望用户选择DTP的值.

I am creating one windows application in C#.
In my one form there is one field of type date time.

I have created one text box and just side of that have created one date time picker control. but I want that DTP become display as icon and when i will click on that icon it should open date time picker option.

Please let me know how can i set the DTP property to display as icon.
Or if no property then i will use icon image but how can i invoke the date time picker on the click event of that image and also i want the user selected value of the DTP .

推荐答案

您将必须构建自己的控件才能执行此操作.但是,这并不难.

例如,这里是一个简单的用户控件,它可以完成您想要的操作.我在用户控件中添加了一个文本框和一个图片框.然后,将图片框图像设置为日历图像.

然后,这是使用MonthCalendar控件的代码:

You''re going to have to build your own control to do this. But, it''s not that hard.

As an example, here''s a simple user control that does what you want. I added a textbox and a picturebox to a user control. Then, I set the picturebox image to an image of a calendar.

Then, here''s the code using a MonthCalendar control:

public partial class IconDateTimePicker : UserControl
{
    MonthCalendar myCal;

    public IconDateTimePicker()
    {
        InitializeComponent();

        //set up myCal
        myCal = new MonthCalendar();
        myCal.Visible = false;

        //hook the events
        myCal.DateSelected += new DateRangeEventHandler(this.monthCalendar_DateSelected);
        myCal.Leave += new EventHandler(this.monthCalendar_Leave);
    }

    //handle when the PictureBox is clicked
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        myCal.Visible = !myCal.Visible;
        myCal.Focus();
    }

    //handle when the UserControl has been added to a form
    private void IconDateTimePicker_ParentChanged(object sender, EventArgs e)
    {
        //add mycal to the parent
        //you could also use a floating form
        this.Parent.Controls.Add(myCal);
        myCal.Left = (this.Left + this.pictureBox1.Right) - myCal.Width;
        myCal.Top = this.Bottom + 5;
    }

    private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e)
    {
        this.textBox1.Text = ((MonthCalendar)sender).SelectionStart.ToShortDateString();
        myCal.Visible = false;
    }

    private void monthCalendar_Leave(object sender, EventArgs e)
    {
        myCal.Visible = false;
    }
}



这是非常基本的,您需​​要向其添加其他内容,例如minDate,maxDate等...

但是您应该想到一种简单的方法来实现此目的.



This is very basic and you would want to add other things to it, such as minDate, maxDate, etc...

But you should get the idea of a simple way to do this.


如果我对您的理解正确,则可以始终使用它们各自的.Visible属性来切换图标和日期时间选择器的可见性.

问候
Espen Harlinn
If I understand you correctly, you can allways toggle the visiblility of the icon and the datetime picker using their respective .Visible property.

Regards
Espen Harlinn


这篇关于日期时间选择器作为C#Windows应用程序中的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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