0

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

  • See the Form template described here: [How can I draw a rounded rectangle as the border for a rounded Form?](https://stackoverflow.com/a/56533229/7444103) and the notes about the a derived Form initialization sequence here: [How to get the size of an inherited form in the base form?](https://stackoverflow.com/a/55760332/7444103). – Jimi Sep 05 '20 at 12:37
  • To add Controls (and related events) to Form templates loaded from a Library, see here: [Best practices on load a form inside a main form, both residing in an external DLL assembly](https://stackoverflow.com/a/62658319/7444103) (disregard the *Best practices* thing, I cannot say what is the *best practice*). – Jimi Sep 05 '20 at 12:56
  • Does this answer your question? [How can I draw a rounded rectangle as the border for a rounded Form?](https://stackoverflow.com/questions/56519392/how-can-i-draw-a-rounded-rectangle-as-the-border-for-a-rounded-form) – Daniel Widdis Sep 06 '20 at 17:56

1 Answers1

2

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
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • The title bar is loading correctly, thank you. Do you know why the following is happening? When the form loads I can see all of the placeholders (locations) for the form's pictures and labels without those controls loading, and I can see four out of five of the buttons which load in the mouse hover color. The form doesn't display correctly until I physically move the form. And then, once I move the mouse again most of the buttons change back to the mouse hover color. I appreciate any and all help. Thank you – Michele H. Sep 05 '20 at 14:13
  • @MicheleH., it likely relates to your custom painting but it's hard to say for sure. I'd suggest that you post a new question on that topic and include all the information specific to that issue. You should provide the simplest example you can that demonstrates the issue, rather than including all your existing code and probably obfuscating the issue. – jmcilhinney Sep 05 '20 at 17:24