• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1920
  • Last Modified:

dateTimePicker - remove highlight

I am using a dateTimePicker control in a windows app using c#. I am only interested in getting & passing on the month & year, so I have enabled the up/down buttons, and I use a custom format to display the month & year (MMMM  '-' yyyy). It works great functionally - does exactly what I want. However, when it displays the month and year (like this:   March - 2011) it piuts a blue background (highlight) around the month name. Although it works perfectly, it looks very odd. Clicking or double-clicking on the highlighted month name has no effect - the contents are read-only. There are a number of properties associated with color for the control (CalendarForeColor, CalendarMonthBackground, etc) but they have no effect - probably because I'm actually disabling the calendar part. I have also tried setting them in code, again, with no effect.

Not a huge deal, the thing still works, after all, but it would look a lot better if I could disable or control this background highlighting.
0
bassman592
Asked:
bassman592
  • 5
  • 4
1 Solution
 
kdwoodCommented:

Sounds like the blue highlight on the month is due to the DateTimePicker having focus.   Try adding this to your DateTimePicker ValueChanged event:

Me.Focus()

Regards,

Keith
0
 
bassman592Author Commented:
I added this:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            Me.Focus();
            btnExportToExcel.Enabled = true;
            btnExportToPowerPoint.Enabled = true;
        }

But it gives me the error:
"The name 'Me' does not exist in the current context"

Also, the highlight appears as soon as the form is displayed, before the user selects anything.
0
 
kdwoodCommented:

Sorry, I'm not a C# coder, but I believe you need to use:

this.Focus() instead of Me.Focus

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
bassman592Author Commented:
good catch!

Well, it doesn't complain, but it also doesn't remove the highlight - either at the start, or after I click the up/down arrows for different months.

Does it do this in VB?
0
 
kdwoodCommented:
Yes, it worked in VB.  I'll try in C# a little later.  On the road now.
0
 
kdwoodCommented:
bassman,

Very interesting!   The reason why it worked for me was because I did not have the ShowUpDown property set to True on the DateTimePicker.   As soon as I set that to True, my suggestion above no longer worked.

The only work around that I can find, which is a bit of a bandaid, is to put a blank label on your form and set focus to it in the ValueChangedEvent.

I not a fan of this approach, but I seem to find a better way.   Perhaps another expert has an idea.

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
       
        {
            label1.Focus();
            btnExportToExcel.Enabled = true;
            btnExportToPowerPoint.Enabled = true;
        }

Open in new window

0
 
bassman592Author Commented:
Great idea! However, it works a little differently than you described. First, I tried what you suggested, using a label, but it didn't work. BTW, in all of these experiments, I'm using the Form's Load event instead of the control's ValueChanged event, since the highlight appears right off the bat. After trying the label, I tried palying with the tabindex property. Unfortunately, the dateTimePicker and the blank label (I was actually already using one) are the only 2 controls that are both visible and enabled, and for this to work, they both have to be true. Basically, I found that it works only if the "dummy" control is visible, enabled, and is a control that accepts some type of input - like a button, or a text box. Not a label, or a panel. So what I did was create a dummy button, make it really small, then I positioned it over the dateTimePicker control and changed its order to "send to back". So although it was visible and enabled, it still wouldn't be seen (or used). Then I made its tabindex = 0, and it works perfectly! As you said, not ideal, but it works. Also, I found that setting it's focus in code has no effect - you have to set the tabindex (although I guess you could do THAT in code). But the basic idea is yours, so I'm accepting your answer as the solution. Thank you.
0
 
bassman592Author Commented:
Good out-of-the-box thinking!
0
 
kdwoodCommented:

Glad you got it working.  Thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now