Groups | Blog | Home
all groups > vb.net controls > february 2004 >

vb.net controls : Changing Time/Date values


AW
2/11/2004 9:21:41 PM
Sorry for lack of Subject in previous post.


[quoted text, click to view]

lpc2096 NO[at]SPAM microsoft.com
2/16/2004 6:34:32 PM
Hi,

The following sample should allow you to select a datetime field in a
textbox using one command button and incrementing the field value using a
second button. To use the following snippet you will need to create a
Windows Application in VB.NET. Add a textbox (Textbox1) and 2 command
buttons (Command1 and Command2) to the default form. paste the following
code into the form class Form1. Button1 is used to select the field and
Button2 is used to increment the selected datetime field.


'Form level variables:
Private CharPosition As Integer 'Starting Character position in
DateTime field
Private FieldNumber As Integer 'Field in date time selected
Private MyDateTime As Date = #1/2/2003 8:31:45 AM# 'Starting date time


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox1.Text = Format(MyDateTime, "dd/MM/yyyy hh:mm:ss")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NumberCharsInField As Integer = 2 'fields occupy 2 characters
except year
If CharPosition = 6 Then
NumberCharsInField = 4 'year field contains 4 characters
End If
If CharPosition = 9 Then CharPosition = 11 'Compensate for 4 char
year field
With TextBox1
.Select(CharPosition, NumberCharsInField) 'Select the desired
field
.Focus() 'Set focus back to textbox
End With
CharPosition += 3 'increment char position to next date time field
If FieldNumber >= 6 Then FieldNumber = 0 'Reset FieldNumber
FieldNumber += 1 'Increment field count
If CharPosition >= TextBox1.Text.Length Then CharPosition = 0
'Reset CharPosition

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Select Case FieldNumber
Case 1 'Day field selected
MyDateTime = MyDateTime.AddDays(1)
Case 2 'Month field selected
MyDateTime = MyDateTime.AddMonths(1)
Case 3 'Year field selected
MyDateTime = MyDateTime.AddYears(1)
Case 4 'Hours field selected
MyDateTime = MyDateTime.AddHours(1)
Case 5 'Minutes field selected
MyDateTime = MyDateTime.AddMinutes(1)
Case 6 'Seconds field selected
MyDateTime = MyDateTime.AddSeconds(1)
End Select
TextBox1.Text = Format(MyDateTime, "dd/MM/yyyy hh:mm:ss")

End Sub

Hope this helps.
Marc Rodman
AddThis Social Bookmark Button