×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Date in the MaskedTextBox

Date in the MaskedTextBox

Date in the MaskedTextBox

(OP)
Colleagues,

I tried to display a date in the MaskedTextBox control, with the mask "00/00/0000".
The date was 03/29/2020, and it was shown in that Masked text Box as "32/92/020_".
That is zero in "03" was dropped coz the short date format showed "3/29/2020".
I tried to display 01/01/2020 - and sure enough, it displayed "11/20/20__"
I tried masks "99/99/9999" and "##/##/####" - same result: no zeros.
Could you please help me to make a mask so it will not drop those zeros?
TIA!

Regards,

Ilya

RE: Date in the MaskedTextBox

Hi,

Do you mean that you could not enter from the keyboard...

03292020

...and see displayed...

03/29/2020

???

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: Date in the MaskedTextBox

Seems to me that at some point before it hits the masked textbox, VB is seeing 03292020 as a number, not a string, and thus stripping off the leading 0, son perhaps you can show us how you are populating the masked textbox

RE: Date in the MaskedTextBox

(OP)
Good day colleagues!
To SkipVought: sorry for not being specific enough! I meant "display a date in the MaskedTextBox control, with the mask "00/00/0000" programmatically".
To Strongm:

CODE --> vb

'====================================================================================================================================
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'====================================================================================================================================
' Purpose       : Initializes the default values for data entry boxes.
' Description   : Calculates the values for the Check, Period Begins and Period Ends dates, assigns app's start dir as a working dir.
' Side effects  : None.
' Notes         : 1. Company- and application-specific.
'                 2. Complies with .NET Framework ver. 1.1 and higher.
'====================================================================================================================================
pcStartDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
pcStartDir = CharTran(pcStartDir, "bin\Debug", "", 0) & "\"

Dim ldChkDate As Date = GetNextFridayDate(Today), _
    ldPerEnd As Date = ldChkDate.AddDays(-6), _
    ldPerBeg As Date = ldPerEnd.AddDays(-13)

Me.txtSrcXMLs.Text = pcStartDir
Me.txtClientID.Text = "TEST_CLIENT"
Me.txtRunID.Text = "PO007010800000614A"
Me.txtChkDate.Text = ldChkDate

Me.txtMaskedPerBeg.Text = New DateTime(2020, 1, 1) ' ldPerBeg

Me.txtChkPeriodStart.Text = ldPerBeg
Me.txtChkPeriodEnd.Text = ldPerEnd

End Sub
'==================================================================================================================================== 

Here's how the form looks after loading:



AHVBGA!

Regards,

Ilya

RE: Date in the MaskedTextBox

With Mask 00/00/0000
this code:

CODE

Me.txtMaskedPerBeg.Text = Format(New DateTime(2020, 1, 1), "MM/dd/yyyy") 
gives me:
01/01/2020


(upper / lower case in Format makes a difference...)


---- Andy

There is a great need for a sarcasm font.

RE: Date in the MaskedTextBox

Need to see code behind GetNextFridayDate

RE: Date in the MaskedTextBox

(OP)
To strongman: per your request

CODE --> vb

'===================================================================================================================================
Public Function GetNextFridayDate(ByVal tdDate As Date) As Date
'===================================================================================================================================
' Purpose       : Calculates the date of the next Friday.
' Description   : .
' Parameters    : Date as Date - mandatory.
' Returns       : Next Friday's date as Date.
' Side effects  : None.
' Notes         : 1. Generic, complies with .NET Framework ver. 1.1 and higher.
'                 2. If the tdDate parameter is blank - today's date is assumed.
'===================================================================================================================================
' Parameter's validation
If IsNothing(tdDate) Then
   tdDate = Today
Else

   If Not IsDate(tdDate) Then
      tdDate = Today
   End If 'Not IsDate(tdDate)

End If 'IsNothing(tdDate)

' Declare and initialize the needed local memvar
Dim ldFriday As Date = tdDate

' Calculate the date of the closest next Friday
Do While ldFriday.DayOfWeek < DayOfWeek.Friday
   ldFriday = ldFriday.AddDays(1)
Loop

Return ldFriday

End Function
'=================================================================================================================================== 

Albeit I can't imagine what you need it for - care to explain?

To Andrzejek: this works, thanks

Regards,

Ilya

RE: Date in the MaskedTextBox

What I and Andy and strongm were trying to get to was that you need a TEXT value to be entered into your masked textbox not a NUMERIC value. Two totally different animals.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

RE: Date in the MaskedTextBox

In your GetNextFridayDate, do you really need Parameter's validation since you accept tdDate As Date?
Even if you get Nothing, you still end up with a 'valid' date:

CODE

Dim dtMyDate As Date
dtMyDate = Nothing
Debug.Print(Format(dtMyDate, "MM/dd/yyyy hh:mm:ss")) 

gives you: 01/01/0001 at midnight, still a 'valid' date smile


---- Andy

There is a great need for a sarcasm font.

RE: Date in the MaskedTextBox

(OP)
To Andrzejek: old habit, colleague, old habit.
For my 30+ years of programming User Interfaces, I've seen so many cases when End User entered some "abracadabra", so I make it a rule for myself to either let User select an item from a list of pre-defined values, or verify/validate any "free-hand" entries.

HANW everybody!

Regards,

Ilya

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close