Results 1 to 14 of 14

Thread: Date format in bound textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Date format in bound textbox

    I show dates in a bound textbox using data bindings.
    Data is displayed with date and time. How can I change the format to show date or time only?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: Date format in bound textbox

    Meanwhile I could resolve the problem using the masked text box instead of the text box.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Date format in bound textbox

    An even better solution would have been to use the DateTimePicker control.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: Date format in bound textbox

    I think, you are right.
    I used the DTP in the beginning but in this particular case, I did not want the user to accedently open the DTP and change the value. Only in certain cases the DTP should be used to change data. For this case I decided to use the method that has been published elsewhere in the forum.
    I move a hidden DTP to the location of the date, make it visible and copy the selected date to the field. It works very nicely. In fact the DTP is placed at the right side of the datefield. This way it looks very similar to the Datefield in a grid in MS Access which only comes up when the field is selected.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Date format in bound textbox

    Your method of hiding the DateTimePicker seems more of a hack rather than a solution. If you do not want the user to edit the value then you'd toggle the Enabled property. Let's assume that you do not want the user to edit the value in the DateTimePicker when a ComboBox's SelectedIndex is anything except for 0, then you'd use the following code in the SelectedIndexChanged event:
    Code:
    MyDateTimePicker.Enabled = (MyComboBox.SelectedIndex = 0)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Date format in bound textbox

    To answer your original question, set the format on the binding. Something like this:
    Code:
    Dim bs As New BindingSource
    bs.DataSource = {DateTime.Now(), DateTime.Now.AddDays(5)}
    TextBox1.DataBindings.Add("Text", bs, Nothing, True, DataSourceUpdateMode.OnPropertyChanged, String.Empty, Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: Date format in bound textbox

    To dday9 and TnTinMN,
    I am looking with great interest at your solution and realize that I still have a long way to go to be reasonably familiar with VB.
    Would you please be so kind and explain the syntax to me.
    thanks

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Date format in bound textbox

    In terms of my suggestion, the Enabled property is a Boolean property that indicates whether or not the control is editable or not(similar to a TextBox's ReadOnly property). I set the Enabled property to whether or not the ComboBox's SelectedIndex is 0. It is a much more concise version of this:
    Code:
    If MyComboBox.SelectedIndex = 0 Then
        MyDateTimePicker.Enabled = True
    Else
        MyDateTimePicker.Enabled = False
    End If
    In terms o f TnTinMN's suggestion, he is using a BindingSource which can be thought of as the "glue to hold the data/control relation" between your data source and control in which is being bound. He then set's the DataSource of the BindingSource equal to the current Date and Time as well as the 5 days from the current Date and Time. Finally he bind's the TextBox's Text to show just the Short Date pattern of the selected DateTime value.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: Date format in bound textbox

    thanks for explanation.
    How do I know that the brackets act as if-clause and MyComboBox.SelectedIndex will not be assigned 0? Is there some doc abound this kind of syntax.

    For the second I think I need time to digest. I even couldn't ask a question to this.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Date format in bound textbox

    They do not act as an if-clause, I use them to improve readability. You could just as well have used:
    Code:
    MyDateTimePicker.Enabled = MyComboBox.SelectedIndex = 0
    The point is that the Enabled property gets or sets a Boolean value and so I set it equal to the logical expression of if the SelectedIndex equals 0. This is one of the few times I prefer C# syntax because in C# you will explicitly know that we are doing a comparison and not an assignment:
    Code:
    //C# Syntax
    MyDateTimePicker.Enabled = MyComboBox.SelectedIndex == 0;
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: Date format in bound textbox

    That means VB works from right to left. Any positive number will be converted to 1 (true) and 0 would be converted to 0 (false). This makes sense to me.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Date format in bound textbox

    I don't understand what you mean right-to-left.

    The code assigns a property equal to the result of a comparison.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Date format in bound textbox

    I have no problem trying to provide a better explanation than you could find by looking at the documentation. However, I expect you to first make an attempt at reading the relevant information first and then making a concise yet precise explanation of that which is not understood. You can almost always be successful with a search engine query by prefacing the query with "msdn" followed by the search term.

    It has become apparent from your posts that you really need instruction on the VB language itself. To that end, I recommend that you study the "Visual Basic Language Reference". You can also find similar documentation in your VB installation directory. It will be located at a path similar to:

    "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VB\Specifications\1033\Visual Basic Language Specification.docx"

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Date format in bound textbox

    Quote Originally Posted by smktec View Post
    That means VB works from right to left. Any positive number will be converted to 1 (true) and 0 would be converted to 0 (false). This makes sense to me.
    That's not true... False is defined as 0 ... True is defined as Not False ... which means ANY non-zero number evaluates to True... VB may interpret 1 as true, but to other languages True has a value of -1 ...

    At any rate, 0 is always regard as false... and other value is regarded as True, doesn't matter it is positive or negative.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width