如何在VB.NET 2015中的winform应用程序中制作自动控件大小 [英] How to make auto control sizes in winform application in VB.NET 2015

查看:56
本文介绍了如何在VB.NET 2015中的winform应用程序中制作自动控件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



美好的一天,



我在Windows窗体应用程序中工作在vb.net中使用visual studio 2015.

我需要在表单上放置所有控件的自动大小,如标签,文本框,组合框,DataGridViews等。



例如,当窗体的大小增加或减少时,控件的大小也必须相应地增加或减少字体大小。

此外,如果应用程序在具有高分辨率和大显示屏幕的系统上运行,然后表格将自动调整,因为for的Windowstate属性设置为最大化。在这种情况下,我希望控件的大小和字体大小也应相应增加,反之亦然。



希望我成功地解释了我真正需要的东西。

如果有人可以指导我,我会很感激这种特惠。



谢谢!



问候:

Muhammad Asim Mughal



我的尝试:



为此,我使用了表布局面板控件,并使用了Anchor&控件的Doc属性。所以控件的大小增加和减少但字体大小保持不变,这就是为什么Form的分辨率看起来是非平衡的。

Hi!

Good Day to All,

I am working in a windows form application in vb.net using visual studio 2015.
I need to make auto sizes of all the controls placed on the form like Labels, Text Boxes, Combo Boxes, DataGridViews etc.

For example when the sizes of the form increases or decreases then the size of the controls also must be increase or decrease with the font sizes accordingly.
Moreover if the application is running on a system that has a high resolution and big display screen then the form will be adjusted automatically because the Windowstate Property of the for is set as Maximized. In that case i want that the sizes of the controls and their font sizes should also be increased accordingly and the vice versa.

Hopefully i successfully explained what i need actually.
If anyone can guide me i will be grateful for this ac of kind.

Thanks!

Regards:
Muhammad Asim Mughal

What I have tried:

For this purpose i used Table Layout Panel Control and also used the Anchor & Doc property of the controls. So the sizes of the controls are increased and decreased but the font sizes remains same so that's why the resolution of the Form looks UN-balanced.

推荐答案

有3种方式要做到这一点:

1st:你自定义每个控件,使它能够适应控件尺寸的字体大小

第二:你自定义你的容器控件做与属于它的控件相同

第3名:您使用属于它的控件向您的表单(也是一个容器)添加一个方法。



现在你应该决定是否要为每个控件设置最大字体大小(一个控件的字体大小为16,一个控件有12个,依此类推)...或者......如果你想要的话拥有控件通用的最大字体大小(一个控件的字体大小= 16,一个控制器有12个 - 所以每个都设置为12个。)



通往意识到它是:

- 迭代到你的控件 - 收集容器

- 获取每个Control的大小及其Text-Content并计算此Control的最大可能字体大小并记住它(取决于您的决定)

- ::

- 迭代到您的容器的Controls-Collection

- 将每个Control的大小设置为新的(计算的)Fontsize



这是一种获取控件给定大小的最大匹配字体大小的方法,具体取决于它的文本内容和所选字体,它的样式:

There are 3 ways to do it :
1st : You customize each of your controls to make it able to fit it's Fontsize to the Controls dimension
2nd : You customize your Container-Control to do the same with the Controls belonging to it
3rd : You add a method to your Form (which is also a Container) to do that with the Controls belonging to it.

Now you should decide if you want to have the maximum Fontsize for each of those Controls (one Control has Fontsize=16 and one has 12 and so on) ... OR ... if you want to have the maximum Fontsize which is common to the Controls (one Control has Fontsize=16 and one has 12 - so you set each to 12).

The way to realize it is :
- iterate to the Controls-Collection of your Container
- get the Size of each Control and it's Text-Content and calculate the maximum possible Fontsize of this Control and memorize it (depending on your decision)
- ::
- iterate to the Controls-Collection of your Container
- set the Size of each Control to the new (calculated) Fontsize

Here is a method to get the maximum matching Fontsize for a given Size of a Control depending on it's Text-Content and the selected Font and it's Style :
Function GetFontSizeMatch(ByVal myText As String, ByVal myFont As Font, ByVal mySize As Size) As Single

        If Trim(myText).Length <= 0 Then myText = "X"

        Dim currentSize As Single = CSng(myFont.Size)
        Dim workFont As Font = New Font(myFont.Name, currentSize, myFont.Style, myFont.Unit)
        Dim myTextSize As SizeF

        If (mySize.Width >= 1) AndAlso (mySize.Height >= 1) Then
            Do
                currentSize += 4.0 : If currentSize > 50.0 Then Exit Do
                workFont = New Font(workFont.Name, currentSize, workFont.Style, workFont.Unit)
                myTextSize = TextRenderer.MeasureText(myText, workFont)
            Loop Until (myTextSize.Width > mySize.Width) Or (myTextSize.Height > mySize.Height)

            Do
                currentSize -= 0.5 : If currentSize < 5.0 Then Exit Do
                workFont = New Font(workFont.Name, currentSize, workFont.Style, workFont.Unit)
                myTextSize = TextRenderer.MeasureText(myText, workFont)
            Loop Until (myTextSize.Width <= mySize.Width) AndAlso (myTextSize.Height <= mySize.Height)

        End If

        Return currentSize

    End Function


这篇关于如何在VB.NET 2015中的winform应用程序中制作自动控件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