hi i have a datetime picker in user control form in C#. when i load the user control i want the datetime picker to disply no value in the datetime picker box.
the code is below.

dpEDate.Format = DateTimePickerFormat.Custom;
            dpEDate.CustomFormat = " ";

and when the user control is loaded and when a user try to insert a value it dosen't allow to select the user value in the first attempt. but in the second attempt the user can select the value.
what is the solution to this?

Recommended Answers

All 24 Replies

I'm not sure what you are asking here. I created a form with a DateTimePicker and a label. I set the format as you did above and added a method to the DateTimePicker.ValueChanged event:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
    label1.Text = dateTimePicker1.Value.ToString();
}

If you do this you'll see that the value changes every time.

If you use code like this to hide the displayed date

dateTimePicker1.Format = DateTimePickerFormat.Custom;
	dateTimePicker1.CustomFormat = " ";

[Edit] (BTW thanks for the tip on how to do this)[/Edit]
then you can use the ValueChanged event to reset the display format.

private void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)
{
	dateTimePicker1.Format = DateTimePickerFormat.Long; // use long format or
	//dateTimePicker1.CustomFormat = "dd/MM/yyyy"; // set custom to desired value
}

When the user selects a date it is immediately displayed in the format you want.

If you use code like this to hide the displayed date

dateTimePicker1.Format = DateTimePickerFormat.Custom;
	dateTimePicker1.CustomFormat = " ";

[Edit] (BTW thanks for the tip on how to do this)[/Edit]
then you can use the ValueChanged event to reset the display format.

private void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)
{
	dateTimePicker1.Format = DateTimePickerFormat.Long; // use long format or
	//dateTimePicker1.CustomFormat = "dd/MM/yyyy"; // set custom to desired value
}

When the user selects a date it is immediately displayed in the format you want.

hey
when i use this code there is a problem in it.

when the user first tries to select the date the datetime picker drop doen disply will not go away? why is this happening

Don't know. Why don't you give us clue (i.e. some code).

Don't know. Why don't you give us clue (i.e. some code).

when i add the ode you have posted and select the date the value change method executes
in it i have the code

dpDOB.Format = DateTimePickerFormat.Short;

and for the value for 'dpDOB.Format' the value show as short when the add break points and see

any idea...

private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format= DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat=" ";
        }

select your datetime picker control and select <ValueChanged>  event, then write the code as below...

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Short;
        }
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format= DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat=" ";
}
select your datetime picker control and select <ValueChanged>  event, then write the code as below...
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
}

this is exactly what i did. when i select the date from the datetimepicker in the first sattempt the datetimepicker drop down dosen't close when i select on a date . i have to click somewhere else in the form to close it.

did you get it
how can i solve his??? any idea what the problem is

Are you attaching to any other of the DateTimePicker events (e.g. Validating)?
Are there any data bindings to this DataTimePicker?

Are you attaching to any other of the DateTimePicker events (e.g. Validating)?
Are there any data bindings to this DataTimePicker?

no i am only using the form load and the datetime picker value changed, nothing else

i have to click another place in the form for the date to display in the date time picker.
how can i solve this problem

OK, try creating a completely new Windows forms project, add a DateTimePicker to the form and the code to hide the date etc. then test its operation.

If it works then your problem is something else in your current project that is affecting the way the DateTimePicker operates. Think about what this could be.

If if does not work then ... I don't know ... .Net framework issue?

OK, try creating a completely new Windows forms project, add a DateTimePicker to the form and the code to hide the date etc. then test its operation.

If it works then your problem is something else in your current project that is affecting the way the DateTimePicker operates. Think about what this could be.

If if does not work then ... I don't know ... .Net framework issue?

i tried it it is the same thing happening,

when i click on a date the drop down date picker will not go away,
i have to click a different area after i click on the date for it to appear in the date time picker,

but the second time onwards it works,
so it is just the fist time that it is not working

I can not get it to fail in the way you describe.
In you test project the DateTimePicker is the only thing on the form, right?

I can not get it to fail in the way you describe.
In you test project the DateTimePicker is the only thing on the form, right?

yes it is the only thig in the form

OK, this is odd.

Clutching at straws here, but let's see if using your code make any difference.
As it should be a small project, post all the code in Form1.cs and Form1.Designer.cs from your test project.

Also, post any other info, such as PC type .net framework, VS version etc. that you think might have any connection with what is happening.

Does anyone else have any ideas?

OK, I found this thread that at the end discusses the very same issue.
There is no solution given but it made me think of something you could try.
Instead of setting the format back to Short in the ValueChanged event try using the CloseUp event which fires when the calendar window is closed.
(This worked fine when I tested it, but then so does using ValueChanged).
Let me know how you get on.

commented: For sticking with this for so long :) +14

OK, I found this thread that at the end discusses the very same issue.
There is no solution given but it made me think of something you could try.
Instead of setting the format back to Short in the ValueChanged event try using the CloseUp event which fires when the calendar window is closed.
(This worked fine when I tested it, but then so does using ValueChanged).
Let me know how you get on.

it works


thank you

Good. Now please mark the thread as solved.

OK. I did a little bit of digging in to how the DTP works using Reflector.
It is way too deep for me to fully understand (lots of windows messaging), but I did find out that setting the Format causes the control to recreate its window handle.
This must be interfering with the calendar drop down operation when done in the ValueChanged event handler.
Still don't understand why it works on my machine and not on yours.:confused:

OK. I did a little bit of digging in to how the DTP works using Reflector.
It is way too deep for me to fully understand (lots of windows messaging), but I did find out that setting the Format causes the control to recreate its window handle.
This must be interfering with the calendar drop down operation when done in the ValueChanged event handler.
Still don't understand why it works on my machine and not on yours.:confused:

may be the version is different
i am using visual studio 2008 standard, may be some thing is different

send me the article DTP works using reflectors

Reflector is a decompiler for .Net applications.
With it you can look at the .Net framework dll files and see how they are coded.
(This is not always a good idea because it usually results in more confusion and a lot of time wasted following various code paths.)
It used to be free but now costs about $35.
There is a beta version of a free decompiler by JetBrains called dotPeek but it is a bit buggy.
Here is a link if you are interested in trying it.

Reflector is a decompiler for .Net applications.
With it you can look at the .Net framework dll files and see how they are coded.
(This is not always a good idea because it usually results in more confusion and a lot of time wasted following various code paths.)
It used to be free but now costs about $35.
There is a beta version of a free decompiler by JetBrains called dotPeek but it is a bit buggy.
Here is a link if you are interested in trying it.

hi
i am having a problem in displaying it to the form from the database
how can i solve this

regards,

i am having a problem in displaying it to the form from the database
how can i solve this
regards,

Firstly, What is 'it'?

Secondly, this sounds like a different problem. Perhaps you should start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.