how to do multiple pages in VB.NET form - vb.net

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.

Related

Unable to Move Control on Windows Form Designer [duplicate]

Setup:
I have created a Form that I wish to have serve as the base from which I will inherit other forms. This base form serves as a "template" of sorts, but it also provides a good deal of functionality related to the structure, as well as the interrelation of all of the controls provided.
A primer for the images that follow... The top info-colored bar is a custom control inherited from ToolStrip. The bottom strip is another custom, again inherited from ToolStrip. The left white block is a TreeView and the right block is a TabControl (having deleted all TabPages from it...I intend for these to be added in the inherited forms).
Image of base form in designer:
Image of inherited form in designer:
Clearly, the only difference is that when I open the inherited form, I get a little box icon superimposed over each control, and when I click them, I get the padlock telling me I cannot edit.
The problems:
All controls on the inherited form are locked. I have researched the issue of visual inheritance, and as far as I can tell, I'm not using any controls that expressly do not support it, as this link suggests there are. In this Q&A, Hans suggests changing the modifier on those controls, which I have done. In fact, I tried both Public and Protected, all to no good result.
I am stumped.
This is a technical restriction in the designer, it is specific to the SplitContainer control you are using. And some other ones. The trouble-maker is the ISupportInitialize interface.
Controls use this interface when they can't afford the properties of the control to be assigned in an arbitrary order. The designer helps when it sees that the control implements this interface, it calls the BeginInit() method when it starts assigning properties, EndInit() when it is done. The control uses these methods to delay the side-effect of property assignments, the EndInit() method makes them effective. Important for SplitContainer, the minimum sizes of the panels also affect the splitter position.
Perhaps you can see the rub, the InitializeComponent() method in the base form class has already called ISupportInitialize.EndInit(). So modifying properties again in the derived form class is unlikely to turn out well. The designer protects the control from this by locking it.
Very inconvenient, there is no simple workaround. If modifying the SplitContainer in the derived form class is a hard requirement then you'll have to give up on inheriting it or write the code by hand in the derived class constructor.

How to make existing forms deriven from a base form in 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.

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?

Creating your own component in vb.net 2008

How do you create your own custom component for vb.net 2008? I want it to simply output to a .dll, not a whole winforms app.
So, here is what I have done so far:
Made a class library project
Added a custom control object
Confused myself badly
Googled it, to no avail
How can I control the component? For example, I want my component to not have a visible design view, I want it to stay below like the stopwatch component and the notifyicon component and such, it is not something to be designed. Then, how do I edit the possible properties a user can control, and make them effect the end result? What do I place the code which powers the component on? The class library file, or something else?
Thanks for your help! I have a whole set of components I am going to create, this will get me going much faster than trial and error.
I think you may want to check some walkthrough on how to create components. Such as this one: Walkthrough: Authoring a Component with Visual Basic. Once you are done with that one, there are more walkthroughs on various related topics, such as how to use design-time support, implementing designers and so on.
OK... This is a really abbreviated example. You should start by basing yous off of an existing .net component.
Public Class MyControl
Inherits DataGridView
'...add your properties/functionality...'
End Class
Then compile the DLL, and add it as a reference to whatever project you are working on. Once added, you can add the controls in the DLL into your toolbox.
This has more instructions on how to modify a UserControl (slightly different from the one above, but it explains well. This is a general explanation.
#comments -
Yes, there, are things that will do what you want. Start with a class that inherits Form instead of DataGridView in the example I gave you, and the changes described in the links provided.
"Your properties and functionality" is whatever you want to do that the base control does not do.

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.