Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 5-Jan-11 7:31am
RDBurmon9.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Rahul Dhoble at 5-Jan-11 14:14pm
   
The form format is like this
 
Member's Birth Date :
 
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
    }
}
 
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 298
1 Sergey Alexandrovich Kryukov 249
2 Yogesh Kumar Tyagi 165
3 ChauhanAjay 156
4 ClimerChinna 150
0 Sergey Alexandrovich Kryukov 6,205
1 OriginalGriff 5,333
2 CPallini 2,473
3 Richard MacCutchan 1,617
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 5 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100