Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - User control sharing between several projects

Reply
 
Old 04-22-2008   #1 (permalink)
SalamElias


 
 

User control sharing between several projects

Hi, I have a user control which I like to use in several projects (winforms)
in the same solution. Inside lets say Winapp1 , When adding a reference to
this control, I can dynamically or statically create it and when running the
application it is correctly drawn on the form. But when I take out the
reference to this control in Winapp1, reference it in a utility project,
then reference the utility project in Winapp1 or Winapp2, instantiate the
utility.mControl, it doesn't get drawn on the form and I get no errors.
I did a quick check, I added 2 instances of the user control referenced in
the utility project to my form, using
messagebox.show(myform.controls.count), it displays 2 which means that
controls are added but not being able to be drawn. Of course I double checked
properties such as visible = true..............
Thanks in advance for any help

My System SpecsSystem Spec
Old 04-22-2008   #2 (permalink)
Jack Jackson


 
 

Re: User control sharing between several projects

On Tue, 22 Apr 2008 05:01:00 -0700, SalamElias
<eliassal@xxxxxx> wrote:
Quote:

>Hi, I have a user control which I like to use in several projects (winforms)
>in the same solution. Inside lets say Winapp1 , When adding a reference to
>this control, I can dynamically or statically create it and when running the
>application it is correctly drawn on the form. But when I take out the
>reference to this control in Winapp1, reference it in a utility project,
>then reference the utility project in Winapp1 or Winapp2, instantiate the
>utility.mControl, it doesn't get drawn on the form and I get no errors.
>I did a quick check, I added 2 instances of the user control referenced in
>the utility project to my form, using
>messagebox.show(myform.controls.count), it displays 2 which means that
>controls are added but not being able to be drawn. Of course I double checked
>properties such as visible = true..............
>Thanks in advance for any help
When you instantiate the control, are you adding it to its parent's
Control collection?
My System SpecsSystem Spec
Old 04-22-2008   #3 (permalink)
Linda Liu[MSFT]


 
 

RE: User control sharing between several projects

Hi Salam,

Based on my understanding, you have Control Library project which contains
a UserControl and a Class Library project and some Windows Application
projects in a solution.

If you add add a reference to the Control Library project in the Class
Library project and then add a reference to the Class Library project in
the Windows Application projects and then add the UserControl on the form,
the UserControl doesn't appear on the form when the application is run.

If I'm off base, please feel free to let me know.

I performed a test based on your description, but didn't reproduce the
problem on my side. The steps of my test are as follows:

1. Create a solution and add a Control Library project and a Class Library
project and a Windows Application project to the solution. Add a
UserControl in the Control Library project.

2. Derive the Class1 in the Class Library project from the UserControl in
the Control Library project:
namespace ClassLibrary1
{
public class Class1:WindowsControlLibrary1.UserControl1
{
}
}

3. Build the solution.

4. Drag the UserControl from Toolbox and drop it onto the form in the
Windows Application project. A reference to the Control Library project is
added to the Windows Application project automatically. Build and run the
application and the UserControl appears on the form.

5. Remove the reference to the Control Library project from the Windows
Application project and add a reference to the Class Library project in the
Windows Application project.

6. Add the following lines of code in the Form's Load event handler to add
the UserControl on the form dynamically:
private void Form1_Load(object sender, EventArgs e)
{
ClassLibrary1.Class1 mycontrol = new ClassLibrary1.Class1();
this.Controls.Add(mycontrol);
}

7. Build the solution. An compilation error occurs:
The 'WindowsControlLibrary1.UserControl1' is defined in an assembly that is
not referenced. You must add a reference to the assembly "...".

Is there any difference between your solution and mine?

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


My System SpecsSystem Spec
Old 04-23-2008   #4 (permalink)
SalamElias


 
 

RE: User control sharing between several projects

yes, you are almost correct, one difference is my user control is compiled as
a dll and referenced in my utility class library as follows.

Public Class Gauge
Inherits AGauge.AGauge

Then in my windows app I do
Dim Gauge1 Gauge = New Gauge()


but I don't get a compiltaion error and even as I said my message box
indicates that there is a new control added to the form
"Linda Liu[MSFT]" wrote:
Quote:

