Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 14-Apr-12 19:49pm
v10
Comments
CodeHawkz 14-Apr-12 4:28am    
Am I seeing things or am I seeing two identical 'button1_click' methods in the same scope?
Nelek 14-Apr-12 9:31am    
Don't worry, your eyes are healthy. Both are "button1_Click"
Member 8819196 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... :-\

Don't create a new PageSetupDialog or new FontDialog everytime you press the button.
C#
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".
 
Share this answer
 
Comments
Member 8819196 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 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 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 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 20-Apr-12 8:06am    
Why don't you try making the PrintDocument as an instance variable
I was in a hurry to release the project, so i opted forthis workaround:

C#
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:

C#
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...
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900