Hello!

I have a problem with the size. I've made a function that saves the size, but each time I start the application does the size get larger then the last time. How do I fix that problem?

Here are the code for the function:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            FileInfo FI1 = new FileInfo("Width.txt");
            FileInfo FI2 = new FileInfo("Height.txt");

            if (!FI1.Exists)
            {
                StreamWriter SW = new StreamWriter("Width.txt");
                SW.Write(this.Size.Width);
                SW.Close();
            }

            if (!FI2.Exists)
            {
                StreamWriter SW = new StreamWriter("Height.txt");
                SW.Write(this.Size.Height);
                SW.Close();
            }

            StreamReader SR1 = new StreamReader("Width.txt");
            StreamReader SR2 = new StreamReader("Height.txt");
            TextBox TB1 = new TextBox();
            TextBox TB2 = new TextBox();
            TB1.Text = SR1.ReadLine();
            TB2.Text = SR2.ReadLine();
            SR1.Close();
            SR2.Close();
            this.ClientSize = new Size(Convert.ToInt32(TB1.Text), Convert.ToInt32(TB2.Text));
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Maximized)
            {
                StreamWriter SW1 = new StreamWriter("Width.txt");
                StreamWriter SW2 = new StreamWriter("Height.txt");
                SW1.Write(this.Size.Width);
                SW2.Write(this.Size.Height);
                SW1.Close();
                SW2.Close();
            }
        }
    }
}
Attachments HightChange.jpg 8.19 KB

I think I solved the problem my self.
I change this code:

this.ClientSize = new Size(Convert.ToInt32(TB1.Text), Convert.ToInt32(TB2.Text));

to:

this.ClientSize = new Size(Convert.ToInt32(TB1.Text) - 37, Convert.ToInt32(TB2.Text) - 37);

But I'm not sure if - 37 is the exact right number.

My friend,

Height - 34
and width - 8

use the next method to indicate the increase value of the width and hieght just opne and close the window without any change in the size, you will know the increased pixels

protected override void OnClosing(CancelEventArgs e)
        {
            StreamWriter SW1 = new StreamWriter("Width.txt");
            StreamWriter SW2 = new StreamWriter("Height.txt");
            SW1.Write(this.Size.Width);
            SW2.Write(this.Size.Height);
            SW1.Close();
            SW2.Close();

            base.OnClosing(e);
        }
This question has already been answered. Start a new discussion instead.