13 Replies - 441 Views - Last Post: 14 August 2011 - 02:31 PM Rate Topic: -----

#1 Delocaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 11

Calling a function when the window resizes

Posted 13 August 2011 - 04:14 AM

So i just started with C# today, and my first project was a Javascript array generator. (JS generator in C#? Weird, yes, but that's what i'm trying to do.) It all works, but when you resize the window, it's all in it's previous position. (That means if you maximize, you have A LOT of unused space.) So i wrote a function to fit all the controls when you resize. Only problem is to call that function when i resize the window. I tried to solve it for ~2 hours now, and still haven't foud a solution. (With Google!) Making a function called onresize didn't work, neither did Form1_Resize. (As it used to do in VB.NET) So, here's my code:
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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            String txt = textBox2.Text;
            this.Text = "Splitting string";
            string[] arr = txt.Split(' ');
            string del = "\",\"";
            this.Text = "Joining array with delimeters";
            string cod = string.Join(del, arr);
            this.Text = "Adding prefix and suffix";
            string fin = "var " + textBox1.Text + "=[\"" + cod + "\"];";
            Clipboard.SetText(fin);
            this.Text = "Done. Result copied to clipboard.";
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            resize();
        }
        private void Form1_Resize(object sender, System.EventArgs e)
        {
            resize();
        }
        private void resize()
        {
            textBox1.Size = new Size(this.Width - 200, 20);
            button1.Location = new Point(this.Width - 111, 4);
            textBox2.Size = new Size(this.Width - 28, this.Height - 28);
        }
    }
}



Help a newb, please? Also, this is my first post here, so if i wrote the title wrong, broke the rules, or misspelled anything, just let me know.

Thanks in Advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a function when the window resizes

#2 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 16-March 09

Re: Calling a function when the window resizes

Posted 13 August 2011 - 04:45 AM

You must create an event when your control is being resized. The easiest way to do this is to select a control and in properties window go under events, and then double click in a field where it says resize. All you have to do now is to call your function from there (Event had been automatically created).
Was This Post Helpful? 1
  • +
  • -

#3 Delocaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 11

Re: Calling a function when the window resizes

Posted 13 August 2011 - 04:50 AM

View Postwuu, on 13 August 2011 - 04:45 AM, said:

You must create an event when your control is being resized. The easiest way to do this is to select a control and in properties window go under events, and then double click in a field where it says resize. All you have to do now is to call your function from there (Event had been automatically created).

Thank you SO much! +rep to you!
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: Calling a function when the window resizes

Posted 13 August 2011 - 06:33 AM

Let me also throw in a couple tips:
Take note of #2
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)


  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    FormMain_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    

This post has been edited by tlhIn`toq: 13 August 2011 - 06:35 AM

Was This Post Helpful? 1
  • +
  • -

#5 Delocaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 11

Re: Calling a function when the window resizes

Posted 13 August 2011 - 06:36 AM

View PosttlhIn`toq, on 13 August 2011 - 06:33 AM, said:

Let me also throw in a couple tips:
Take note of #2
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)


  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    FormMain_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    

1) Don't expect me to know all that in my very first project.
2) Is this spam? Your username looks like you're a spambot...
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: Calling a function when the window resizes

Posted 13 August 2011 - 06:46 AM

Dude, chill. No need to be so touchy, or insulting (spambot)

Nobody *expects* you to know anything. That's why we throw out tips to help you along. Just little nuggets gained from experience. Things that are good to have in the back of your mind as you are designing your applications. Things that help you build good habits early because it is easier than un-learning bad habits later down the line.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: Calling a function when the window resizes

Posted 13 August 2011 - 07:37 AM

Just an observation. If you had gone through a "Learn C# in 21 days" type of self-teaching book you would have picked this up. So I am guessing you are trying to teach yourself without first going through a book or tutorial: Just jumping in and trying to design and build a program right off the bat. That really is the hard way to go.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

We don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Aug 2011
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 AmbientTech  Icon User is offline

  • I'm an @sshole :)

Reputation: 4
  • View blog
  • Posts: 2,649
  • Joined: 11-August 11

Re: Calling a function when the window resizes

Posted 13 August 2011 - 07:50 AM

Just a thought, and I'm new to this, too. It won't resize your objects so if that's your goal sorry for the bad hint, but if you just won't them to locate more appropriately on the page position, you can also turn off the anchor properties for the object in the designer page.
Just my 2 cents worth.
Was This Post Helpful? 1
  • +
  • -

#9 Delocaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 11

Re: Calling a function when the window resizes

Posted 13 August 2011 - 07:58 AM

View PostAmbientTech, on 13 August 2011 - 07:50 AM, said:

Just a thought, and I'm new to this, too. It won't resize your objects so if that's your goal sorry for the bad hint, but if you just won't them to locate more appropriately on the page position, you can also turn off the anchor properties for the object in the designer page.
Just my 2 cents worth.

That wasn't my goal. My goal was to move (button) and resize (textbox) the objects so it won't look so bad when you resize thr window.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: Calling a function when the window resizes

Posted 13 August 2011 - 07:58 AM

I think the OP is handling the resizing programmatically.
It looks like he is just trying to get the method to run and do something to confirm it is working, first. Then fine tune the size and placement of each control.

private void Form1_Resize(object sender, System.EventArgs e)
        {
            resize();
        }
        private void resize()
        {
            textBox1.Size = new Size(this.Width - 200, 20);
            button1.Location = new Point(this.Width - 111, 4);
            textBox2.Size = new Size(this.Width - 28, this.Height - 28);
        }


Was This Post Helpful? 0
  • +
  • -

#11 Delocaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 11

Re: Calling a function when the window resizes

Posted 13 August 2011 - 08:00 AM

View PosttlhIn`toq, on 13 August 2011 - 06:46 AM, said:

Dude, chill. No need to be so touchy, or insulting (spambot)

Nobody *expects* you to know anything. That's why we throw out tips to help you along. Just little nuggets gained from experience. Things that are good to have in the back of your mind as you are designing your applications. Things that help you build good habits early because it is easier than un-learning bad habits later down the line.

I thought you were a spambot because of your name. (Random unpronounceable gibberish)

View PosttlhIn`toq, on 13 August 2011 - 07:58 AM, said:

I think the OP is handling the resizing programmatically.
It looks like he is just trying to get the method to run and do something to confirm it is working, first. Then fine tune the size and placement of each control.

private void Form1_Resize(object sender, System.EventArgs e)
        {
            resize();
        }
        private void resize()
        {
            textBox1.Size = new Size(this.Width - 200, 20);
            button1.Location = new Point(this.Width - 111, 4);
            textBox2.Size = new Size(this.Width - 28, this.Height - 28);
        }


And yes, you are correct.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: Calling a function when the window resizes

Posted 13 August 2011 - 08:23 AM

At the risk of being insulted again, let me throw out a couple more tips:

You don't want to use this.width.
You want to use this.ClientRectangle.Width.
this.width includes the form borders which can vary in size depending on the theme the user has selected for their PC. God forbid they have some 3rd party utility that give them really futuristic looking windows like out of Minority Report. Your form will redeploy itself into something really crazy.
.ClientRectangle is the area within the boarders where your controls can reside.
http://msdn.microsof...trectangle.aspx

When I resize and relocate controls like this I tend to pick a control that a few others are meant to group around, then resize relative to that. It means making less code changes. In the example below the first button sets itself to the middle horizontally, and all the others position themselves automatically. You can change the overall spacing by changing just one variable: margin
void resize()
{
   int margin = 10; // Change all the spacing with one code change

   btnSend.Location = new Point( this.ClientRectangle.Width/2 - btnSend.Width/2, 10); // Horizontally centered
   btnReceive.Location = new Point (btnSend.Location.X, btnSend.Bounds.Bottom + margin); // Below the send button
   btnFred.Location = new point (btnSend.Bounds.Right + margin, btnSend.Location.Y); // Right of the Send button
   btnBarney.Location = new Point (btnReceive.Bounds.Right + margin, btnReceive.Location.Y); // Right of the Receive button
   txtTransmission.Location = new Point( btnBarney.Location.X, btnBarney.Bounds.Bottom + margin); // Below the Barney button
   txtTransmission.Size = new Size(btnBarney.Bounds.Right - btnReceive.Location.X, txtTransmission.Height);// Same height as designed, but span the width of both columns of buttons
}


Was This Post Helpful? 3
  • +
  • -

#13 Delocaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 11

Re: Calling a function when the window resizes

Posted 13 August 2011 - 08:26 AM

View PosttlhIn`toq, on 13 August 2011 - 08:23 AM, said:

At the risk of being insulted again, let me throw out a couple more tips:

You don't want to use this.width.
You want to use this.ClientRectangle.Width.
this.width includes the form borders which can vary in size depending on the theme the user has selected for their PC. God forbid they have some 3rd party utility that give them really futuristic looking windows like out of Minority Report. Your form will redeploy itself into something really crazy.
.ClientRectangle is the area within the boarders where your controls can reside.
http://msdn.microsof...trectangle.aspx

When I resize and relocate controls like this I tend to pick a control that a few others are meant to group around, then resize relative to that. It means making less code changes. In the example below the first button sets itself to the middle horizontally, and all the others position themselves automatically. You can change the overall spacing by changing just one variable: margin
void resize()
{
   int margin = 10; // Change all the spacing with one code change

   btnSend.Location = new Point( this.ClientRectangle.Width/2 - btnSend.Width/2, 10); // Horizontally centered
   btnReceive.Location = new Point (btnSend.Location.X, btnSend.Bounds.Bottom + margin); // Below the send button
   btnFred.Location = new point (btnSend.Bounds.Right + margin, btnSend.Location.Y); // Right of the Send button
   btnBarney.Location = new Point (btnReceive.Bounds.Right + margin, btnReceive.Location.Y); // Right of the Receive button
   txtTransmission.Location = new Point( btnBarney.Location.X, btnBarney.Bounds.Bottom + margin); // Below the Barney button
   txtTransmission.Size = new Size(btnBarney.Bounds.Right - btnReceive.Location.X, txtTransmission.Height);// Same height as designed, but span the width of both columns of buttons
}


Thank you.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4314
  • View blog
  • Posts: 7,480
  • Joined: 08-June 10

Re: Calling a function when the window resizes

Posted 14 August 2011 - 02:31 PM

His name is Klingon, I believe. And trust me, no spambot is going to get a mentor badge or 1700 rep on this site.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1