Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give Windows Form control a percentage width/height

Tags:

.net

winforms

Kind of new to Windows Form development, but well-experienced in web development. Is there a way to assign a percentage width/height to a Windows Form control so that it expands/shrinks as the user resizes the window? It would be possible to write code to alter the width/height of the control as the window resizes, but I'm hoping there's a better way like in HTML/CSS. Is there?

like image 648
AlliterativeAlice Avatar asked Aug 22 '13 14:08

AlliterativeAlice


1 Answers

In a word, no. You can easily do it programmatically in the Parent control resize event though. For example:

    private void Form1_Resize(object sender, EventArgs e)
    {
        dataGridView1.Width = Convert.ToInt32(this.Width * 0.9);
        dataGridView1.Height = Convert.ToInt32(this.Height * 0.9);
    }

But if you can, use WPF as mentioned in the comments above.

like image 128
Gareth Avatar answered Sep 30 '22 21:09

Gareth