Results 1 to 12 of 12

Thread: [RESOLVED] databound textbox.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Resolved [RESOLVED] databound textbox.

    I am having a little problem with a databinding.

    I am using the following to display a date in a textbox on a form:

    Code:
    .txtOpen.DataBindings.Add(New Binding("Text", frmEvent._MasterBase5_0DataSet, "tblEventMaster.dteOpen"))
    This is to display a date field set as a short date. I do get a short date in the textbox, but it also includes the time. It is my guess that the problem is from the "Text", but I have no way of knowing that for sure. So on the assumption that I am correct, what should "Text" be replaced with?

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: databound textbox.

    Typically you would use a datetimepicker control
    Code:
            Dim DT As New DataTable
            'Fillyour DT
            Dim BS As New BindingSource With {.DataSource = DT}
            DateTimePicker1.DataBindings.Add("Value", BS, "DateCol")
    She dont like NULL values tho

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: databound textbox.

    If you are going to use a TextBox then you don't replace "Text" with anything because that specifies the property of the control that you're binding to. What you need to do is set the FormatString of your Binding object. You use the same format specifiers as you would for String.Format or Date.ToString, e.g.
    vb.net Code:
    1. .txtOpen.DataBindings.Add(New Binding("Text", frmEvent._MasterBase5_0DataSet, "tblEventMaster.dteOpen") With {.FormattingEnabled = True, .FormatString = "d"))

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: databound textbox.

    That would be a problem. There are 5 of these fields and the dates are filled in only upon the completion of certain activities, i.e. they are null for much of the time that this form, and its controls, are accessed.

    I have another form with texboxes like the one under discussion that have textbox controls that are databound to date fields. In this case each of the controls are databound to their respective fields through the control properties window. All of those textboxes display the short date as it is found in the database/dataset.

    I also took a look at the use of a masked textbox set for short date. It had a problem with dates that did not have a leading zero where the month was less than 10. e.g. 1/12/2018 would display as 11/22/018_ That didn't help.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: databound textbox.

    JM, that works! But I have to say I really have no idea what that is all about. Could you perhaps guide me to where I could obtain an explanation as to what exactly is going on?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: databound textbox.

    In your Binding constructor, the three arguments are the control property to bind, the list containing the data items and the list item property to bind.

    In your case, you're specifying that the Text property of the control should be bound, so the data from the list item gets assigned to that Text property. In the case of a TextBox, the Text property is obviously the property that you're most likely to bind to, but you could also bind other properties, e.g. bind to ReadOnly if you wanted control whether the contents could be edited based on data rather than logic. If you were going to bind a CheckBox control then you'd normally bind the Checked property. Likewise, you'd bind Value for a DateTimePicker and SelectedValue for a ComboBox or ListBox.

    You're also specifying that 'frmEvent._MasterBase5_0DataSet' is to be the source of the items to bind and, for the current item, the data to use is to come from the 'tblEventMaster.dteOpen' property. Personally, I would tend to specify 'frmEvent._MasterBase5_0DataSet.'tblEventMaster' as the list and 'dteOpen' as the property but it amounts to the same thing.

    The 'With' clause that I've used there is simply a shorthand for setting property values on a newly created object. It's called Object Initialiser Syntax. The code I posted is functionally equivalent to this:
    vb.net Code:
    1. Dim b As New Binding("Text", frmEvent._MasterBase5_0DataSet, "tblEventMaster.dteOpen")
    2.  
    3. With b
    4.     .FormattingEnabled = True
    5.     .FormatString = "d"
    6. End With
    7.  
    8. .txtOpen.DataBindings.Add(b)
    or this:
    vb.net Code:
    1. Dim b As New Binding("Text", frmEvent._MasterBase5_0DataSet, "tblEventMaster.dteOpen")
    2.  
    3. b.FormattingEnabled = True
    4. b.FormatString = "d"
    5.  
    6. .txtOpen.DataBindings.Add(b)
    The FormattingEnabled property determines whether the FormatString property is used or ignored. If it's True, the value to be assigned to the control's bound property has its ToString method called and the value of the FormatString property passed as an argument, so you're effectively calling 'myDate.ToString("d")'. The "d" specifier is standard for short date.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: databound textbox.

    Thanks JM, I see how that works now that you put it into a form I have used and understand. I have never seen a With used like that before. I have to say that, for whatever reason, a lot of things are beginning to become clear to me that never were before.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: databound textbox.

    Actually, I have a couple more questions then I will let you rest. I noticed that in the form where I had set the databinding properties for a textbox control in the properties window the date did not need to have any further formatting properties set to be correct. I was unable to find the .formattingEnabled and the .FormatString properties anywhere in the properties window. Where would these be found and why are they automatically set when the databinding properties are set using the properties window?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: databound textbox.

    Quote Originally Posted by gwboolean View Post
    I noticed that in the form where I had set the databinding properties for a textbox control in the properties window the date did not need to have any further formatting properties set to be correct. I was unable to find the .formattingEnabled and the .FormatString properties anywhere in the properties window. Where would these be found and why are they automatically set when the databinding properties are set using the properties window?
    Select your control in the designer, open the Properties window, expand the (DataBindings) node, select the Advanced item and click the button with the ellipsis (...). That's where you configure the Binding object that is generated via the designer. You don't set the properties directly necessarily. For instance, if 'Format type' is set to 'No Formatting' then 'FormattingEnabled will be False, otherwise it will be True. If you select 'Date Time', then you get to select the FormatString via example. I just did that with a TextBox bound to a DateOfBirth column and I got this in the designer code file:
    vb.net Code:
    1. Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.PersonBindingSource, "DateOfBirth", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, Nothing, "d"))
    Note that that sets FormattingEnabled and FormatString via the Binding constructor, using the fourth and seventh parameters respectively.

    Note that, if you want to open the designer code file, you'll need to click the 'Show All Files' button in the Solution Explorer first and expand the node for your form.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: [RESOLVED] databound textbox.

    This is truly some detailed and esoteric crap. But as I long ago learned, details kill. It seems like the deeper I dive into this the worse it gets. Now that I have the whining out, this is actually all pretty cool. I am getting to the point where I can come close to understanding what all is going on, but I still find the designer to be a little scary

    Thanks JM

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [RESOLVED] databound textbox.

    With regards to using the designer for binding, most people would tend to recommend against it and, in fact, it's probably the main reason that typed DataSets have an undeserved bad name. Setting up binding in the designer means tightly coupling your data access code and your presentation code. Doing so makes things easier for beginners because they can get a lot done with a limited understanding of how it works but more experienced developers prefer to separate their data access code and their presentation code, possibly into separate projects.

    The Binding object is actually perfectly logical and simply does the work of moving the data between the data source and the UI and back again that you would otherwise have to write code for yourself. The first three arguments to the Binding constructor in my code, i.e. "Text", Me.PersonBindingSource and "DateOfBirth", specify that Me.PersonBindingSource is the data source and, when applicable, data is to be copied from the "DateOfBirth" property of its current item to the "Text" property of the control and back again. That saves you write code like this:
    vb.net Code:
    1. Me.TextBox1.Text = DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth").ToString()
    and like this:
    vb.net Code:
    1. DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth") = Date.Parse(Me.TextBox1.Text)
    The fourth and seventh arguments, i.e. True and "d", specify that the dates displayed should use the system short date format, so saving you from writing code like this:
    vb.net Code:
    1. Me.TextBox1.Text = DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth").ToString("d")
    and like this:
    vb.net Code:
    1. DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth") = Date.ParseExact(Me.TextBox1.Text, "d", Nothing)
    The fifth argument, i.e. System.Windows.Forms.DataSourceUpdateMode.OnValidation, specifies that the data should be copied from the control to the data source on the Validated event rather than on the TextChanged event, which is a decision you'd have to make too, if you were writing the code yourself. The code inside a Binding object is reasonably complex because it has to handle many more situations that you would write code for yourself, but it still just does the very same things that you would do if you were moving the data back and forth yourself.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: [RESOLVED] databound textbox.

    Looks pretty cool JM, but this is going to take some time for me to digest and wrap my head around. From what I can already understand this really looks clean to me and probably much more efficient. I do thank you.

Tags for this Thread

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