locked
How to call form_resize event handler in form_load event handler using c# RRS feed

  • Question

  • Hi all,

       Good Day! I am working in C# Visual Studio 2008. In this i want to make re-size when the form load is happening. 

    In form load event i called the form re-size event. But it is not working.

    In form re-size event i have called a function to re-size the controls of the form.

    How call form re-size event in form load event. 

    private void frmMain_Load(object sender, EventArgs e)
            {
    this.Resize += new System.EventHandler(this.frmMain_Resize);
    }
    
    private void frmMain_Resize(object sender, EventArgs e)
            {
    
    
               if  (rSzVarSet == true) rResize(this);
            } //private void Form1_Resize(object sender, EventArgs e)


    Thanks & Regards Sasi ................. Go Green

    • Moved by CoolDadTx Monday, August 31, 2015 4:06 PM Winforms related
    Monday, August 31, 2015 11:52 AM

Answers

  • Event Listener are to react if somebody else (especially the user) does something. You don't call them manually anyplace, you let the Element you register them with call them.
    Registering of them should generally be done by the designer code. If it is part of the designer, don't manually add events at runtime (if the events must be added dynamically, it is very likely NOT a good element for the designer).

    If you want to use the same code in different code pieces, you make a function from that and call the function in both places.
    Like you already did with rResize(this). Why are you not just calling rResize(this) in the Form Load? If you want to not duplicate the bool check, then the check does not belong into the listener! But into rResize proper.

    For cases like this I often use default parameters to allow overriding of the check:

    function void rResize(MyFormClass form, bool forceResize = false){
      if(rSzVarSet || forceResize){
        //do logic here
      }
    }

    Generally speaking if you have to resize controls everytime the Form is resized, you really have to look into proper use of Layout Containers/properly override layout related functions of your custom controls. Because those can do the job a thousand times better, faster and more reliable then you ever could this way.

    I also noticed that your lower code has a check if a TabControll actually has tabs. Could it be the tabs are not there yet, because you have not filled that tab control at the time FormLoad is called?
    Then of course you have to call rResize after the Tabs have been added. And that is another good example of why layout should be left to layout containers.

    • Edited by Christopher84 Monday, August 31, 2015 2:20 PM
    • Proposed as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    • Marked as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    Monday, August 31, 2015 2:15 PM
  • Hi,

    You can call the resize event handler in load event like,

    this.frmMain_Resize(this, new EventArgs());
    Regards,

    Anand


    • Edited by Anandaraj T Tuesday, September 1, 2015 5:21 AM Improved content
    • Proposed as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    • Marked as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    Tuesday, September 1, 2015 5:20 AM

All replies

  • I think that you can simply add the line 'rResize(this);' to frmMain_Load.
    • Edited by Viorel_MVP Monday, August 31, 2015 12:22 PM
    Monday, August 31, 2015 12:21 PM
  • You do not need to add an event handler since it only resizes on form load. For that reason call it directly:

    private void frmMain_Load(object sender, EventArgs e)
            {
               rResize(this);
    }

    if you want to resize in case of form resize than add the event handler you added above into that form events handler methods

    Monday, August 31, 2015 12:29 PM
  • i have called rResize(this); directly. But not working.

    Thanks & Regards Sasi ................. Go Green

    Monday, August 31, 2015 12:29 PM
  • What do you expect and what is the result?

    What does rResize make to the Form View?

    Monday, August 31, 2015 12:32 PM
  • When i click maximize form getting resized but when calling function in form load it is not working.

    private void frmMain_Load(object sender, EventArgs e)
            {
    this.AutoSize = true;
                this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                
    
                
                
                //  Auto  resize routine
    
                hasTabs = true; //is tabcontrol present/
    
    
                // Put values in the variables
                rW = this.Width; // this value is for container
                rH = this.Height; //this value is for container
    
                fW = this.Width; //do not change this value
                fH = this.Height; //and this one too
    
    
                // Loop through the controls inside the  form i.e. Tabcontrol Container
                foreach (Control c in this.Controls)
                {
                    c.Tag = c.Name + "/" + c.Left + "/" + c.Top + "/" +
                    c.Width + "/" + c.Height + "/" + (float)c.Font.Size;
                    rSzVarSet = true;
                    if (hasTabs)
                    {
                        if (c.GetType() == typeof(TabControl))
                        {
    
                            foreach (Control f in c.Controls)
                            {
                                foreach (Control j in f.Controls) //tabpage
                                {
                                    j.Tag = j.Name + "/" + j.Left + "/" + j.Top +
                                    "/" + j.Width + "/" + j.Height + "/" + (float)j.Font.Size;
                                  //  rSzVarSet = true;
                                } //foreach (Control j in f.Controls) 
    
                            } //foreach (Control f in c.Controls)
    
                        }  //if (c.GetType() == typeof(TabControl))
    
                    } //if (hasTabs)
    
                    //this.WindowState = FormWindowState.Maximized;
    
                }  //foreach (Control c in this.Controls)
                // auto resize routine
    }
    
    
    
    private void rResize(Control t) // Routine to Auto resize the control
             {            // rRH- Ratio of cntrol height to container height 
                 // rRW - ratio of control width to container height
                 // rH -root Height or the container height
                 // rW - root width or the container width
                 // fW -initial form width
                 // fH -initial form height
    
                 string[] s = {"t","e" };; //set as null contents
                 //s = 
    
                 if (this.Width < fW || this.Height < fH)
                 {
                     //in the future you can change this block to return to 
                     //original when form is resize below the default startup size.
                     this.Width = (int)fW; // i use simple casting
                     this.Height = (int)fH; // this one also
                     return;
                 }  // if (this.Width < fW || this.Height < fH)
    
    
                 foreach (Control c in t.Controls)
                 {
                     // Option 1:
                     double rRW = (t.Width > rW ? t.Width / (rW) : rW / t.Width);
                     double rRH = (t.Height > rH ? t.Height / (rH) : rH / t.Height);
                     
                     //if (string.IsNullOrEmpty(c.Tag.ToString()) == false)
                     //{
                     s =  c.Tag.ToString().Split('/'); //split string using delimiter
                     if (c.Name == s[0].ToString()) //check if the control name is //the same of the stored value in the form load event.
                     {
    
                         //Use integer casting
                         // if you will notice, i use casting to minimize conversion time during runtime
                         c.Width = (int)(Convert.ToInt32(s[3]) * rRW);
                         c.Height = (int)(Convert.ToInt32(s[4]) * rRH);
                         c.Left = (int)(Convert.ToInt32(s[1]) * rRW);
                         c.Top = (int)(Convert.ToInt32(s[2]) * rRH);
                         decimal outfntD;
                         Int32 outfntI;
                         float fnt = 1;
                         if (Decimal.TryParse(s[5], out outfntD))
                             fnt = (float)outfntD;
    
                         if (Int32.TryParse(s[5], out outfntI))
                             fnt = (float)outfntI;
    
                         fnt = (float)(fnt * rRH);
    
                         // c.Font = new Font(this.Font.FontFamily, ((float)(Convert.ToInt32(s[5]) * rRH)));
                         c.Font = new Font(this.Font.FontFamily, fnt);
    
                     }     //if (c.Name == s[0].ToString())
                     //} // is null check
    
                     if (hasTabs) //check if tabcontrol is present inside the container //i.e., form/tabcontrol or groupbox
                     {
                         if (c.GetType() == typeof(TabControl))
                         {
                             foreach (Control f in c.Controls)
                             {
                                 foreach (Control j in f.Controls) //tabpage
                                 {
                                     s = j.Tag.ToString().Split('/');
    
                                     if (j.Name == s[0].ToString())
                                     {
    
                                         j.Width = (int)(Convert.ToInt32(s[3]) * rRW);
                                         j.Height = (int)(Convert.ToInt32(s[4]) * rRH);
                                         j.Left = (int)(Convert.ToInt32(s[1]) * rRW);
                                         j.Top = (int)(Convert.ToInt32(s[2]) * rRH);
                                         decimal outfntDj;
                                         Int32 outfntIj;
                                         float fntj = 1;
                                         if (Decimal.TryParse(s[5], out outfntDj))
                                             fntj = (float)outfntDj;
    
                                         if (Int32.TryParse(s[5], out outfntIj))
                                             fntj = (float)outfntIj;
    
                                         fntj = (float)(fntj * rRH);
                                         //    j.Font = new Font(this.Font.FontFamily, (float)(Convert.ToInt32(s[5]) * rRH));
                                         j.Font = new Font(this.Font.FontFamily, fntj);
    
                                     } //if (j.Name == s[0].ToString())
    
                                 } //foreach (Control j in f.Controls)
    
                             } //foreach (Control f in c.Controls)
    
                         } //if (c.GetType() == typeof(TabControl))
    
                     } //if (hasTabs)
    
                 } //foreach (Control c in t.Controls)
    
             } //private void rResize(Control t)
            // end resize routine


    Thanks & Regards Sasi ................. Go Green


    • Edited by kvg sasi Monday, August 31, 2015 12:40 PM
    Monday, August 31, 2015 12:40 PM
  • Event Listener are to react if somebody else (especially the user) does something. You don't call them manually anyplace, you let the Element you register them with call them.
    Registering of them should generally be done by the designer code. If it is part of the designer, don't manually add events at runtime (if the events must be added dynamically, it is very likely NOT a good element for the designer).

    If you want to use the same code in different code pieces, you make a function from that and call the function in both places.
    Like you already did with rResize(this). Why are you not just calling rResize(this) in the Form Load? If you want to not duplicate the bool check, then the check does not belong into the listener! But into rResize proper.

    For cases like this I often use default parameters to allow overriding of the check:

    function void rResize(MyFormClass form, bool forceResize = false){
      if(rSzVarSet || forceResize){
        //do logic here
      }
    }

    Generally speaking if you have to resize controls everytime the Form is resized, you really have to look into proper use of Layout Containers/properly override layout related functions of your custom controls. Because those can do the job a thousand times better, faster and more reliable then you ever could this way.

    I also noticed that your lower code has a check if a TabControll actually has tabs. Could it be the tabs are not there yet, because you have not filled that tab control at the time FormLoad is called?
    Then of course you have to call rResize after the Tabs have been added. And that is another good example of why layout should be left to layout containers.

    • Edited by Christopher84 Monday, August 31, 2015 2:20 PM
    • Proposed as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    • Marked as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    Monday, August 31, 2015 2:15 PM
  • Hi,

    You can call the resize event handler in load event like,

    this.frmMain_Resize(this, new EventArgs());
    Regards,

    Anand


    • Edited by Anandaraj T Tuesday, September 1, 2015 5:21 AM Improved content
    • Proposed as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    • Marked as answer by Youjun Tang Monday, September 7, 2015 1:47 AM
    Tuesday, September 1, 2015 5:20 AM
  • Hi Anandaraj,

        Good Day! Thanks a lot.  I tried like this its also working.

    this.WindowState = FormWindowState.Maximized;

       


    Thanks & Regards Sasi ................. Go Green

    • Proposed as answer by Youjun Tang Thursday, September 10, 2015 5:25 AM
    Thursday, September 10, 2015 4:47 AM