Windows forms and user controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maya

    Windows forms and user controls

    Hello everyone,

    I have a windows form that has 2 child usercontrols i created and added
    inside, control1 has a label that displays some text based on
    activities i do in control2, my question is how do i update the label
    text inside control1 from control2? previously i got around this issue
    by using properties and static variables but this doesn't work with
    form controls such as the label i have.

    Any solution would be highly appreciated.

    Cheers,

    Maya.

  • Eric Giles

    #2
    RE: Windows forms and user controls

    The problem you're going to have is that the two controls do not have a
    reference to the other. This means that one control on the form needs to know
    how to find the other.

    Is there a need for the controls to be separated? For instance, do you need
    to have the felxibility to place them in different locations on the form?

    It depends on the requirements that you have. For instance, if this was
    something that you were going to use once on one form, you might be better
    off raising an event from one usercontrol and then writing the code into the
    form's event handler to manipulate the second control.

    If you were going to use these controls often (on numerous forms) or
    multiple sets of these controls on any given form, you might be better off
    providing a property within the second control that you could access at
    design-time (via the properties window) that allowed you to pick the control
    that you wanted to bind it to at runtime. You would do this by creating a
    class that inherits from ControlDesigner and then link it to your actual
    usercontrol by adding Attributes to your control's class.

    For example:

    <Designer(GetTy pe(MyControlDes igner))> _
    Public Class UserControl1
    Inherits System.Windows. Forms.UserContr ol
    ' Control's inner workings
    End Class

    Friend Class MyControlDesign er
    Inherits ControlDesigner
    ' Control Designer's inner workings
    End Class

    Have a search around for information on Control Designers if this idea seems
    interesting.

    Hope this helps,

    -Eric



    "Maya" wrote:
    [color=blue]
    > Hello everyone,
    >
    > I have a windows form that has 2 child usercontrols i created and added
    > inside, control1 has a label that displays some text based on
    > activities i do in control2, my question is how do i update the label
    > text inside control1 from control2? previously i got around this issue
    > by using properties and static variables but this doesn't work with
    > form controls such as the label i have.
    >
    > Any solution would be highly appreciated.
    >
    > Cheers,
    >
    > Maya.
    >
    >[/color]

    Comment

    Working...