Solved

How do I select the text in a DateTimePicker at runtime?

Posted on 2010-08-18
7
651 Views
Last Modified: 2013-12-17
I am using two c# System.Windows.Forms.DateTimePicker controls to make a date range (From and To)
When a date range is invalid I want to set the focus and highlight the first DateTimePicker control (dtpFrom.)

This is my code:
dtpFrom.Focus();
dtpFrom.Select();

the control has the Focus but the text is not highlighted.
if i use the keyboard arrow keys "to move around" in the control, then the text is highlighted.

I think if I can send the control a "arrow key" message it will work.


0
Comment
Question by:Karlus
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
Comment Utility
I think I can help you get what you're after without jumping through the "arrow keys" hoops.  What data are you after specifically?  There are a number of ways of capturing the different pieces of date/time data from a datetimepicker at runtime...it just depends on what piece (or pieces) you want.
0
 
LVL 16

Expert Comment

by:kris_per
Comment Utility

I just added datetimepicker and in a button handler added the Focus and Select methods as you showed above. when i clicked the button the date part (18 for today) gets selected (with blue background)...so it seems to be working for me....have you set any specific property for the datetimepicker...looks like something more to be checked...
0
 

Author Comment

by:Karlus
Comment Utility
I don't want the data.
I want to highlight the text in the control.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Karlus
Comment Utility
The controls on the dialog look like the older win95 controls
this includes the date time picker.

also, if I use the keyboard to enter in a new date or use the arrow keys the control selects (blue) the text every time from that point onward.
0
 
LVL 16

Expert Comment

by:kris_per
Comment Utility

Where do you call the Focus and Select methods...

Note that when the form is displayed for the first time, picker wont have the high-light unless it is the first control....blue-highlight will happen only when the datetimepicker gets the focus...when the focus is on another control (say in a textbox), then datetimepicker will lose the highlight. it wont be highlighted all the time....
0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
Comment Utility

> look like the older win95 controls

Does your code calls Application.EnableVisualStyles method when starting...this will make the controls to look xp-like...If not, try calling EnableVisualStyles in the main to see if that makes any difference...

0
 

Author Comment

by:Karlus
Comment Utility
Application.EnableVisualStyles();

worked!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now