0

I have a Windows application developed in C# with 20 forms. How can I save a user selected font in a database and reload it in the application as it apply on all forms in my project as a default form, label, textbox, etc.?

Actually, I want to let users to customise application forms.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
franchesco totti
  • 582
  • 1
  • 7
  • 28

1 Answers1

0

You can't save a font Object into a Database directly. You can make a function that will convert Font to String and back, so that the string value can then be saved into the database.

In the appearance settings dialog of your application, you can have all the settings you need, as long as you call your fontToString() function as it saves. Then (I'm assuming you have a database to save to, otherwise you could either use AppData or the Properties.Settings.Default feature of .NET) save the string you got from your function to the database.

You should also implement a function in a public static class or something easily accessible called updateFontInAllWindows(), where you look up the settings from the database and apply them to every form. Here's some sample code (Font usefont is the final result of your database query).

Font usefont = DatabaseHandler.GetFont();
List<Form> formList = new List<Form>();
formList.Add(Form1);
formList.Add(Form2);
foreach (Form frm in formList) {
   foreach (Control ctl in frm.Controls) {
       ctl.Font = usefont;
   }
}

This code loops through each control in each form and applies the font to it. You can expand it to use other settings such as BackgroundColor as well. Note that you must add each form to be customised by the user's settings to the formList List. Good luck!

LS97
  • 336
  • 4
  • 15
  • should I call this function in all forms load event or just mainform of my app? or I can do something like binding objects properties to it – franchesco totti Aug 24 '13 at 13:28
  • 1
    I wouldn't suggest using property binding on the forms since this way you have a more comprehensive control of the options set. You should call this function only once at startup, whenever you're ready (after initialising the database or whatever you need to do). – LS97 Aug 24 '13 at 13:35