-
Sep 23rd, 2017, 01:49 PM
#1
Thread Starter
Fanatic Member
[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:
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?
-
Sep 23rd, 2017, 05:11 PM
#2
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.
-
Sep 23rd, 2017, 11:23 PM
#3
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:
ComboBox1.DisplayMember = "Name" ComboBox1.ValueMember = "ID" ComboBox1.DataSource = myDataTable
with this:
vb.net Code:
With ComboBox1 .DisplayMember = "Name" .ValueMember = "ID" .DataSource = myDataTable End With
Using With for one member actually makes the code more complex, so it's a bad idea.
-
Sep 24th, 2017, 12:31 PM
#4
Thread Starter
Fanatic Member
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:
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
-
Sep 24th, 2017, 12:35 PM
#5
Thread Starter
Fanatic Member
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.
-
Sep 24th, 2017, 06:33 PM
#6
Re: Using With/EndWith for databound Properties
Originally Posted by gwboolean
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|