Avatar of Tony Gardner
Tony Gardner
Flag for United States of America asked on

Handling DataBindings Properties for Constituent Objects within a User Control

Hello Again Experts.

I am continuing my learning process with creating a simple User Control. That is, I'm fairly certain that it should be simple!

My User Control is called "TwoLabels". It consists of two labels -- one on the left and one on the right. Typically, the left side would be literal text, and the right side would be used for DataBound text, but it should support any combination if possible.

I've learned that the two labels are "constituent controls" and as such I need to create properties in the class to expose the properties I will need to access in the Designer.

This is no problem for simple properties such as Text. ForeColor, etc. The property I'm stuck on of course is the DataBindings.

If you're familiar with the Designer, you know that there is a dropdown available for selecting the DataBinding Text value. When I add a TwoLabels object to my data bound form, this dropdown is available, and I can select a value from my DataSource, but it doesn't appear to have any affect on the displayed value at runtime.

I've tried creating Properties such as this, but without the dropdown, the entered value is not reliable.

Imports System.Windows.Forms

Public Class TwoLabels

    Private varValue1L As String
    Private varValue1R As String
    Private varValue2L As String
    Private varValue2R As String
    Private varValue3L As String
    Private varValue3R As String[embed=file 1406767]

    Property LeftText() As String
        Get
            Return lblLeftText.Text
        End Get
        Set(ByVal value As String)
            varValue1L = value
            lblLeftText.Text = varValue1L
        End Set
    End Property

    Property RightText() As String
        Get
            Return lblRightText.Text
        End Get
        Set(ByVal value As String)
            varValue1R = value
            lblRightText.Text = varValue1R
        End Set
    End Property

    Property LeftBindingText() As String
        Get
            Return varValue2L
        End Get
        Set(ByVal value As String)
            varValue2L = value
            lblLeftText.Text = varValue2L
        End Set
    End Property

    Property RightBindingText() As String
        Get
            Return varValue2R
        End Get
        Set(ByVal value As String)
            varValue2R = value
            lblRightText.Text = varValue2R
        End Set
    End Property

    Property LeftBindingSrc() As String
        Get
            Return varValue3L
        End Get
        Set(ByVal value As String)
            varValue3L = value
            lblLeftText.DataBindings.Add(New Binding("Text", Main.sData, varValue3L, True))
        End Set
    End Property

    Property RightBindingSrc() As String
        Get
            Return varValue3R
        End Get
        Set(ByVal value As String)
            varValue3R = value
            lblRightText.DataBindings.Add(New Binding("Text", Main.sData, varValue3R, True))
        End Set
    End Property

End Class

Open in new window


By the way, if we can figure out how to get the value selected in the standard dropdown assigned to one of the labels, that would be an acceptable solution as well.
DataBindings-Dropdown.jpg
Visual Basic.NETMicrosoft Visual Studio

Avatar of undefined
Last Comment
Tony Gardner

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chinmay Patel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tony Gardner

ASKER
One would think that these articles are EXACTLY what I'm looking to accomplish, but even after completing all three walk-throughs, I don't feel that I'm much closer to creating the User Control I'm hoping to achieve.

I believe that the major disconnect is that I really don't need to create Properties for the DataBinding since they are already built-in to the label class, so I should be able to simply leverage what's already there for the right-hand label, and then maybe create properties that Get/Set the right and left-hand labels with what's already there.

At this point, the required code for these properties eludes me, but I'm sure I'll figure it out just like everything else I set my mind to solve.

By the way, you mentioned that the Microsoft walk-through was in C#, and for a long time I used to think that ALL of them were in C#, until I found the little gem hidden in the attached image.

I'll need to leave this one open for another day or so until we can achieve the desired result.

TonyC# / VB option in docs.microsoft.com
Tony Gardner

ASKER
I have not had any success creating this User Control, but I didn't want to leave EE hanging, and I also wanted to make sure that Chinmay received some credit for his Microsoft walk-through suggestions. Obviously, if anyone ever figures out how to create a user control similar to what I have explained in the original question, please let me know.

Cheers,
Tony G.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck