954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hight and Width Changes Problem

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.19KB
FullBjarne
Newbie Poster
9 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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.

FullBjarne
Newbie Poster
9 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

My friend,

Is this all the code that access the files Width.txt and Height.txt?

AmirBedair
Light Poster
26 posts since Jul 2008
Reputation Points: 10
Solved Threads: 3
 

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);
        }
AmirBedair
Light Poster
26 posts since Jul 2008
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You