Solved

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

Posted on 2010-08-18
7
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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