Results 1 to 6 of 6

Thread: [RESOLVED] Using With/EndWith for databound Properties

  1. #1

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

    Resolved [RESOLVED] Using With/EndWith for databound Properties

    When setting the databinding properties for a form control I usually set this from the properties window like so:

    Name:  PropertyTable.jpg
Views: 258
Size:  16.3 KB

    However, I would prefer to set this property manually in an event or routine in the form using a With/EndWith. Here is what I have figured out so far (for a lablel control:

    With lblName
    .DataBindings = .Text
    End With

    Of course I have no idea if what I already have is right. Can anyone provide me with the syntax for setting this property?

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Using With/EndWith for databound Properties

    What your doing is really way wrong. Your trying to set the labels bindingsource to the labels Text property.

    This should get you pointed in the right direction,
    Code:
                With Label1
                    .DataBindings.Add("Text", someBindingSource, "somefieldName")
                End With
    I should mention you can also bind the label using a datasource.

    for other examples and information search "visual basic label databindings property"
    Last edited by wes4dbt; Sep 23rd, 2017 at 05:15 PM.

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

    Re: Using With/EndWith for databound Properties

    There's no point using a With block to access one member. A With block doesn't actually do anything for the application. It simply exists to make your code a bit more succinct when accessing multiple members of the same reference. For instance, if you were binding a ComboBox then you might replace this:
    vb.net Code:
    1. ComboBox1.DisplayMember = "Name"
    2. ComboBox1.ValueMember = "ID"
    3. ComboBox1.DataSource = myDataTable
    with this:
    vb.net Code:
    1. With ComboBox1
    2.     .DisplayMember = "Name"
    3.     .ValueMember = "ID"
    4.     .DataSource = myDataTable
    5. End With
    Using With for one member actually makes the code more complex, so it's a bad idea.

  4. #4

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

    Re: Using With/EndWith for databound Properties

    Wes,

    I have no idea why this would be way wrong. In the example I was using above the form control was a label. The intent of the label was to be filled with data from a field in a tableadapter field (datasource). the text property is used to show any data that already resides in that field in the tableadapter. For a textbox or label this is usually what I would bind my control to.
    I especially use this where I use a combobox that is bound to a lookup table. The properties for a combobox should be set like so:

    Name:  ComboxProperties.jpg
Views: 134
Size:  36.2 KB

    What these settings do is provide the user with the ability to see any existing entry from the tableadapter (note the text property is set to a different table than the DataSource). The user can either use the combobox to input from the collection (in this case a lookup table), type in something different, or keep what is already there.

    Thanks for the info. That helps

  5. #5

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

    Re: Using With/EndWith for databound Properties

    jmChil,

    That is true about the With/EndWith. But there will indeed be more than one member when I am done with this. You are also correct about the only use is to make the code more succinct (readable), which is why I frequently use them.

    Anyway, thanks for the info, it helps a lot.

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

    Re: Using With/EndWith for databound Properties

    Quote Originally Posted by gwboolean View Post
    You are also correct about the only use is to make the code more succinct (readable)
    So the use of With in this case is irrelevant. All that's relevant is that you want to bind in code rather than in the designer.

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