Solved

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

Posted on 2010-08-18
7
658 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
Technology Partners: 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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