2

I have two Forms, which have similar Functionality (i.e. an amount of similar Controls) but complete different Layout. So the normal inheritedForm (which VS2010 provides) wont work here.

I have tried following:

Public Class BaseForm
  Inherits System.Windows.Forms.Form

  Friend WithEvents Button1 As System.Windows.Forms.Button
End Class  

And in Form1.Designer.vb:

Partial Class Form1
    Inherits BaseForm
    ...
    <all Designer generated Code>
    ...

    ' Friend WithEvents Button1 As System.Windows.Forms.Button <- remove this Line
End Class

If i compile/execute this, the test form works as expected; But now I am unable to design the form any longer.

If I switch to Design-Mode, it says:

 The variable 'Button1' is either undeclared or was never assigned. 

http://imgur.com/ieWvf

So it looks like, the Designer only tries to guess how the Form looks like by inspecting the top-most Class, without compiling the full inheritance-Tree...

Does anyone know a workaround for this?

Thx, Daniel

5
  • Do not edit designer generated code. Sep 21, 2011 at 8:27
  • What would than be the best-Practice in this case? If i want to write code, which refers to this forms, but it should have not care with which visual implementation it interacts.
    – DanielD
    Sep 21, 2011 at 8:43
  • It is completely unclear to me what you are trying to achieve. If a base form is useless to you then don't inherit from it. Sep 21, 2011 at 8:49
  • Functionality and exposed Controls are the same. Or at least I have a same core functionality across different forms, but the visual design of the Forms is different. And i'd like to be able to design the Forms with the Designer, rather (re)placing the controls by Code (which I would have to do, if I use the inheritedForm)
    – DanielD
    Sep 21, 2011 at 10:21
  • You can do some editing of the inherited controls. Simple property assinments. But you can't remove one, there is no option to un-inherit a control. Set its Visible property to False or Dispose() it in the form constructor. Sep 21, 2011 at 11:36

1 Answer 1

0

I have a feeling you have some existing controls on the base form which you want to bring to the inherited form and then add more functionality of your own. Remember one thing, inheritance in VB.NET is by default set to shadowing, so you can redefine code in your inherited page for the same button to do a different function.

However, I think you forgot the Overridable keyword when declaring the form elements in the parent form. This will help the compiler know that the form can be overridden and allow you to define custom functionality.

Remember, all controls and pages in VB.NET are classes and whatever we learned during OOP classes in college apply here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.