Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old August 28th, 2003, 02:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default 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.
 
Old August 28th, 2003, 07:26 AM
Authorized User
 
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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);

 
Old August 28th, 2003, 07:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

tnx 4 reply & ur time!

Always:),
Hovik Melkomian.
 
Old August 28th, 2003, 07:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.
 
Old August 28th, 2003, 07:45 AM
Authorized User
 
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>

 
Old August 28th, 2003, 08:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I asked about WindowsApp. I know how its in Web. How shoudl I do it in Windows App.?!?!?

Always:),
Hovik Melkomian.
 
Old August 28th, 2003, 08:14 AM
Authorized User
 
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old September 11th, 2003, 03:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

If I have my date in YYYYMMDD format how can I format it to YYYY/MM/DD.
Any help is appreciated.

Always:),
Hovik Melkomian.
 
Old September 11th, 2003, 06:45 AM
Authorized User
 
Join Date: Aug 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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);

 
Old September 11th, 2003, 06:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Textbox Number Formatting pencilneck Visual Basic 2005 Basics 2 October 25th, 2007 03:46 PM
pointing cursor from one textbox to other textbox lakshmi_annayappa ASP.NET 1.0 and 1.1 Basics 2 August 2nd, 2007 03:41 PM
Formatting TextBox bound to a database column Raby Visual Basic 2005 Basics 1 September 6th, 2006 07:47 PM
Textbox Formatting echovue Access 5 May 26th, 2006 09:47 AM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.