Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 279949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:04:33+00:00 2026-05-12T05:04:33+00:00

I have an user control in our library that I need to inherit and

  • 0

I have an user control in our library that I need to inherit and make some update to it. The issue that I’m having right now is that I cannot instantiate the new user control directly. I have to call a method in the library that will create and pass an instance of the user control. Please check sample code below.

I tried to use casting and I got InvalidCastException. I think that’s because the second requires more storage than the first.

Thanks in advance for helping on this.

Namespace ProjectA.Components

    Public Class MainClass

        Public Function CreateCustomControl() As CustomControl
            Dim cc As CustomControl = Activator.CreateInstance(Of CustomControl)()
            Return cc
        End Function

    End Class

    Public Class CustomControl
        Inherits System.Windows.Forms.UserControl
    End Class

End Namespace

Namespace ProjectB

    Public Class ExtendedCustomControl
        Inherits ProjectA.Components.CustomControl
    End Class

    Public Class MainForm
        Inherits System.Windows.Forms.Form

        Private Sub CreateInstance()
            Dim i As New ProjectA.Components.MainClass
            Dim myControl As ExtendedCustomControl = i.CreateCustomControl
            ' InvalidCastException is thrown.
        End Sub

    End Class

End Namespace
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:04:33+00:00Added an answer on May 12, 2026 at 5:04 am

    It’s because you aren’t instantiating an ExtendedCustomControl, you are instantiating a CustomControl. Activator.CreateObject is just creating the base class. You can’t upcast something unless it actually IS of the class you’re casting to.

    Maybe you want CreateCustomControl() to take a System.Type and then pass it into Activator.CreateInstance instead? That way you might be able to make what you want?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an User Control that host another controls like panel, chart controls. Right
I have written a user control for our SharePoint site that builds an HTML
In onw of our application, we have some reports that need very specific margins
I have a user control that is being integrated to the page on run
I have a user control with several buttons, which need to take different actions
I have a web user control, it serves some potentially intensive data calculations and
I have user control in my MVC2 app placed in the Content folder (it's
i have user control, which i render on several views. i want to show
I have a user control bound to appropriate ViewModel. I have a GraphLayout (using
I have this User Control XAML: <Window x:Class=MyProj.Dialog xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:shell=http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell mc:Ignorable=d>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.