如何通过更改表单宽度自动复选框的文本? [英] How can I make the text of checkbox wraps automatically with changing form width?
问题描述
在窗体窗体中我有复选框列表,其中包含长文本,窗体大小可调整。
In windows forms I have checkbox list that contains long text, the form is re-sizable..
可以使文本在运行时自动换行到形式宽度?
can I make the text wraps automatically in run-time according to the form width ?
推荐答案
我尝试了很多方法,但最后这个工作经过很多研究: -
I tried many approaches but at last this one worked after a lot of researches:-
只是我使用两个事件来检测面板的大小变化,包含控件,然后我相应地调整控件。
simply I used two events to detect size change of the panel contains the controls then I adjusted the controls accordingly..
第一个事件是 LayoutEventHandler ,
检测到分辨率更改</ strong>
:
1-根据分辨率获得面板宽度(较低的接受分辨率为1024x768)
1- get the panel width considering the resolution (lower accepted resolution is 1024x768)
Rectangle resolution = Screen.PrimaryScreen.Bounds;
int panelWidth *= (int)Math.Floor((double)resolution.Width / 1024);
2-循环 所有控件 ,并调整控件宽度以适合面板宽度(i)减去垂直滚动宽度的10个像素),然后我从 MeasureString 函数,它接受控件文本,字体和控件宽度,并返回控件大小。
2- loop on all controls and adjust the control width to fit the panel width (i subtracted 10 pixels for vertical scroll width), then I got the control height from MeasureString function which takes the control text, the font and control width, and return the control size.
(例如,我将高度乘以大约因子1.25以克服线高和填充)
foreach (var control in controls)
{
if (control is RadioButton || control is CheckBox)
{
control.Width = panelWidth - 10;
Font fontUsed = control.Font;
using (Graphics g = control.CreateGraphics())
{
SizeF size = g.MeasureString(control.Text, fontUsed, control.Width);
control.Height = (int)Math.Ceiling(size.Height * 1.25);
}
}
}
这篇关于如何通过更改表单宽度自动复选框的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!