How to make existing forms deriven from a base form in VB.net - vb.net

I have an existing project in VB.net with many Windows forms.I want to change all forms to deriven from a base form created in a class library.
is there any way to do so?
if i do inheritance with a code like this the designer dose not show the child form and gets error
Class Form1
Inherits BaseFormLibrary.BaseForm

Since you want to change existing forms to inherit another base class you need to change each form that you want to take effect and change what they inherit.
Go to each of your forms designer code class. Inside towards the top you will see the inherits statment. Change what it inherits.

Related

Base class to handle derived class's controls and events

I'm working on 4 winform forms that have 5 common combobox controls. The logic for filling and handling the combobox control events is identical on all 4 of the winform forms. The code is interrelated...so for example, combobox 1 controls filling combobox 2 and fills a few text boxes, combobox 2 affects combobox3...etc.
The reason I'd like to combine the code is that others have changed one or two of the forms without updating the others...so the code is becoming a mess. Unfortunately, the controls aren't located near each other...so I can't make some sort of composite control.
I was thinking of adding the common code in a base class that inherits Form, and then have the 4 forms inherit from the base class but I ran into issues accessing the events in the base class and the combobox code accesses other controls on the forms that aren't in the base class.
What is the best way to accomplish this (within the constraints of not being able to completely rewrite this in MVC, or ripping these forms completely apart, etc.)? Should I use a base class? If so, how do you access the controls in the derived class from the base class and how do you get the base class to respond to the event handlers?
I was thinking I could call MyBase.EventMethod for the derived class's event handler?

Can one move controls around in a form class derived from another form?

I have created a forms which are derived from another form thus:-
Public Class MyForm
' ...etc
End Class
Public Class MyDerivedForm
Inherits MyBaseForm
' ...etc
End Class
Public Class MyOtherDerivedForm
Inherits MyBaseForm
' ...etc
End Class
This works quite nicely and I can add controls to the derived form using the form designer. But I'd like to move some of the inherited controls around a bit on MyDerivedForm without disturbing MyBaseForm or MyOtherDerivedForm. I can't see any way of doing this on the forms designer.
Is it possible to do this (preferably with the designer but with code if necessary)?
This is not a typical VB.NET problem so not so sure what's going on here. You'll get the lock icon on the inherited controls and a grayed-out Properties window for an inherited control when the Modifiers property of the control in the base class is Private. The Winforms designer observes the accessibility of the base class member. Private members can't be messed with. The default value for Modifiers is Friend in VB.NET, Private in C#.
Make it Friend to allow the derived form class to modify the control properties. If the base form class lives in another assembly then Friend isn't good enough, you'll need Public.
The Anchor property can be an issue, but only if you anchor to the right or bottom. The control has a knack for ending up in the wrong spot when the derived form has a different size from the base form if the control is anchored that way. Simply avoided by not anchoring at the right/bottom in the base class and changing the anchor in the derived class.
In my VB.NET WinForms application I have inherited forms and I can just grab the inherited controls in the designer and move them about as I would with non-inherited controls on the form.
However I have noticed two things in the past that stop this. If you change the position of the controls on the base form, or change some of the positioning properties (such as anchor or docking), then this can (but not always) move your inherited controls. Also I couldn't move some of my inherited controls in an earlier version of .NET (2.0 I believe), but I never figured out the cause for that so I had to resort to changing the locations via the property grid.

Non visual inheritance of WinForms with vb.net

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.
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
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.

how to do multiple pages in VB.NET form

I've inherited some VB.Net code that I think needs some restructuring. The project has three forms, each of which is its own Windows Form file that inherits from System.Windows.Forms.Form.
The problem is that all these forms share a common navigation menu bar that does not change as the user switches between forms, and so the original programmer has duplicated the menu code in each of the three files to generate the menu on each one! I figure this can't be right.
To restructure it, I thought I would create a base form that implemented the menu, and then let the other forms inherit from that, but I ran into the problem that Windows forms already inherit from the class mentioned above, and can't inherit from another class.
I noticed I can add an item called "inherited form", but is that the way to go here? The problem of creating multiple screens with a common menu bar has to be incredibly common. Is there one true way to do this? Should I use inherited forms, or should I by have just one base form and make the other screens just plain classes and not forms at all? Or something else I'm not thinking of?
Depending on the specifics; you might want to consider using MDI forms.
Another option I've seen is having the menu/shared toolbars be encapsulated in a UserControl and used where desired.
Make a class that inherits from Form class and add the common menu functionality there. Then make your three forms inherit from this new class instead of Form.

Can I change properties of inherited controls at design time?

I am using visual inheritance and was wondering if there is a way to change the properties of inherited controls at design time, preferably in the form designer. If not, then in the designer code.
I have my control declared as Public in the base class. I can access it in the child form code, but not in the form designer. Is this just not possible?
There are limitations placed within visual studio for visual inheritance. Unfortunately, derived forms\usercontrols cannot modify controls containing collections within the base, namely DataGridViewRows, ToolStrips, ListViewColumns, etc.
Microsoft Bug Report
There are ways around this in certain situations. Create a protected property in the base class that exposes the exact properties of the control you wish to modify (DataGridView.BackgroundColor, or ListView.Columns).
Your base form should be able access this property to change the components needed. I've done this for ListView.Columns, and DataGridView.rows successfully, however ToolStrip.Items would not work.
It seems to work only for certain controls, but not all and I can't understand why. On my base form I have a TabControl that within it is a ComboBox, a ToolStrip, and a DataGridView. All of them are set to Public, but I can only modify the properties of the ComboBox and not the other two controls.
I have no idea why this is.
You need to change your control visibility. Put the control property Modifiers on public and recompile the project and then you can change properties of the inherited control.