Use a variable to change a button size

Hi Experts,

using C# I have got a string  from a database (75, 75), I now want to assign this string so it will become the new size of various buttons I have on my form,  something like:

string  myButtonSize;
button1.Size = new System.Drawing.Size(myButtonSize)

Hope somebody can help
dizzycatAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
There's no direct method Size.FromString but you can use the TypeConverter Class like this:
try {
    string myButtonSize = textBox1.Text;
    button1.Size = (System.Drawing.Size)TypeDescriptor.GetConverter(typeof(System.Drawing.Size)).ConvertFromString(myButtonSize);
}
catch (Exception ex) {
    MessageBox.Show(ex.ToString(), ex.Message);
}

Open in new window

capture
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Robert SchuttSoftware EngineerCommented:
Alternatively, you can define your own Size.FromString() method using extension methods:
namespace EE_Q_28644875 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e) {
            try {
                string myButtonSize = textBox1.Text;
                button1.Size = Size.FromString(myButtonSize);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString(), ex.Message);
            }
        }
    }

    public static class ExtensionsClass {
        public static System.Drawing.Size FromString(this System.Drawing.Size sz, string s) {
            try {
                string[] arr = s.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                System.Drawing.Size newsz = new System.Drawing.Size(Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]));
                return newsz;
            }
            catch (Exception ex) {
                return new System.Drawing.Size(1, 1); // think of a more useful default or throw error
            }
        }
    }
}

Open in new window

0
dizzycatAuthor Commented:
Thanks Robert, this works just great.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.