• 0

[C#] Is it possible to override the Form class?


Question

Sorry, forgot to say in the topic title that this is C#.

Let me explain what I want to do...

We all know that with Windows Vista brought a new UI font, Segoe UI, but XP still uses Tahoma as UI font. This doesn't help us programmers. I've searched in Google and what I found was to use SystemFonts.MessageBoxFont as the Form's font because all other controls Font properties will inherit from it.

Now, I could do this.font = SystemFonts.MessageBoxFont; before InitializeComponent(); on every single form I use but that will have 2 problems:

1) I'll have to repeat this line for every form.

2) The designer will not be updated accordingly.

If I moved this.font = SystemFonts.MessageBoxFont; into InitializeComponent() itself, then, the designer would be updated but I still would need to do it manually for every form.

My first solution:

Create a class (MyForm, for instance) that inherits from Form and change the properties I need in it's constructor. Then, every form I use in my application must be inherited from MyForm instead of Form. This is simpler than to place the line above in every InitializeComponent(), although, I still have to manually change every from to inherit from MyForm instead of Form.

To sum this up... I'm looking for a way (if possible) to change some default properties of Form without creating a new class that inherits from it, which allows me to make all the needed properties changes in one place and all my forms will still inherit from Form (as they are by default).

Is this possible?

Edited by Nazgulled

15 answers to this question

Recommended Posts

  • 0

What he means is create a class with static methods that you use to create instances of your forms:

public static class FormFactory
{
	public static TForm CreateForm<TForm>() where TForm : Form
	{
		TForm form = Activator.CreateInstance<TForm>();
		form.Font = SystemFonts.MessageBoxFont;

		return form;
	}
}

and then you would use it as such:

Form1 myForm = FormFactory.CreateForm<Form1>();

  • 0

Thanks for the suggestion and the whole code but that's almost like that I'm doing now... It's a different way of doing what I want but it's basically the same thing, what I mean is, I'll still need to manually create an instance for every form.

I may be looking for something that doesn't exist or it's not possible, but that's why I'm asking cause one doesn't know everything ;).

  • 0
  Nazgulled said:
Thanks for the suggestion and the whole code but that's almost like that I'm doing now... It's a different way of doing what I want but it's basically the same thing, what I mean is, I'll still need to manually create an instance for every form.

I may be looking for something that doesn't exist or it's not possible, but that's why I'm asking cause one doesn't know everything ;) .

  Quote
To sum this up... I'm looking for a way (if possible) to change some default properties of Form without creating a new class that inherits from it, which allows me to make all the needed properties changes in one place and all my forms will still inherit from Form (as they are by default).

Is this possible?

I'm confused... The method Antaris illustrated allows you to modify all your forms in one place, and have them all keep Form as their base. I don't know what else you're expecting it to do.

  • 0

Nazgulled, I think you might be getting confused with what is actually going on with your Form types.

Although the designer allows you to (as its name suggests) design the form, the form is still instantiated manually in code, its just that the program generates the code to do so. The factory method I illustrated could be used with your designed forms too. Just change the line of code that usually says:

Application.Run(new Form1());

which is generally found in the generated Program.cs, to this:

Application.Run(FormFactory.CreateForm<Form1>;());

All you are doing is using the factory method to apply any property changes ahead of the instance being returned. It doesn't matter if you use the designer, the Form is still being declared manually.

  • 0

But your method is not very different from my own (inheriting from a sub classed form) and that's not what I was looking for...

If I have 100 forms I'll have to do 100 modifications either using your method or mine and that's what I was looking to avoid... I was looking for a one time global change only, if there was one...

What I'm saying is, using my method, I'll have to inherit all my forms from the sub classed form. Using your method, I'll have to use the FormFactory for every form but there are the type of things I was looking to avoid.

Maybe that's not possible at all, but like I said, I don't know everything, so I just asked...

  • 0

Well, if thats the way you want to do it, do the following:

public class SubclassedForm : System.Windows.Forms.Form
{
	protected override void OnLoad(EventArgs e)
	{		
		base.OnLoad(e);
		this.Font = SystemFonts.MessageBoxFont;
	}
}

What you are doing is subclassing the form and overriding the OnLoad method (this is not the OnLoad event, but the method that will call any event subscribers). What will happen, is that any class that inherits from SubclassedForm will call the OnLoad method (unless the inheriting form overrides the OnLoad method itself, in which case it will need to make a call to base.OnLoad(e)).

To be honest, my personal preference is to go with the Factory pattern, as I believe it is much more flexible.

  • 0

You misunderstood the point... I already knew how to do it with inheritance but that's not what I want. Both solutions force me to do lots of code changes (depending on the number of forms) but that's not what I want. I was looking for a way to do one time change only, without needing to change their base class or how they are initialized.

Like I said, I'm probably looking for something that's not possible so just forget about it. Cause I don't know how to explain myself better than this.

Thanks for all your time guys.

  • 0

Its not that I misunderstood the point, the fact is there isn't a one time change. The Form type is sealed. The Designer type is sealed. VS uses that designer to generate code. It won't automatically set the font, because this is inherited from the base classes. You either need to inherit from the Form type, or follow the factory pattern and set your properties in the one place.

  • 0
  Nazgulled said:
You misunderstood the point... I already knew how to do it with inheritance but that's not what I want. Both solutions force me to do lots of code changes (depending on the number of forms) but that's not what I want. I was looking for a way to do one time change only, without needing to change their base class or how they are initialized.

Like I said, I'm probably looking for something that's not possible so just forget about it. Cause I don't know how to explain myself better than this.

Thanks for all your time guys.

