|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
August 28th, 2003, 02:52 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
formatting TextBox
How can I format TextBox to a date format like: YYYY/MM/DD
I'll be thankful.
Always:),
Hovik Melkomian.
__________________
Always,
Hovik Melkomian.
|
August 28th, 2003, 07:26 AM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If you just want to use the date format of the current locale then you can use something like this:
DateTime dt = Convert.ToDateTime(this.txtDate.Text);
this.txtDate.Text = dt.ToShortDateString();
However, if you want to force YYYY/MM/DD then there are a couple of options. The simplest is probably this, but it's fairly messy:
DateTime dt = Convert.ToDateTime(this.txtDate.Text);
this.txtDate.Text = dt.Year.ToString() + "/" + dt.Month.ToString() + "/" + dt.Day.ToString();
Alternatively, the "proper" way to do this (which is a fair bit more elegant) is:
DateTime dt = Convert.ToDateTime(this.txtDate.Text);
this.textBoxDate.Text = String.Format("{0:yyyy/MM/dd}", dt);
|
August 28th, 2003, 07:33 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
tnx 4 reply & ur time!
Always:),
Hovik Melkomian.
|
August 28th, 2003, 07:39 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
hi again, sorry:
How can I make the TextBox Selected.
In textBox of WebContols when the textBox is not empty in focus mode its selected my defualt?! Sould I code for that in Focus or there is a prperty?! I couldnt find it?!
I'll be thankful.
Always:),
Hovik Melkomian.
|
August 28th, 2003, 07:45 AM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If it's on a web page then you generally need to use JavaScript to set the focus on whatever control you want, like for example:
<SCRIPT language="JavaScript">
//Works in IE and netscape
document.getElementById("theASPdotNETID").focus();
//Works in IE only
document.all.theASPdotNETID.focus();
</SCRIPT>
|
August 28th, 2003, 08:00 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
I asked about WindowsApp. I know how its in Web. How shoudl I do it in Windows App.?!?!?
Always:),
Hovik Melkomian.
|
August 28th, 2003, 08:14 AM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry about that...
To set the focus in a Windows App use
FormName.ActiveControl = this.textBox;
or whatever. For some reason the textBox.Focus() method doesn't always seem to work in the load event of a form.
|
September 11th, 2003, 03:04 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
If I have my date in YYYYMMDD format how can I format it to YYYY/MM/DD.
Any help is appreciated.
Always:),
Hovik Melkomian.
|
September 11th, 2003, 06:45 AM
|
Authorized User
|
|
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
To do that you shouldn't need to use any DateTime objects. Just use string manipulation:
string dbDate = "20030911";
dbDate = dbDate.Substring(0, 4) + "/" + dbDate.Substring(4, 2) + "/" + dbDate.Substring(6, 2);
I can't find any other way of doing it, if anyone else can I'd be interested to hear it. If you need it as a DateTime object in the end you can just use, e.g.
DateTime dt = Convert.ToDateTime(dbDate);
|
September 11th, 2003, 06:58 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
Dear Edvard:
tnx for ur fast reply but I tryed it before & have some problem.
My data comes from DataBase using DataSet its in string format.
I DataBinded my field to TextBox & using dbDate = dbDate.Substring(0, 4) + "/" + dbDate.Substring(4, 2) + "/" + dbDate.Substring(6, 2); makes my program a little slower! Im guess I can do it with set & get the textBox but how dont know?!
Always:),
Hovik Melkomian.
|
|
|