1

I've created a simple WinForms app in visual studio and I've run into the issue of during runtime, my Winforms changes form or something?

When coding in visual studio and not running the code, it looks like this which is what I'm used to:

enter image description here

When I run my code, the form looks like this, which to me looks more windows 10ish, and I have no idea why.

enter image description here

The runtime form appears as if the minimum size has not been set correctly(which it has) and also the border around the form looks different.

Both of these issues are new to me even though I've been building simple WinForms programs as plugins for another program for over a year now. Perhaps the other program has something to do with it. I'm self-taught so bear with me.

I've recently tried to add a work GitHub account to my personal laptop so I can write code for both my personal and work projects. I don't think the two items are related.

So far,

  1. I've tried removing the System.Windows.Forms reference and then re-adding it.

  2. I've tried cleaning and rebuilding it.

  3. I've tried messing with FormBorderStyle but that doesn't seem to help anything appearance-wise.

  4. I found this post cause it looked liked similar and I tried putting the Application.EnableVisualStyles(); before calling the .ShowDialog() and also tried putting it in the form.Load() function just out of desperation but no luck.

This seems like it could be a relatively simple solution but I'm just not finding it.

Update - 20210808-2222 PST

Context: Running Parallels Windows 10 through macbookpro.
Visual Studio - .NET Framework 4.7.2

End of Update

Update - 20210809-2735 PST

So from the comments that have come in, I've tried the following:

inserting Application.EnableVisualStyles(); at the beginning of Main. but nothing seems to change, my forms still show up the same.

I also tried inserting Application.Application.SetHighDpiMode() but this is for .NET 5. I am using .Net Framework 4.7.2

As i looked through the links posted in the comments. A number of them mentioned a .xml file but my form only has a .Designer.cs and a .resx.

Although basic, here some code for reference in case it helps.

namespace ConsoleApp1
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            // select folder
            // todo: switch to commonOpenFileDialg https://stackoverflow.com/questions/11624298/how-to-use-openfiledialog-to-select-a-folder
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory;
            folderBrowserDialog.Description = "Please select a folder that cones the files you would like rename";
            folderBrowserDialog.ShowDialog();


            // User picks a folder

            if (!folderBrowserDialog.SelectedPath.Equals(""))
            {
                Form1 form1 = new Form1();
                form1.ShowDialog(); // more stuff happens in the form.

 
            }
        }
    }
}



**End of update

Any help and/or direction is appreciated.

Cflux
  • 1,423
  • 3
  • 19
  • 39
  • 2
    Your app is not DpiAware. You didn't mention the .Net version in use. If .Net Framework, start from here: [How to configure an app to run correctly on a machine with a high DPI setting](https://stackoverflow.com/a/13228495/7444103), go on reading here: [High DPI support in Windows Forms](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms). If it's .Net 5+ it's somewhat different, you should define the DpiAwareness mode in Program.cs. – Jimi Aug 09 '21 at 05:09
  • 1
    The docs say to put in your `Main` before anything else happens (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.enablevisualstyles). – Flydog57 Aug 09 '21 at 05:13
  • 1
    Here are a couple of relevant links, with several different suggestions: [Why is my Visual Studio (VB.NET) layout different in Design View vs. at run-time on my home computer, but identical on my work computer?](https://stackoverflow.com/questions/29447403/) and [Form borders look different in Designer and while running](https://social.msdn.microsoft.com/Forums/en-US/834f7ebe-7a68-4684-86a6-af34ae014054/form-borders-look-different-in-designer-and-while-running?forum=vbgeneral) – paulsm4 Aug 09 '21 at 05:15
  • 3
    See [Application.SetHighDpiMode()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.sethighdpimode). All these methods, including `Application.EnableVisualStyles()`, must be called before you create any Form, so in `Main` (Program.cs) before `Application.Run()`. Unless you're trying to create this Form in a Thread other than the default UI Thread. – Jimi Aug 09 '21 at 05:17
  • 1
    I'm afraid your update timestamp is confusing as it doesn't clarify what timezone we're dealing with. - End of comment 20210809-1452 JST – ProgrammingLlama Aug 09 '21 at 05:52
  • @Llama, added the timezone for you. – Cflux Aug 09 '21 at 15:37
  • the Application.EnableVisualStyles(); should be added before Application.Run(new Form()); in the Main method. – Ashish Sinha Aug 09 '21 at 16:07
  • All, I've read through the links and am still stuck. You can see an update of my findings/explorations in the post. Thank you all for your help so far. – Cflux Aug 10 '21 at 01:26
  • @Jimi, It turns out your answer was correct. I had to add an app.manifest and make the necessary adjustments. If you want to post as an answer, I'll mark it correct. – Cflux Aug 10 '21 at 01:50
  • I can do that, if you explain what this application is about. You have `namespace ConsoleApp1` there. Is this actually meant to be a Console app? Or you wanted to build a WinForms app instead (wrong Template)? If the former, do you want to show a Form in your Console app if a User selects a folder from a FolderBrowserDialog, otherwise execute other code or quit? If the latter, why didn't you use the standard Windows Forms Template to build the application? – Jimi Aug 10 '21 at 20:04
  • @Jimi, it was the latter of the 2. I suppose I could have restarted, but I already had my code in the consoleApp1 and figured I could at least try to make it work. Your suggestion worked for both cases. – Cflux Aug 12 '21 at 04:05

0 Answers0