> Hi Salam,
>
> Based on my understanding, you have Control Library project which contains
> a UserControl and a Class Library project and some Windows Application
> projects in a solution.
>
> If you add add a reference to the Control Library project in the Class
> Library project and then add a reference to the Class Library project in
> the Windows Application projects and then add the UserControl on the form,
> the UserControl doesn't appear on the form when the application is run.
>
> If I'm off base, please feel free to let me know.
>
> I performed a test based on your description, but didn't reproduce the
> problem on my side. The steps of my test are as follows:
>
> 1. Create a solution and add a Control Library project and a Class Library
> project and a Windows Application project to the solution. Add a
> UserControl in the Control Library project.
>
> 2. Derive the Class1 in the Class Library project from the UserControl in
> the Control Library project:
> namespace ClassLibrary1
> {
> public class Class1:WindowsControlLibrary1.UserControl1
> {
> }
> }
>
> 3. Build the solution.
>
> 4. Drag the UserControl from Toolbox and drop it onto the form in the
> Windows Application project. A reference to the Control Library project is
> added to the Windows Application project automatically. Build and run the
> application and the UserControl appears on the form.
>
> 5. Remove the reference to the Control Library project from the Windows
> Application project and add a reference to the Class Library project in the
> Windows Application project.
>
> 6. Add the following lines of code in the Form's Load event handler to add
> the UserControl on the form dynamically:
> private void Form1_Load(object sender, EventArgs e)
> {
> ClassLibrary1.Class1 mycontrol = new ClassLibrary1.Class1();
> this.Controls.Add(mycontrol);
> }
>
> 7. Build the solution. An compilation error occurs:
> The 'WindowsControlLibrary1.UserControl1' is defined in an assembly that is
> not referenced. You must add a reference to the assembly "...".
>
> Is there any difference between your solution and mine?
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscripti...ult.aspx#notif
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscripti...t/default.aspx.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
My System SpecsSystem Spec
Old 04-24-2008   #5 (permalink)
Linda Liu[MSFT]


 
 

RE: User control sharing between several projects

Hi Salam,

Thank you for your reply!

Since I couldn't reproduce the problem on my side, it would be better if
you could reproduce the problem in a simple solution and send it to me. To
get my actual email address, remove 'online' from my displayed email
address.

I look forward to your reply!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Old 04-25-2008   #6 (permalink)
Linda Liu[MSFT]


 
 

RE: User control sharing between several projects

Hi Salam,

Thank you for your sample solution!

I run the sample application and did see the problem.

After looking at the source code, I find the reason that causes the
problem. You create an instance of the AGauge.AGauge class inside the Gauge
class and initialize this instance in the constructor, but the Gauge class
itself is not initialized at all. Then you add an instance of the Gauge in
a form's Controls collection rather than the nested AGauge control within
the Gauge instance, so nothing appears on the form.

Change the code in the sub New within the Gauge class as follow can solve
the problem:
Public Class Gauge
Inherits AGauge.AGauge

Public Sub New(ByVal gaugeName As String, ByVal gaugeCaption As
String)
_AGauge = New AGauge.AGauge

' replace "_AGauge" with "Me"
Me.BaseArcColor = System.Drawing.Color.Gray
Me.BaseArcRadius = 80
Me.BaseArcStart = 135
Me.BaseArcSweep = 270
...
End Sub
...
End Class

In addition, I don't think it's necessary to create an instance of the
AGauge.AGauge class within the Gauge class because Gauge is a kind of
AGauge.AGauge already.

BTW, in my initial test to try to reproduce the problem, I created C#
projects. As I said in my first reply, the compilation fails if I only add
a reference to the Class Library project in the Windows Application
project. But it's not true in VB.NET projects. So it seems that the
mechanism of C# compiler is a little different from that of VB.NET compiler.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
dont know user account control password and no administrator user General Discussion
Control Panel\Network and Sharing Center Vista networking & sharing
Control the Maximum allowed User Sessions in Fast User Switching Vista General
Sharing internet connection with better control over who uses it? Vista networking & sharing
does user account control have to be on when sharing files Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46