locked
formating textbox databinding text RRS feed

  • Question

  • I'm filling some textboxes by databinding them to a dataset which works fine.
    However on the one displaying a date I get date and time:
    03/05/2016 00:00:00

    Is there a way I can
    get the textbox to just display the date:

    03/05/2016

    I tried the code below but clearly not right

    Dim QDatebinding As New Binding("Text", HdataSet.Tables(0), "QuoteDate")
            QDatebinding.FormatString = "dd/mm/yyyy"
            TextBox4.DataBindings.Add(QDatebinding)

    Friday, May 27, 2016 12:35 PM

Answers

  • Hi

    Have a try with this (I have typed directly so not 100% sure it is correct)

     With TextBox4.DataBindings.Add("Text", HdataSet.Tables(0), "QuoteDate")
                .FormattingEnabled = True
                .FormatString = "dd/mm/yyyy"
            End With


    Regards Les, Livingston, Scotland

    • Marked as answer by AndyNakamura Friday, May 27, 2016 3:18 PM
    Friday, May 27, 2016 3:03 PM

All replies

  • You can use the Format Event. See below link:

    https://bytes.com/topic/visual-basic-net/answers/350192-formatting-texbox-show-date-specific-format


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, May 27, 2016 1:03 PM
  • Thanks Paul,
    I've tried the method in the link you sent but I must be doing something wrong because the program never enters the Sub. It just reads the addHandler line then moves on the the next line.

    TextBox4.DataBindings.Add(QDatebinding)
            AddHandler TextBox4.DataBindings(0).Format, AddressOf Me.FormatDate
        Private Sub FormatDate(ByVal sender As Object, ByVal e As ConvertEventArgs)
            Dim dt As Date
            Try
                dt = e.Value
                e.Value = dt.ToShortDateString 'Format(e.Value, "MMM d, yyyy")
            Catch
                e.Value = "Unknown"
            End Try
        End Sub
    Not sure what I am doing wrong here.

    Friday, May 27, 2016 1:40 PM
  • Hi

    Have a try with this (I have typed directly so not 100% sure it is correct)

     With TextBox4.DataBindings.Add("Text", HdataSet.Tables(0), "QuoteDate")
                .FormattingEnabled = True
                .FormatString = "dd/mm/yyyy"
            End With


    Regards Les, Livingston, Scotland

    • Marked as answer by AndyNakamura Friday, May 27, 2016 3:18 PM
    Friday, May 27, 2016 3:03 PM
  • Hi leshay,
    That works correctly and no need to add a handler.
    I since got a variation of Paul's link to work by adding the handler to the binding. (I didn't realise I could do it all in one line.

    Dim QDatebinding As New Binding("Text", HdataSet.Tables(0), "QuoteDate")
            AddHandler QDatebinding.Format, AddressOf Me.FormatDate
            TextBox4.DataBindings.Add(QDatebinding)
        End Sub
    
        Private Sub FormatDate(ByVal sender As Object, ByVal e As ConvertEventArgs)
            Dim dt As Date
            Try
                dt = e.Value
                e.Value = dt.ToShortDateString 'Format(e.Value, "MMM d, yyyy")
            Catch
                e.Value = "Unknown"
            End Try
        End Sub
    Thanks


    Friday, May 27, 2016 3:17 PM