0

I'm somewhat new to coding and I'm making an application in Windows Forms C#.

What I want to do is resize some of the forms depending on the length of a string. It's doing a calculation with some inputs, and if the output can't all be seen in the form I want it to be resized.

I have done this so far, but it doesn't seem to work:

public Form3(int age, string name)
        {
            InitializeComponent();
            Variables.age = age;
            Variables.name = name;

            if (Variables.length > 22)
            {
                this.Width = Variables.length * 5;
            }
        }
StevoHN
  • 441
  • 1
  • 5
  • 19
  • possible duplicate of [Calculate the display width of a string in C#?](http://stackoverflow.com/questions/263614/calculate-the-display-width-of-a-string-in-c) – Sinatr Jun 24 '15 at 14:34
  • 1
    Why is the `InitializeComponent();` commented? What is `Variable` ? – Thomas Ayoub Jun 24 '15 at 14:35
  • @Sinatr WHAT? This has is similar like *java* and *javascript* or *car* and *carpet* – Thomas Ayoub Jun 24 '15 at 14:36
  • @Thomas Oh well, I just commented it out for the show in this post. It's not like this in my actual code. – StevoHN Jun 24 '15 at 14:38
  • Then show us actual code, it will be simpler for us to help you – Thomas Ayoub Jun 24 '15 at 14:39
  • 1
    @Sinatr I don't think that it is a duplicate but the topic you comment can be the answer for StevoHN StevoHN you can maybe try with: this.Width = Graphics.MeasureString(name); – Fleve Jun 24 '15 at 14:40
  • You don't show us the class definition of the `Variables` object, so we can't tell how its `length` property is initialized. If it's not being initialized, then we must assume a default value of zero... which means your `if` statement is never triggered. And while your resizing technique will crudely do what you describe, there are ways to make it much more precise (as @Fleve mentioned). – Terry Lewis Jun 24 '15 at 15:02

1 Answers1

0

This is the whole point that the AutoSize property was created. It will enable dynamic growing and shrinking based on your form's content. Also, it has several different modes.

umbreon222
  • 247
  • 2
  • 8