User Control cannot access form variables

  • Thread starter Thread starter MikeTI
  • Start date Start date
M

MikeTI

Hi All

I am using VB Net 2008.

I am trying to build a large application with multiple integrated functional
modules like:

- Operation
- Accounting etc.

I have tried to work on a premise where:
- there would be a small root application which will build the basic
environment for the entire application and display the main menu. Variables
that control basic environment would be passed on from one form to another.
- each function module would be a "Windows Forms Control Library".
- when an option is selected from the menu in the root program, the form
would be loaded from the appropriate Windows Forms Control Library (DLL).
- each form has multiple user controls.

NOW:
I am able to load the form and display the first user control. The first
user control hides on demand and a second user control is displayed on the
form. However I am unable to access the controls on the form either from the
first or second user control. Similarly I am unable to access the controls
on the first user control from the second user control.

Questions:
1. Am I doing the right thing or is there any other better way of doing
this.
2. How can I access the controls on the parent from from user controls
(first and second)

Example of Code:
--------------------
Imports AM ' One functional module

Public Class UserClass01

Dim UserControl01s As New UserControl01

Private Sub frmA002_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
UserControl01s .Height = SplitContainer1.Panel2.Height
UserControl01s .Width = SplitContainer1.Panel2.Width
UserControl01s .Parent = Me.SplitContainer1.Panel2
UserControl01s .Dock = DockStyle.Fill
UserControl01s .Show()
End Sub
 
MikeTI said:
Hi All

I am using VB Net 2008.

I am trying to build a large application with multiple integrated
functional modules like:

- Operation
- Accounting etc.

Each one should be an class/object, an independent little machine that
can take care of itself like populating itself from and persisting
itself to a database, along with the ability to interact with other objects.
I have tried to work on a premise where:
- there would be a small root application which will build the basic
environment for the entire application and display the main menu.
Variables that control basic environment would be passed on from one
form to another.

That would be a Control object, a public object, that has all the
properties in it, and the object is passed around.
- each function module would be a "Windows Forms Control Library".
- when an option is selected from the menu in the root program, the form
would be loaded from the appropriate Windows Forms Control Library (DLL).
- each form has multiple user controls.

Each form would have its own public Interface with UI controls on the
Interface using a Model View Presenter and, each UserControl would have
its own public Interface with UI controls and its Model View Presenter.
2. How can I access the controls on the parent from from user controls
(first and second)

You treat the form, controls and user controls as objects, because
that's what they are -- objects, and you can pass them around and make
them interact with each other and interact with your custom objects
(Operations, Accounting, Control etc, etc).

Have you ever heard of design patterns?
 
I am using VB Net 2008.
I am trying to build a large application with multiple integrated
functional modules like:
I am able to load the form and display the first user control. The first
user control hides on demand and a second user control is displayed on
the form. However I am unable to access the controls on the form either
from the first or second user control. Similarly I am unable to access
the controls on the first user control from the second user control.

Questions:
1. Am I doing the right thing or is there any other better way of doing
this.
2. How can I access the controls on the parent from from user controls
(first and second)

Forms and UserControls are classes, just like any other; you just happen
to be able to see them on screen.

If you want to be able to use the properties and methods of class 'A'
from class 'B' then you have to "get" a reference to 'A' into 'B' -
somehow. Here's one way:

Class UserControl1
Inherits UserControl

Sub New()
End Sub

Sub New( ByVal parentForm as Form1 )
m_parent = parentForm

' and from here on, Me.Parent.<whatever>
' gets you access to the form
End Sub

Private ReadOnly Property Parent() as Form1
Get
Return m_parent
End Get
End Property

Private m_parent As Form1 = Nothing

End Class

Class Form1
Inherits Form

Sub Form_Load( ...
Dim uc1 as New UserControl1( Me )
. . .
Me.Controls.Add( uc1 )
uc1.Size = New Size( ...
. . .
End Sub

End Class

OK, I realise that the Forms Designer insists on having a niladic
constructor (a "Sub New" with zero arguments) to work with; you could
pass the reference to the base Form into the UserControl through a
Property instead, if you prefer.

HTH,
Phill W.
 
Back
Top