circular references between usercontrols and 'main' form in vb6->.net upgrade

in upgrading from vb6 to vs2005 user controls are introducing a circular reference problem in the following way.

In the vb6 solution, user controls could access methods in the 'Main Form' upon which the user controls are used.

In VS2005, I create a separate Windows Control Library Project for each User Control. For composite controls I 'reference' any other controls as needed.

However, there are controls which call methods/functions on the 'Main form' within a Windows Application project. That project includes the references to all the user controls ( which show up in the ToolBox.) Circular references have been introduced which didn't exist before. The Mainform references the User Control. The User Control calls MainForm methods.

The functions which are being called on the Main form contain references to controls on the mainform so I can't simply remove all those functions without affecting the app.

Anyone been there Have any ideas

Thank you. -Greg



Answer this question

circular references between usercontrols and 'main' form in vb6->.net upgrade

  • Wilk06

    Thank you Scott.

    What if I have a function on the Mainform I am calling from the control (with the current codebase)

    eg. MainForm.RedrawGraphics((TYPE_3D))

    How would I do this using the Eventing mechanism just mentioned

    Thanks again.

    -greg


  • Tom Frey

    In your control, declare something like:

    Public Event MyEvent(sender as object, e as eventargs)

    Then, when you want to communicate with the form, raise the event as follows:

    RaiseEvent MyEvent(Me, EventArgs.Empty)

    On the form, you can handle this event:

    Public Sub HandlesMyEvent(sender as Object, e as EventArgs) Handles MyControl.MyEvent
    'Code on the form to handle the event
    End Sub

    This way, the control doesn't need to know anything specific about the form, and doesn't need to reference the project that the form is in.


  • Mile Petrov

    Yes, this is a .NET specific problem. You can't compile the control library assembly because it needs the assembly containing the form. You can't compile the form assembly because it needs the control library assembly. The solution is to put your controls in the same assembly as the form. Or cleanup your code and let the controls communicate with the form through events.



  • Lucian Wang

    In the control, you would have code like this:

    Public Event DataChanged(sender as object, e as EventArgs)

    Private Sub Something() ' Some routine in your control
    RaiseEvent DataChanged(me, EventArgs.Empty)
    End Sub

    Then, In your form you have something like

    Private Sub MyControl_DataChanged(sender as Object, e as EventArgs) Handles MyControl.DataChanged
    me.RedrawGraphics(TYPE_3D)
    End Sub



  • dreadjr

    Thank you Scott. Now I can decouple this currently monolithic app. I can put them in their own project and reference them in the project containing the mainform. appreciatively -greg
  • nicromi

    Thank you so much. Greatly appreciated.

    What I had to do to build/test the user controls was to create a separate control library project.
    But the control really doesn't have to go into the toolbox; so no need to compile a separate dll.
    I added the control programmatically here;

    Public Class Form1
    Public ctrl_TrackControl As TrackControl
    Private Sub Form1_Load(xxxx) Handles Me.Load
    ctrl_TrackControl = New TrackControl
    ctrl_TrackControl.Visible = True
    Me.Controls.Add(Me.ctrl_TrackControl)
    End Sub
    End Class

    I am really interested in the code cleanup approach.
    What do you mean by letting the controls communicate with the form through events.
    I'm trying to think about/understand that at a high level.
    I'm thinking of some general notion of decoupling the direct calls to the main form by registering an event on the control and registering that event/handler on the Main Form
    Thanks, -Greg


  • circular references between usercontrols and 'main' form in vb6->.net upgrade