Solved

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

Posted on 2010-08-18
7
653 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Example works but why do I need to click twice on button 4 37
VB.NET 2008 Publish Error 2 27
.NET 2008 VB and C# 6 27
SQL Query Help Top 1 and Distinct? 6 25
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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