Adjust Form layout according to monitor resolution

Asked

Viewed 4,220 times

0

I made a form in fullscreen, where the monitor I use is 23'' inches.

But where the functionality will be deployed, they use 19". When I executed, the Form did not get the size of the smaller monitor, and got cut some components.

Is there any way to make it responsive according to the monitor or will I have to edit the size of the components?

23"

23"

19"

19"

2 answers

4


Making a responsive form is not difficult. There are several alternatives that can be used (Tabbledlayoutpanel, Anchor Properties and Dock of the controls, Flowlayoutpanel and the junction of all inclusive).

I’ll give you a small example using the Anchor property of the controls. See below:

Minha Busca

The Anchor property indicates in which directions the control will be anchored. For example, if the anchor of my control is "Top, Right" it indicates that my control will always keep the same distance from the Top and Right edge of the parent’s control. All control by default comes with the Anchor property defined as "Top,Left", meaning it will keep the relative distance above and left.

Anchor

Following our example we will make the "Go" button always in the upper right corner of the form. To do this we will change the Achor property to "Top, Rigth". Once this is done, when resizing the form, it will assume the behavior below:

Botão

The next step is to make the text box always keep the same distance both left and right. For this, we will use the anchor "Top, Left, Rigth":

Campo Texto

Finally, we want the grid below to keep the same distance in all directions. For this, just set Anchor as "Top, Bottom, Left, Rigth".

Janela full

I hope this little example will help your goal. Note that this property Achor can be defined via code as below where I define "Botton, Right" in a text field:

 textBox1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;

Remembering that the anchor (Anchor) is always relative to the parent control. In the example below I set the anchor to all directions on the "Hello" button but as this button has as parent a panel, it will follow the behavior of the parent.

inserir a descrição da imagem aqui

2

That’s right. Winforms has everything fixed. And most leave this way.

It is possible to use a flow panel to help her get more "responsive" (I made fun of her, I don’t like her using that word for this). But if you don’t know how to do it right, the result can get a lot worse and often not even realize it. But you have to change some controls to fit the panel size.

I know there are libraries that help a little more, but I’ve never used them and it seems to me that they weigh in and some are full of problems.

Of course you can also set up your own scheme, but it takes a lot of work to get it right in Winforms, and it tends to go very wrong if you don’t master the subject.

And yes, some people prefer to do two layouts different. Not that you need two codes, just parameterize the positions and size of the controls. Of course, like everything, you need to know how to do.

With WPF can do a little better. In fact it can do even more interesting it is to work with \zoom to fill everything (not that it is useful in all cases).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.