"ControlType is not accessible in this context because it is Friend" error in an inherited Windows Form application that inherits the form in another application (814319)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. 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.
  2. Right-click Class1, and then click Delete.
  3. Right-click ClassLibrary1, point to Add, and then click Add Windows Form.

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

    Timer1 is created.
  5. Right-click Timer1, and then click Properties.
  6. In the Properties window, locate the Modifiers property.

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

    Note In Visual Studio 2005, select Visual Basic or Visual C#.
  10. Under Templates, select Windows Application.

    By default, Form1 is created.
  11. Right-click Form1, and then click Delete.
  12. Right-click Project Name, point to Add, and then click Add Inherited Form.

    By default, Form1 is created.
  13. In WindowsApplication1, Right-click Form1, and then click View Code.
  14. 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)
          {
      }
  15. On the Build menu, click Build Solution.

    You receive the error message in the "Symptoms" section of this article.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

307222 HOW TO: Use Inheritance in Visual Basic .NET



For more information, see your .NET Framework SDK Documentation or visit the following MSDN Web site:

Windows Forms Inheritance
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconforminheritance.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbWebForms kbForms kbInheritance kbControl kbProperties kbEvent kbImport kbprb KB814319 kbAudDeveloper