Datetime format in textbox

  • Thread starter Thread starter Agnes
  • Start date Start date
A

Agnes

I know someone post the solution before, but I didn't keep in mind .
So I am sorry to ask the same question again
I had bind a datatime to the textbox name txtInvoice

I want it displayed in dd-mm-yyyy
I try txtInvoice.txt = Format(txtInvoice.txt,"dd-mm-yyyy"), its return
error.
Anyone know to do it ??
Thanks
 
* "Agnes said:
I had bind a datatime to the textbox name txtInvoice

I want it displayed in dd-mm-yyyy
I try txtInvoice.txt = Format(txtInvoice.txt,"dd-mm-yyyy"), its return
error.

\\\
Dim d As Date = Date.Parse(txtInvoice.Text)
txtInvoice.Text = d.ToString(...)
///
 
Hi Agnes,

It was still on my clipboard as answer to John,

I hope this helps?

Cor

Private Sub myroutine()
Mybinding = New Binding("Text", ds.Tables(0), "mydatfield")
textdatfield.DataBindings.Add(Mybinding)
AddHandler mybinding.Format, AddressOf DBdateTextbox
AddHandler mybinding.Parse, AddressOf TextBoxDBdate
End sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("dd - MM - yyyy")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
///
 
Dear Cor,
It works BUT as i want to overwrite the date , it won't allow . I over
the date e.g 15-07-2004 to 05-07-2004,after press'tab'
It changed back into '15-07-2004'
Please ~~~
From AGnes
p.s- Your got a cliboard ?? where is it ?
 
Hi Agnes,

What is the standard time notation in Hongkong?

Cor

ps my clipboard was the place I had copied things.
 
Standard time notation in Hong kong ? normally dd-mm-yyyy
If i used in vfp . it is called italian date format

From Agnes
 
Hi Agnes,

Before I wrote this message I tested it completly again, while I have used
it before, however I could not simulate your problem so show some code as
you have implemented it?

Cor
 
I just wrote a simple form with 2 textbox , txtDate is binded as following
As form load, it showed my data e.g 19-05-2004, and then i overwrite the
date field into '20-05-2004' ,press tab
it changed back into '19-05-2004'
Anything I done wrong ???
From Agnes

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
daTest.Fill(DsTest)
Dim Mybinding As New Binding("Text", DsTest.Tables(0), "date")
txtDate.DataBindings.Add(Mybinding)
AddHandler txtDate.DataBindings("Text").Format, AddressOf
DBdateTextbox
AddHandler txtDate.DataBindings("Text").Parse, AddressOf
TextBoxDBdate
End Sub
 
Hi Agnes,

Can you try this sample, it needs one textbox and one button on a form.

I hope this helps?

Cor

\\\
Dim dt As New DataTable
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add(New DataColumn("mydate", GetType(System.DateTime)))
dt.Rows.Add(dt.NewRow)
Dim Mybinding As New Binding("Text", dt, "mydate")
TextBox1.DataBindings.Add(Mybinding)
AddHandler Mybinding.Format, AddressOf DBdateTextbox
AddHandler Mybinding.Parse, AddressOf TextBoxDBdate
dt.Rows(0)("mydate") = New Date(2004, 7, 15)
End Sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("dd - MM - yyyy")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
BindingContext(dt).EndCurrentEdit()
MessageBox.Show(dt.Rows(0)(0).ToString)
End Sub
///
 
Dear Cor,
I just try it.. [I overwrite the date to 16-07-2004 ,after press button
, it changed back >_< ]
Anyway, thanks for your kind help and coding.
I will try my best to search thr internet and seek the solution.
Thanks in advance
From Agnes
 
Dear Great Cor,
I add this statment before Bindingcontext.endcurrentedit()
[dt.Rows(0)("mydate") = TextBox1.Text]
It seems work and accept my overwrite, I will try it more and let u know the
result

From Agnes

Agnes said:
Dear Cor,
I just try it.. [I overwrite the date to 16-07-2004 ,after press button
, it changed back >_< ]
Anyway, thanks for your kind help and coding.
I will try my best to search thr internet and seek the solution.
Thanks in advance
From Agnes

Cor Ligthert said:
Hi Agnes,

Can you try this sample, it needs one textbox and one button on a form.

I hope this helps?

Cor

\\\
Dim dt As New DataTable
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add(New DataColumn("mydate", GetType(System.DateTime)))
dt.Rows.Add(dt.NewRow)
Dim Mybinding As New Binding("Text", dt, "mydate")
TextBox1.DataBindings.Add(Mybinding)
AddHandler Mybinding.Format, AddressOf DBdateTextbox
AddHandler Mybinding.Parse, AddressOf TextBoxDBdate
dt.Rows(0)("mydate") = New Date(2004, 7, 15)
End Sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("dd - MM - yyyy")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
BindingContext(dt).EndCurrentEdit()
MessageBox.Show(dt.Rows(0)(0).ToString)
End Sub
///
 
Oh my god, from 15-07-2004 to 16-07-2004 is ok .
however, 15-08-2004 , it got invalid datetime error

Agnes said:
Dear Cor,
I just try it.. [I overwrite the date to 16-07-2004 ,after press button
, it changed back >_< ]
Anyway, thanks for your kind help and coding.
I will try my best to search thr internet and seek the solution.
Thanks in advance
From Agnes

Cor Ligthert said:
Hi Agnes,

Can you try this sample, it needs one textbox and one button on a form.

I hope this helps?

Cor

\\\
Dim dt As New DataTable
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add(New DataColumn("mydate", GetType(System.DateTime)))
dt.Rows.Add(dt.NewRow)
Dim Mybinding As New Binding("Text", dt, "mydate")
TextBox1.DataBindings.Add(Mybinding)
AddHandler Mybinding.Format, AddressOf DBdateTextbox
AddHandler Mybinding.Parse, AddressOf TextBoxDBdate
dt.Rows(0)("mydate") = New Date(2004, 7, 15)
End Sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("dd - MM - yyyy")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
BindingContext(dt).EndCurrentEdit()
MessageBox.Show(dt.Rows(0)(0).ToString)
End Sub
///
 
Hi Agnes,

You can be sure that I tested this, this is a one piece complete testable
new build solution.

So in this sample it should do it. Or do you not mean that with this
message?

Cor
 
That is because you need to create a custom databinding object with an
event that handles the formating and add the format function in there.
Ex.

Dim WithEvents oBinding As Binding
dim oDt as new Dataset'Make sure this is the correct dataset you are
binding the text box to.

private sub BindData()
'A new binding object declared above using "WithEvents" is explicitly
created that
'binds the text field to the "GetDateTime" property of the
MyDateTime class.
'The binding object is then added to the textbox's databindings
collection.

oBinding = New Binding("Text", oDt, "GetDateTime")
txt1.DataBindings.Add(oBinding)
txt2.DataBindings.Add("Text", oDt, "GetDateTime")
end sub

Private Sub oBinding_Format(ByVal sender As Object, ByVal e
As System.Windows.Forms.ConvertEventArgs) Handles
oBinding.Format
e.Value = Format(e.Value, "MM/dd/yy")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load


BindData()
end sub

Brad Shook
Programmer/Analysts II
 
Back
Top