6 Replies - 19619 Views - Last Post: 29 July 2010 - 03:57 PM Rate Topic: -----

#1 ramo1995   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 38
  • Joined: 24-July 10

Form Size Changed Event Handler

Posted 26 July 2010 - 06:57 AM

Hello.
I am trying to use an event in which a label automatically postions itself in the center of a form when the form's size is changed ( Maximize, restored down, etc). I have tried many events with the same formula but nothing happens.

1.The Size Changed event:

private void Form1_SizeChanged(object sender, EventArgs e)
        {
            string formwidths = this.Width.ToString();
            int formwidth = Int32.Parse(formwidths);
            string formheights = this.Height.ToString();
            int formheight = Int32.Parse(formheights);
            int finalx = formwidth / 2 + 76;
            int finaly = formheight / 12;
            label2.Location = new System.Drawing.Point(finalx, finaly);
        }


2. The Resize event:

 private void Form1_Resize(object sender, EventArgs e)
        {
            string formwidths = this.Width.ToString();
            int formwidth = Int32.Parse(formwidths);
            string formheights = this .Height .ToString ();
            int formheight = Int32.Parse  ( formheights );
            int finalx = formwidth / 2 + 76;
            int finaly = formheight  / 12;
            label2.Location = new System.Drawing.Point(finalx, finaly);
                       
        }


3. The Resize End Event:

private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            string formwidths = this.Width.ToString();
            int formwidth = Int32.Parse(formwidths);
            string formheights = this.Height.ToString();
            int formheight = Int32.Parse(formheights);
            int finalx = formwidth / 2 + 76;
            int finaly = formheight / 12;
            label2.Location = new System.Drawing.Point(finalx, finaly);
        }


Nothing happens in any one of them.
Can anyone help?

Thanks!

Is This A Good Question/Topic? 1
  • +

Replies To: Form Size Changed Event Handler

#2 sk8ermeb   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 133
  • Joined: 23-March 10

Re: Form Size Changed Event Handler

Posted 26 July 2010 - 07:32 AM

Are you adding the method to your event handler

Forms.SizeChanged += new EventHandler(Form1_SizeChanged);

This will add your method to the method list to run when the "SizedChanged" event is raised and is the perferred way

or are you trying to override the inherit function of the Form? If you want to, use the following override:


protected override void onresize(EventArgs e)
{
///your code here
}



its supposed to be "onresize" but the "R" wont capitalize??? its captialized in the editor??lol

This will automatically run whenever the form changes because you are overriding the base method in the Forms class.
If anyone sees this as bad styleplease let me know! I think it will work but I am still fairly new to c# myself
this is all assuming you are using System.Windows.Forms; and System;

This post has been edited by sk8ermeb: 26 July 2010 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Core   User is offline

  • using System.Linq;
  • member icon

Reputation: 782
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: Form Size Changed Event Handler

Posted 26 July 2010 - 07:48 AM

You need to disable the anchoring for the Label control (by setting the Anchor property to AnchorStyles.None). That way, it will always be in the center of the form, given that you placed it there at design time.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Form Size Changed Event Handler

Posted 26 July 2010 - 07:51 AM

What's with all the strings for strictly mathmatical work.


Logic:
Get the width of the form
Get the width of the label
Locate the label left of center, by half the width of the label
Do the same with the height

void Form1_SizeChanged(object sender, eventargs e)
{
   label1.Location = new Point (this.Width/2 - label1.width/2, this.Height/2 - label1.Height/2);
}

That's it.
Was This Post Helpful? 1
  • +
  • -

#5 ramo1995   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 38
  • Joined: 24-July 10

Re: Form Size Changed Event Handler

Posted 29 July 2010 - 07:13 AM

Hi. I have a small form in which I want the label to move when the form's size is changed. But when I resize the form the label doesn't move. My form's name is 'Help.cs' and the label is 'Label2'.

Code:

 void Help_SizeChanged(object sender, EventArgs e)
        {
            label2.Location = new Point(this.Width / 2 + 76, this.Height / 7);

        }



The strange thing is that I used the same code with another form and it worked.
        void Form1_SizeChanged(object sender, EventArgs e)

{

   label1.Location = new Point (this.Width/2 -76, this.Height/7 );

}



It worked in the second form but not in the first. Can anyone help?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Form Size Changed Event Handler

Posted 29 July 2010 - 07:15 AM

Topics merged, may be a different form but still same issue so no need for a new thread ;)
Was This Post Helpful? 0
  • +
  • -

#7 sk8ermeb   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 133
  • Joined: 23-March 10

Re: Form Size Changed Event Handler

Posted 29 July 2010 - 03:57 PM

If it is workin in one form and not the other then it must be some code difference between the two forms. Maybe you could post more of the code?

This post has been edited by sk8ermeb: 29 July 2010 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1