Click here to Skip to main content
15,900,714 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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 .
Posted

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:

C#
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;
    }
}


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.
 
Share this answer
 
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
 
Share this answer
 
Comments
RDBurmon 5-Jan-11 14:14pm    
The form format is like this

Member's Birth Date : <text box=""> <date time="" picker="">

Here i have locked the textbox . and user must use DTP to change the value.

But when i have used dtp . It shown as a small combo box , but i want that DTP shown as small calender icon.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900