How to fix when dragging a usercontrol over groupbox control and the usercontrol size is changing?

eshesh michael 60 Reputation points
2023-08-15T03:33:03.79+00:00

I created a UserControl (Windows Forms)...

Then in form1 designer I have a GroupBox and when I'm dragging the new UserControl (DownloadInformation) over the GroupBox the UserControl size is changing and get bigger than what I have set it.

This screenshot is of the UserControl designer, the size is 216, 117:

user1

And this screenshot is when I drag the UserControl in form1 designer and it is in the size I wanted it to be:

user2

And this screenshot is when I drag the UserControl inthe form1 designer of the GroupBox on the left side:

Th size of the UserControl has changed and also the content lines space and everything.

How can I keep the UserControl size when dragging it over the GroupBox?

user3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace My_Test
{
    public partial class DownloadInformation : UserControl
    {
        public DownloadInformation()
        {
            InitializeComponent();

            var flp = new FlowLayoutPanel
            {
                Width = 216,
                Height = 117,
                Location = new Point(10, 10),
                Font = new Font("Microsoft Sans Serif", 14)
            };
            Controls.Add(flp);

            // Put the labels to the dictionary for better processing.
            var labels = new Dictionary<string, string>
            {
                { "Size:", "1.211 MB" },
                { "Downloaded:", "0.727 MB" },
                { "Images left:", "4" },
                { "Speed:", "1.79 MB/s" },
            };

            // Go through each label
            foreach (var line in labels)
            {
                // Each line consists of two labels: Text + Value
                // Each label has its own color.
                flp.Controls.Add(new Label() { Text = line.Key, ForeColor = Color.Green });
                flp.Controls.Add(new Label() { Text = line.Value, ForeColor = Color.Red });

                // Break after each line
                flp.SetFlowBreak(flp.Controls[flp.Controls.Count - 1], true);
            }
        }

        private void DownloadInformation_Load(object sender, EventArgs e)
        {

        }
    }
}

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-08-15T06:01:38.07+00:00

    Hi @eshesh michael , Welcome to Microsoft Q&A,

    From experience, it seems to you that this is scaling due to DPI, can't confirm.

    Try setting the Usercontrol's AutoScaleMode property to None.

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.