troubleshooting Question

Handling DataBindings Properties for Constituent Objects within a User Control

Avatar of Tony Gardner
Tony GardnerFlag for United States of America asked on
Visual Basic.NETMicrosoft Visual Studio
3 Comments1 Solution110 ViewsLast Modified:
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

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
ASKER CERTIFIED SOLUTION
Chinmay Patel
Chief Technology Ninja
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros