473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

calendar

Hi,

I get the date from a calendar and put it into a text box.

TxtDate.Text = Calendar1.SelectedDate.ToShortDateString();

But I would like to also do it the other way around. That is, convert the
textbox to a calendar date, so that the calendar (when you open it) has the
same value as the date in the textbox.

Thanks for your help

Chris
Nov 17 '05 #1
4 4356
You could do:

Calendar1.SelectedDate = DateTime.Parse( TxtDate.Text );

But take care to catch possible exceptions.

Nov 17 '05 #2
You could do:

Calendar1.SelectedDate = DateTime.Parse( TxtDate.Text );

But take care to catch possible exceptions.

Nov 17 '05 #3
Chris

When you say 'calendar' I have assumed you meant the
System.Windows.Forms.DateTimePicker control.

This code adds the selected date to the text box:

private void Calendar1_ValueChanged(object sender, System.EventArgs e)
{
textBox1.Text = Calendar1.Value.ToShortDateString();
}

but you knew that bit :)

To go the other way you need to put some code in the controls 'Enter' event:

private void Calendar1_Enter(object sender, System.EventArgs e)
{
try
{
// set the date to be the date in the text box
char[] sep = {'/'};
string[] tokens = textBox1.Text.Split(sep);
int day = int.Parse(tokens[0]);
int month = int.Parse(tokens[1]);
int year = int.Parse(tokens[2]);

DateTime date = new DateTime(year, month, day);
Calendar1.Value = date;
}
catch
{
Calendar1.Value = DateTime.Now;
}
}

This is pretty crude and will obviously only work with dates in the
format 'day/month/year' using a '/' as a seperator but it should give
you a good start.

Hope this helps :)

Martin

--------------------------------------
- Martin Stickley : MCP C#, ASP .NET -
--------------------------------------
chris wrote:
Hi,

I get the date from a calendar and put it into a text box.

TxtDate.Text = Calendar1.SelectedDate.ToShortDateString();

But I would like to also do it the other way around. That is, convert the
textbox to a calendar date, so that the calendar (when you open it) has the
same value as the date in the textbox.

Thanks for your help

Chris

Nov 17 '05 #4
Does Microsoft ever think of the way to make datetime working better?
I always have different time each time I work with datetime.

"Martin" <ma*************@btinternet.com> wrote in message
news:d2**********@sparta.btinternet.com...
Chris

When you say 'calendar' I have assumed you meant the
System.Windows.Forms.DateTimePicker control.

This code adds the selected date to the text box:

private void Calendar1_ValueChanged(object sender, System.EventArgs e)
{
textBox1.Text = Calendar1.Value.ToShortDateString();
}

but you knew that bit :)

To go the other way you need to put some code in the controls 'Enter'
event:

private void Calendar1_Enter(object sender, System.EventArgs e)
{
try
{
// set the date to be the date in the text box
char[] sep = {'/'};
string[] tokens = textBox1.Text.Split(sep);
int day = int.Parse(tokens[0]);
int month = int.Parse(tokens[1]);
int year = int.Parse(tokens[2]);

DateTime date = new DateTime(year, month, day);
Calendar1.Value = date;
}
catch
{
Calendar1.Value = DateTime.Now;
}
}

This is pretty crude and will obviously only work with dates in the format
'day/month/year' using a '/' as a seperator but it should give you a good
start.

Hope this helps :)

Martin

--------------------------------------
- Martin Stickley : MCP C#, ASP .NET -
--------------------------------------
chris wrote:
Hi,

I get the date from a calendar and put it into a text box.

TxtDate.Text = Calendar1.SelectedDate.ToShortDateString();

But I would like to also do it the other way around. That is, convert the
textbox to a calendar date, so that the calendar (when you open it) has
the same value as the date in the textbox.

Thanks for your help

Chris

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sugapablo | last post by:
Can someone recommend a very simple script to produce a web calendar? I just want something where I can select a month and year and it produces a very basic HTML table, 7 columns across, one...
2
by: cg_news | last post by:
In short, what I am trying to do is, based on a date, calculate the week of year (as described in ISO 8601), then calculate the first and last date in this week period and return them in the format...
2
by: Caesar Augustus | last post by:
First, let me start by saying my asp.net experience is still in it's infancy so please bare with me as I try to explain my situation. I have created a single page that with the use of many...
0
by: R.A.M. | last post by:
Hello, I need to implement popup calendar in ASP.NET application. I created a button opening popup calendar on 'master' page: <asp:Button ID="Calendar" runat="server" Text=Calendar"...
1
by: R.A.M. | last post by:
Hello, I need to implement popup calendar in ASP.NET application. I created a button opening popup calendar on 'master' page: <asp:Button ID="Calendar" runat="server" Text=Calendar"...
0
by: GV | last post by:
Hi all, New to developing in VS 2005 ASP 2.0 Trying to have a easy pop calender for a button on a web page. I keep getting a error message in IE6 that says: Line 69 Char 3 Error:...
3
by: thorpk | last post by:
I posted this problem earlier in the month and some one decided it was better to change the subject and ask a completely different question. I am therefore reposting. I am hoping some one can...
0
by: mathewgk80 | last post by:
HI all, I am having popup calendar Javascript code. But i dont know how it is connecting to asp.net code.. I am using asp.net,c#.net and also using 3tier architecture with master page.... I...
4
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters...
1
by: abhishekbrave | last post by:
The code below is opening a calendar on mouse over in the same window. I need the calendar to be opened in new window. Have to fulfill this requirement urgentely so posting the whole code here. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.