Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-18
7
Medium Priority
?
666 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
ID: 33467755
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
ID: 33467784

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
ID: 33467796
I don't want the data.
I want to highlight the text in the control.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Karlus
ID: 33467832
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
ID: 33467956

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 2000 total points
ID: 33468151

> 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
ID: 33468247
Application.EnableVisualStyles();

worked!
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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