If you have a refactoring tool, like Resharper, you should be able to accomplish the changes pretty easily, but I understand your apprehension. It's a lot of work manually, and a lot of regressions that could be introduced.

Good luck. :)

  • 0
  Antaris said:
Its not that I misunderstood the point, the fact is there isn't a one time change. The Form type is sealed. The Designer type is sealed. VS uses that designer to generate code. It won't automatically set the font, because this is inherited from the base classes. You either need to inherit from the Form type, or follow the factory pattern and set your properties in the one place.

That's exactly what I was asking :)

If that's how things works, I'm ok with it, I just wanted to know if it was or it wasn't possible.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Apple introduces macOS Tahoe with a new Phone app, revamped Spotlight, and more by David Uzondu The speculation can finally end, as the rumors about the name turned out to be completely true. At its Worldwide Developers Conference today, Apple officially christened the next version of its desktop operating system macOS Tahoe. This release, part of the new "26" family of operating systems like iOS 26, is bringing more than just a new name to the table. The most significant and immediate change is a system-wide visual redesign Apple is calling "Liquid Glass." Let's talk about this new design, because it is the first thing you will notice. Apple is taking the translucent, layered look from its visionOS and bringing it everywhere. It is a fundamental change to how macOS looks and feels, which some Neowin readers are definitely not a fan of. Sidebars, toolbars, and menus all have this frosted glass effect, where their color and texture shift based on the window or wallpaper behind them. The menu bar is now completely transparent, and Apple is also adding more customization. You can now change the color of folders, which is a feature people have wanted for nearly forever, and add little symbols or emojis to them. The bigger story for day-to-day use might be how much tighter the Mac and iPhone are becoming. Last year, macOS Sequoia saw the release of apps like the dedicated Passwords app, and this year, with Tahoe, we get a full-blown Phone app on the Mac. It is not just for getting call notifications anymore. You can see your recent calls, check your voicemail, and access your contacts list just like you would on your iPhone. New features like Call Screening, which can figure out who is calling before you answer, are also included. Live Activities from your iPhone, like the status of an Uber ride or a food delivery, will now show up right in your Mac's menu bar. Spotlight search is also getting a massive update. For years, Spotlight has been a simple tool for finding files and launching apps. Now, Apple is trying to turn it into an action center. You will be able to perform tasks directly from the search results, like creating a new note or sending an email, without ever opening the corresponding application. The search results themselves are supposed to be smarter and are no longer separated into rigid categories. Everything just shows up in one big list, ranked by what the system thinks is most relevant to you. Despite Apple's ongoing AI challenges, Apple Intelligence is getting new abilities. The AI features introduced last year are being expanded. A new Live Translation feature can translate text in Messages or audio during a phone call or FaceTime, all on the device, to maintain privacy. The Shortcuts app is also getting more powerful. You can now build automations that tap directly into Apple's AI models to do more complex tasks, like summarizing an audio recording of a lecture and comparing it to your typed notes. And yes, Apple is still trying to make gaming on the Mac a serious thing. A new dedicated app called Apple Games is being introduced. It acts as a central library for all your games, similar to launchers you might see on a PC. It also includes a new Game Overlay, which lets you mess with system settings or chat with friends without leaving your game. Apple announced that titles like Cyberpunk 2077, Crimson Desert, and Lies of P: Overture are on their way to the platform. Other mainstays are getting refreshed as well. Safari has a redesigned tab layout, Messages is getting polls, and the Journal app is finally making its way from the iPhone to the Mac. The first developer beta for macOS Tahoe is available today, June 9, 2025. A public beta is expected to follow next month, with the final version being released for free to everyone with a compatible Mac this fall. You can check out all the details in Apple's official macOS announcement on the Newsroom.
    • Google introduces new analytics tools in Classroom by David Uzondu Google has started the rollout of new analytics tools for Google Classroom, adding a bunch of ways for educators to monitor the activity of their students. This latest addition puts a new "Analytics" tab on class pages, which, according to Google, is meant to help teachers "see relevant insights on the class analytics page that alert them on how students are progressing and where they may need additional support." For example, on the new page, there might pop up a notification saying "3 students' grades increased over 25% since last month," or, on the flip side, "1 student turned in over half their assignments late in the last month." On the Classwork page, teachers can now see a number next to an assignment showing how many students have not even opened the attached files in Google Drive. For any teacher who has stared at a blank submission list, wondering if a student is struggling or just forgot. It shows who has not even started, letting teachers poke a student privately or nudge the whole class to get going. Google says insights are triggered by factors like approaching deadlines or performance issues, such as a student scoring below 70%. But here is the catch: these new tools are not for everyone. This is a premium feature, locked behind the paid Google Workspace for Education Plus license, so schools using the free version are completely out of luck. For those with a subscription, the student engagement metric that shows unopened Drive files is available right now. The main "Analytics" tab and its associated alerts, however, are on a slower schedule. Their extended rollout begins today. The company says the tab itself should show up by June 30, while the full set of insights will continue to appear, with everything expected to be in place by August 1, 2025. Super admins get access to this automatically and will have to decide which other education leaders and staff get to see all the new data.
    • I welcome thee! Here here!
    • I noticed that macOS STILL does not have a Menubar Icon manager. Top right hand corner is already getting cluttered and they've needlessly added two extra things. We need a better way to manage it and all the third-party tools out there are awful. Don't offer me alternatives, I've tried them more than they are rubbish! Apple, fix this!
  • Recent Achievements

    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      484
    2. 2
      +FloatingFatMan
      277
    3. 3
      ATLien_0
      257
    4. 4
      Edouard
      206
    5. 5
      snowy owl
      199
  • Tell a friend

    Love Neowin? Tell a friend!