473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

User Controls problem. PLEASE HELP

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
Nov 20 '05 #1
2 1174

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

Nov 20 '05 #2
This is good

"Keith Fink (MS)" <ke****@onlinemicrosoft.com> wrote in message
news:jq**************@cpmsftngxa06.phx.gbl...

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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BBM | last post by:
Hi, I have been developing with C# User Controls and occasionally have a problem where I "lose" a control from the design surface of the User Control. The controls that I am using to build my...
13
by: Lloyd Sheen | last post by:
I have now spent 5 hours on google/msdn looking for something useful in the creation of user controls for asp.net. The VS 2003 has very limited support for things such as absolute positioning of...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
4
by: John | last post by:
Hi all, I have posted this type of question quite a few times but to date, no-one has actually been able to provide me with a solution. I really need to understand how to do this properly. My...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
2
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
5
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.