Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms
I don't understand!
If I save a Font object in a property, I can esily retrieve it next time I start the application.
But if I try to save a PageSettings object the same way, next time I launch the application it is null.
Look at my code:
    public partial class Form1 : Form
    {
        PageSettings PageSettings=new PageSettings();
        Font font=new Font("Lucida Console",10);
 
        public Form1()
        {
            InitializeComponent();
            if (Properties.Settings.Default.PageSettings != null)PageSettings = 
                    Properties.Settings.Default.PageSettings;
            if (Properties.Settings.Default.Font != null)font = 
                    Properties.Settings.Default.Font;
       }
 
        private void button1_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.Font = font;
            if (fd.ShowDialog() == DialogResult.OK) font = fd.Font;
            Properties.Settings.Default.Font = font;
            Properties.Settings.Default.Save();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            PageSetupDialog psd=new PageSetupDialog();
            psd.PageSettings = setting;
            if(psd.ShowDialog()==DialogResult.OK)PageSettings=psd.PageSettings;
            Properties.Settings.Default.PageSettings = PageSettings;
            Properties.Settings.Default.Save();
        }
    }
 
If I push button1, FontDialog pops up, correctly remembering the Font I selected in previous session.
But if I push button2, PageSetupDialog shows default settings, forgetting previous session's changes.
If I submit new data, they will be remembered until the main window is open, but if I close and restart the program, they are gone.
I assure you I have correctly created the two properties in settings designer, carefully assigning the right type and name...
Posted 13-Apr-12 19:28pm
Edited 14-Apr-12 20:49pm
v10
Comments
PasanRatnayake at 14-Apr-12 4:28am
   
Am I seeing things or am I seeing two identical 'button1_click' methods in the same scope?
Nelek at 14-Apr-12 9:31am
   
Don't worry, your eyes are healthy. Both are "button1_Click"
Member 8819196 at 15-Apr-12 1:52am
   
Sorry, I only have pasted two pieces of test code, just to show the problem, forgetting to fix the "collision". But I can assure that this is not THE problem... :-\
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Don't create a new PageSetupDialog or new FontDialog everytime you press the button.
public partial class Form1 : Form
    {
        PageSettings PageSettings=new PageSettings();
        Font font=new Font("Lucida Console",10);
        FontDialog fd = new FontDialog();
        PageSetupDialog psd=new PageSetupDialog();
 
        public Form1()
        {
            InitializeComponent();
            if (Properties.Settings.Default.PageSettings != null)PageSettings = 
                    Properties.Settings.Default.PageSettings;
            if (Properties.Settings.Default.Font != null)font = 
                    Properties.Settings.Default.Font;
       }
 
        private void button1_Click(object sender, EventArgs e)
        {
            fd.Font = font;
            if (fd.ShowDialog() == DialogResult.OK) font = fd.Font;
            Properties.Settings.Default.Font = font;
            Properties.Settings.Default.Save();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            psd.PageSettings = setting;
            if(psd.ShowDialog()==DialogResult.OK)PageSettings=psd.PageSettings;
            Properties.Settings.Default.PageSettings = PageSettings;
            Properties.Settings.Default.Save();
        }
    }
 
If you like this answer, please don't forget to mark it as "answer".
  Permalink  
Comments
Member 8819196 at 15-Apr-12 18:33pm
   
Obviously this is only simplified test code, just to show the problem. In real application I load Properties.Settings.Default at application init, then set and save at application shutdown, but it silently refuses to save (or to load?) PageSettings, while it correctly saves and reloads other objects and variables (not all, I dont understand which and why...)
Ruselo Riva Asentista at 16-Apr-12 0:57am
   
Ok, the reason is that PageSettings is "hollow", just as FontDialog and PrintDialogs. They are agents to a data-oriented object. Usually this a PrintDocument. The Font Object is a property of PrintDocument that will be changed by the FontDialog, so whenever you save from a FontDialog, you are saving to the PrintDocument rather than the Font.
 
In short, "host" the PageSettings in a PrintDocument and Save() will work.
Member 8819196 at 16-Apr-12 6:42am
   
No, I'm sorry. I've just tried something like:
 
PrintDocument doc = new PrintDocument();
doc.DefaultPageSettings = pageSettings;
Properties.Settings.Default.Doc = doc;
Properties.Settings.Default.Save();
 
but at reload of application Properties.Settings.Default.Doc is null. :-{
Member 8819196 at 16-Apr-12 7:00am
   
Just to clarify, if I try:
 
Margins margins = pageSettings.Margins;
Properties.Settings.Default.Margins = margins;
Properties.Settings.Default.Save();
 
it works without problems: at restart it reloads margins correctly.
But if I try to save the whole PageSettings in one move, it fails.
Ruselo Riva Asentista at 20-Apr-12 8:06am
   
Why don't you try making the PrintDocument as an instance variable
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I was in a hurry to release the project, so i opted forthis workaround:

private void Load()
{
    PageSettings pageSettings = new PageSettings();
    pageSettings.Margins = Properties.Settings.Default.Margins;
    pageSettings.Landscape = Properties.Settings.Default.Landscape;
    pageSettings.PaperSize = Properties.Settings.Default.PaperSize;
    pageSettings.PaperSource = Properties.Settings.Default.PaperSource;
}
 
private void Save()
{
    Properties.Settings.Default.Margins = pageSettings.Margins;
    Properties.Settings.Default.Landscape = pageSettings.Landscape;
    Properties.Settings.Default.PaperSize = pageSettings.PaperSize;
    Properties.Settings.Default.PaperSource = pageSettings.PaperSource;
    Properties.Settings.Default.Save();
}

Sure is a waste of time to write all this lines of code in front of the simplest:
 
private void Load()
{
     pageSettings.PaperSource = Properties.Settings.Default.PageSettings;
}
 
private void Save()
{
    Properties.Settings.Default.PageSettings = pageSettings;
    Properties.Settings.Default.Save();
}

but it doesn't seem to impact on performance and space requirements.
It remains the delusion of not understanding WHY...
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100