User Controls problem. PLEASE HELP

  • Thread starter Thread starter Ruslan Shlain
  • Start date Start date
R

Ruslan Shlain

Hello Everyone,
I have a solution that consists of two projects. The Win user control
library and win forms project. The purpose of the windows forms project is
to be a container for everything that goes on the user control. My question
is if I have a win form and I drag and drop user control on it and I have a
button on that user control on click of which I have to show another win
form from the Windows form project that will hold different user control.
And also how do I pass values back and forth between the forms.

This architecture is mandated to me.

Thanks
 
Ruslan,

It sounds like you just need to expose a property of the UserControl that
is accessible to the Winform so it can pass information in. Or, you can
overload the CTOR of the control so that you pass the arguments in to the
control. Of course, then you have to modify the InitializeComponent of the
parent Winform code to pass in the appropriate arguments.

Regarding the ability to open a form that exists in the Winforms project,
your best bet is to create an instance of the form you need from the
Winforms app, and then pass a reference to it into your UserControl via its
CTOR or a public property. Then your button can call .Show or whatever.

Another way to do it would be to use reflection. Here is a sample using
reflection where you pass the form name into a property of the UserControl.
In the button click, we use reflection to instantiate the form and show it.


Private strFormName As String
Public Property FormName() As String
Get
Return strFormName
End Get
Set(ByVal Value As String)
strFormName = Value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles _
Button1.Click

Dim parentType As Type
Dim parentAsy As [Assembly]
Dim frm As Form
Dim obj As Object

parentType = Me.Parent.GetType
parentAsy = parentType.Assembly
Try
obj = parentAsy.CreateInstance(parentAsy.GetName.Name & "." &
strFormName)
frm = CType(obj, Form)
frm.Show()
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub

Hope this helps!

Sincerely,

Keith Fink
Microsoft Developer Support
 
Back
Top