vb.netrepositoryreusabilitypainteventcustom-titlebar

Is it possible to have one file that controls the look and feel of my custom title bar that I can access from any form?


I have an application with 60+ forms and instead of having 60+ copies of the same code in each form, I would like to have one instance of the code that is executed as any form is loading.

The subs are standard:
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Protected Overrides Sub WndProc(ByRef m As Message)

I've made countless attempts using classes and modules, but am only able to get the forms that are hardcoded to work as desired.

I have triggered the OnPaint event residing in TitleBar (a non-form class), but I am unable to get TitleBar to apply any customizations to any form. Is there anyway I can circumvent the values in TitleBar's Me and MyBase, and assign them the calling form's values? Or can I get TitleBar to loan out its resources? Any help would be greatly appreciated. BTW, is there a word or phrase for what I'm trying to accomplish?

This has been a long round of trial and error.
Thank you


Solution

  • Create a form as normal and add that common code. In all your other forms, instead of inheriting the standard Form class, inherit the class you just created that contains the common code. That's how inheritance in OOP works.

    If you're inheriting a form from a referenced library, you can select Inherited Form in the Add New Item dialogue but that doesn't work to inherit forms in the same project. At least, it doesn't work in an application project, although I haven't tested in a library project. The alternative is to add the form as normal, click the Show All Files button on the toolbar in the Solution Explorer, expand the node for your form, double-click the designer code file to open it and then manually edit the type that your form inherits from, e.g. change this:

    Partial Class Form2
        Inherits System.Windows.Forms.Form
    

    to this:

    Partial Class Form2
        Inherits Form1