Results 1 to 6 of 6

Thread: error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no access

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    730

    error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no access

    I am using this tutorial:
    https://learn.microsoft.com/en-us/ef...arted/winforms
    to write my first program in C# with sqLite Entity Framework.

    I did it halfway through and finished the section titled "Define the DbContext"
    I placed that code in my project, and then it says:
    Make sure to build the solution at this point.
    which I did successfully.
    The exe file has been created in here:
    D:\Dev\VCS\GetStartedWinFormsSqLiteEF1\GetStartedWinForms\GetStartedWinForms\bin\Debug\net7.0-windows\GetStartedWinForms.exe
    Then I moved to the next section titled "Adding controls to the form"
    and added those grids and the button.

    At that point, it gives me this strange error:
    Text:
    D:\Dev\VCS\GetStartedWinForms SqLite EF1\GetStartedWinForms\GetStartedWinForms\MainForm.Designer.cs(32,18):
    error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no accessible extension method 'AutoScaleMode' accepting a first argument of type 'Form1' could be found (are you missing a using directive or an assembly reference?)
    Screenshot (in case it may be more informative than the text):
    https://i.imgur.com/TgsiRGB.jpeg
    and therefore, I cannot continue.

    Even when I try save the solution, it gives me this error again.

    What is the cause of this problem, and how can I fix it?
    Please help.
    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,998

    Re: error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no ac

    Something seems to have screwed up when you renamed your form. Everything visible seems to indicate that the class and the code file it's in are named MainForm while that error message mentions Form1. It seems that some reference to Form1 got left behind when you changed the name to MainForm. I would suggest that you do a Find In Files for "Form1" and change whatever instance(s) you find to "MainForm". If you're unsure about making such manual edits, let us know what the search finds before making the change.

    This is an example of why it's a good idea to use source control. If you commit regularly then you will have a history of changes and you can easily see when something broke and go back to the last good state if necessary, then try again to make the change. ow that you're in this state, you can easily try to fix it and simply rollback your changes if you make matters worse.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    730

    Re: error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no ac

    Quote Originally Posted by jmcilhinney View Post
    Something seems to have screwed up when you renamed your form. Everything visible seems to indicate that the class and the code file it's in are named MainForm while that error message mentions Form1. It seems that some reference to Form1 got left behind when you changed the name to MainForm. I would suggest that you do a Find In Files for "Form1" and change whatever instance(s) you find to "MainForm"......
    You are right.
    I searched and found that two instances of Form1 had lingered in the code after I had renamed Form1 to MainForm.
    I changed those two to MainForm, and suddenly everything worked fine.
    Thanks for your help.

    For the benefit of other people who may encounter the same problem, I post the new code (after I made that change) in here:
    Code:
        partial class MainForm
        {
            /// <summary>
            ///  Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            ///  Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            ///  Required method for Designer support - do not modify
            ///  the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
    And here is a screenshot of the code before I made that change:
    https://i.imgur.com/4XfA7K7.jpeg

    And a final note is that, I think this is a bug in Visual Studio 2022 (I have the latest updates).
    In Visual Studio 2022 C# IDE, if you change the name of a form via the property window, it is NOT reflected in the solution explorer, and in the file menu and elsewhere (below screenshots are from another project):
    And if you change the form name in solution explorer, it is not reflected in the code (as post #1 in this thread exemplifies).

    What do you think?
    Do you agree that this is a bug in Visual Studio 2022?

    Please advise.
    Thanks.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,998

    Re: error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no ac

    Quote Originally Posted by IliaPreston View Post
    What do you think?
    Do you agree that this is a bug in Visual Studio 2022?
    Almost certainly. I don't do much WinForms stuff these days but I think this may be an issue specifically in WinForms projects targeting .NET Core, which includes .NET 5 and later. I think that changing the name of the startup form fails to change the name where the startup form is specified in the code, requiring a manual change. You should report it to Microsoft, but I'd suggest that you test first to determine specific steps to reproduce the error or they won't touch it. It's possible that the issue has already been reported.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    730

    Re: error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no ac

    Quote Originally Posted by jmcilhinney View Post
    ... You should report it to Microsoft, ...
    I just reported it to Microsoft

    Quote Originally Posted by jmcilhinney View Post
    ... It's possible that the issue has already been reported.
    No. I don't think it has been already reported, because it appears to be a bug that has been there for a very long time, probably since the beginning.

    Anyway, I just reported it.
    I am also suggesting that other people on this board please report it to Microsoft too.
    If more people report the same problem, then Microsoft will take it more seriously.

    Thanks.
    Ilia

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,307

    Re: error CS1061: 'Form1' does not contain a definition for 'AutoScaleMode' and no ac

    Quote Originally Posted by IliaPreston View Post
    You are right.
    I searched and found that two instances of Form1 had lingered in the code after I had renamed Form1 to MainForm.
    I changed those two to MainForm, and suddenly everything worked fine.
    Thanks for your help.

    For the benefit of other people who may encounter the same problem, I post the new code (after I made that change) in here:
    Code:
        partial class MainForm
        {
            /// <summary>
            ///  Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            ///  Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            ///  Required method for Designer support - do not modify
            ///  the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
    And here is a screenshot of the code before I made that change:
    https://i.imgur.com/4XfA7K7.jpeg

    And a final note is that, I think this is a bug in Visual Studio 2022 (I have the latest updates).
    In Visual Studio 2022 C# IDE, if you change the name of a form via the property window, it is NOT reflected in the solution explorer, and in the file menu and elsewhere (below screenshots are from another project):
    And if you change the form name in solution explorer, it is not reflected in the code (as post #1 in this thread exemplifies).

    What do you think?
    Do you agree that this is a bug in Visual Studio 2022?

    Please advise.
    Thanks.
    There is no reason for the filename to match the form name / class name in C#, you could have multiple classes in a single file if you so desire. That means the file name not matching the class name isn't a bug, it might be annoying if you want them to be the same but it certainly isn't a bug. On my PC running VS 2022 17.7.4 renaming the file via solution explorer prompts me to rename the class as well - and doing so found all occurrences and renamed them. Renaming the form via the properties window correctly renamed the class, but didn't rename the files (as expected).

    In your first screenshot, line 34 is simply the form's caption and although it is the original form name this shouldn't cause compilation errors. Quite honestly I would almost always change this to reflect the form's purpose anyway - I certainly wouldn't expect renaming the class to change the form caption.
    Last edited by PlausiblyDamp; Sep 17th, 2023 at 07:10 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width