Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Why does this code not WORK?

Why does this code not WORK?

Scheduled Pinned Locked Moved C#
questiongraphics
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Christopher Stratmann

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace WindowsApplication11 {
    public class FormA : Form {
    public virtual new Size PreferredSize {
    get {
    return new Size(300, 300);
    }
    }
    }

    static class Program {
    	\[STAThread\]
    	static void Main() {
    		Application.EnableVisualStyles();
    		Application.SetCompatibleTextRenderingDefault(false);
    		Form form = new FormA();
    		form.Size = form.PreferredSize;  // 300, 300 should be returned but instead 123,34 is returned.
    		Application.Run(form);
    	}
    }
    

    }

    Chris

    A Offline
    A Offline
    Abhijit Jana
    wrote on last edited by
    #2

    chris175 wrote:

    public virtual new Size PreferredSize { get { return new Size(300, 300); } }

    What is "Size" ?

    Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

    C 1 Reply Last reply
    0
    • A Abhijit Jana

      chris175 wrote:

      public virtual new Size PreferredSize { get { return new Size(300, 300); } }

      What is "Size" ?

      Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

      C Offline
      C Offline
      Christopher Stratmann
      wrote on last edited by
      #3

      System.Drawing.Size

      A 1 Reply Last reply
      0
      • C Christopher Stratmann

        using System;
        using System.Drawing;
        using System.Windows.Forms;

        namespace WindowsApplication11 {
        public class FormA : Form {
        public virtual new Size PreferredSize {
        get {
        return new Size(300, 300);
        }
        }
        }

        static class Program {
        	\[STAThread\]
        	static void Main() {
        		Application.EnableVisualStyles();
        		Application.SetCompatibleTextRenderingDefault(false);
        		Form form = new FormA();
        		form.Size = form.PreferredSize;  // 300, 300 should be returned but instead 123,34 is returned.
        		Application.Run(form);
        	}
        }
        

        }

        Chris

        A Offline
        A Offline
        Alan Balkany
        wrote on last edited by
        #4

        I encountered something similar, and got the right size AFTER the call to Application.Run (). If you get the size in FormA's Load method, it might be right at that point.

        1 Reply Last reply
        0
        • C Christopher Stratmann

          using System;
          using System.Drawing;
          using System.Windows.Forms;

          namespace WindowsApplication11 {
          public class FormA : Form {
          public virtual new Size PreferredSize {
          get {
          return new Size(300, 300);
          }
          }
          }

          static class Program {
          	\[STAThread\]
          	static void Main() {
          		Application.EnableVisualStyles();
          		Application.SetCompatibleTextRenderingDefault(false);
          		Form form = new FormA();
          		form.Size = form.PreferredSize;  // 300, 300 should be returned but instead 123,34 is returned.
          		Application.Run(form);
          	}
          }
          

          }

          Chris

          C Offline
          C Offline
          carbon_golem
          wrote on last edited by
          #5

          Because when you Application.Run(form), InitializeComponent() gets called which probably resets the size of the form. Scott P

          “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

          C L 2 Replies Last reply
          0
          • C carbon_golem

            Because when you Application.Run(form), InitializeComponent() gets called which probably resets the size of the form. Scott P

            “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

            C Offline
            C Offline
            Christopher Stratmann
            wrote on last edited by
            #6

            The wrong size is placed on the form BEFORE Application.Run(form) is called. I dont think it has anything to do with InitializeComponent() function since there isn't one within the class FormA. Chris

            1 Reply Last reply
            0
            • C Christopher Stratmann

              using System;
              using System.Drawing;
              using System.Windows.Forms;

              namespace WindowsApplication11 {
              public class FormA : Form {
              public virtual new Size PreferredSize {
              get {
              return new Size(300, 300);
              }
              }
              }

              static class Program {
              	\[STAThread\]
              	static void Main() {
              		Application.EnableVisualStyles();
              		Application.SetCompatibleTextRenderingDefault(false);
              		Form form = new FormA();
              		form.Size = form.PreferredSize;  // 300, 300 should be returned but instead 123,34 is returned.
              		Application.Run(form);
              	}
              }
              

              }

              Chris

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              If you do FormA form = new FormA() then your method will be called, otherwise the System.Windows.Forms.Control.PreferredSize property will be called (can be seen in .NET reflector). I guess this has to do with the fact that PrefferedSize in Control is not marked virtual or abstract. regards

              1 Reply Last reply
              0
              • C Christopher Stratmann

                System.Drawing.Size

                A Offline
                A Offline
                Abhijit Jana
                wrote on last edited by
                #8

                Its Working Fine

                using System;
                using System.Drawing;
                using System.Windows.Forms;
                namespace WindowsApplication11
                {
                public class FormA : Form
                {
                public virtual new Size PreferredSize
                {
                get
                {
                return new Size(300, 300);
                }
                }
                }
                static class Program
                {
                [STAThread]
                static void Main()
                {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form form = new FormA();
                Application.Run(form);
                form.Size = form.PreferredSize; // 300, 300 should be returned but instead 123,34 is returned.

                    }
                }
                

                }

                Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

                1 Reply Last reply
                0
                • C carbon_golem

                  Because when you Application.Run(form), InitializeComponent() gets called which probably resets the size of the form. Scott P

                  “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #9

                  carbon_golem wrote:

                  Because when you Application.Run(form), InitializeComponent()

                  No, you have it all wrong, there is no magic involved. InitializeComponent() is usually called from the Form's constructor.

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 alpha 4a out now (29 May 2008)

                  1 Reply Last reply
                  0
                  • C Christopher Stratmann

                    using System;
                    using System.Drawing;
                    using System.Windows.Forms;

                    namespace WindowsApplication11 {
                    public class FormA : Form {
                    public virtual new Size PreferredSize {
                    get {
                    return new Size(300, 300);
                    }
                    }
                    }

                    static class Program {
                    	\[STAThread\]
                    	static void Main() {
                    		Application.EnableVisualStyles();
                    		Application.SetCompatibleTextRenderingDefault(false);
                    		Form form = new FormA();
                    		form.Size = form.PreferredSize;  // 300, 300 should be returned but instead 123,34 is returned.
                    		Application.Run(form);
                    	}
                    }
                    

                    }

                    Chris

                    D Offline
                    D Offline
                    Daniel Grunwald
                    wrote on last edited by
                    #10

                    PreferredSize isn't virtual; you cannot override it. Instead your code created a new virtual property with the same name - but your Main method refers to the original PreferredSize property. You should override the virtual GetPreferredSize method instead.

                    C 1 Reply Last reply
                    0
                    • D Daniel Grunwald

                      PreferredSize isn't virtual; you cannot override it. Instead your code created a new virtual property with the same name - but your Main method refers to the original PreferredSize property. You should override the virtual GetPreferredSize method instead.

                      C Offline
                      C Offline
                      Christopher Stratmann
                      wrote on last edited by
                      #11

                      That is what I was looking for... Thanks

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups