Microsoft KB Archive/814319

= &quot;ControlType is not accessible in this context because it is Friend&quot; error in an inherited Windows Form application that inherits the form in another application =

Article ID: 814319

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.



SYMPTOMS
You create an inherited Windows form in an application by inheriting the form in another application, such as a class library. Next, you add the code that handles the events of the inherited form controls. When you build the project, you may receive the following error message:

'ClassLibraryName.FormName.Friend Dim WithEvents ControlName As System.Windows.Forms.ControlType' is not accessible in this context because it is 'Friend'



CAUSE
This problem occurs because the control in the referenced class library is declared with Friend modifiers. You cannot inherit Friend variables from one assembly to another. Therefore, you cannot add handlers for Friend controls from another library in the inherited form.



WORKAROUND
To work around this problem, set the access modifiers of the control as Public or Protected in the base form. To do this, follow these steps:
 * 1) Open the class library project in Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005.
 * 2) Right-click Control on the form, and then click Properties.
 * 3) In the Properties window, locate the Modifiers property.
 * 4) From the Modifiers list, click to select Public or Protected.
 * 5) On the Build menu, click Build Solution.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 In Visual Studio .NET or Visual Studio 2005, create a new class library by using Visual Basic .NET, Visual Basic 2005, Visual C# 2005, or Visual C# .NET.

By default, ClassLibrary1 is created. Right-click Class1, and then click Delete. Right-click ClassLibrary1, point to Add, and then click Add Windows Form.

By default, Form1 is created. From the toolbox, drag the Timer control to Form1.

Timer1 is created. Right-click Timer1, and then click Properties.</li> In the Properties window, locate the Modifiers property.

By default, Modifiers is set to Friend.</li> On the Build menu, click Build Solution.</li> On the File menu, point to Add Project, and then click New Project.</li> Under Project types, select Visual Basic projects or Visual C# Projects.

Note In Visual Studio 2005, select Visual Basic or Visual C#.</li> Under Templates, select Windows Application.

By default, Form1 is created.</li> Right-click Form1, and then click Delete.</li> Right-click Project Name, point to Add, and then click Add Inherited Form.

By default, Form1 is created.</li> In WindowsApplication1, Right-click Form1, and then click View Code.</li>  Add the following code for the Form1 class:

Visual Basic.NET or Visual Basic 2005 code Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

End Sub Visual C# .NET or Visual C# 2005 code

Add the following code to Form1_Load event // Event handler for timer1 control timer1.Tick += new System.EventHandler(timer1_Tick);

Add the following code after Form1_Load event code: private void timer1_Tick(object sender, System.EventArgs e)     { } </li> On the Build menu, click Build Solution.

You receive the error message in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">