Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date in the MaskedTextBox

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
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
 
Hi,

Do you mean that you could not enter from the keyboard...
[tt]
03292020
[/tt]
...and see displayed...
[tt]
03/29/2020
[/tt]
???

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
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
 
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:
'====================================================================================================================================
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:

20200416MaskedTxtBoxDatesMissingZeros_vtsfds.jpg


AHVBGA!



Regards,

Ilya
 
With Mask [tt]00/00/0000[/tt]
this code:
Code:
Me.txtMaskedPerBeg.Text = Format(New DateTime(2020, 1, 1), "MM/dd/yyyy")
gives me: [tt]
01/01/2020[/tt]

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


---- Andy

There is a great need for a sarcasm font.
 
To strongman: per your request

Code:
'===================================================================================================================================
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
 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
In your [tt]GetNextFridayDate[/tt], do you really need Parameter's validation since you accept [tt]tdDate As Date[/tt]?
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 :)




---- Andy

There is a great need for a sarcasm font.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top