【问题标题】:AutoSize for Label / TextBox in .NET Compact Framework.NET Compact Framework 中标签/文本框的 AutoSize
【发布时间】:2009-04-20 08:08:28
【问题描述】:

对于在 .NET Compact Framework 中省略 Label 和 TextBox 控件的 AutoSize 属性,我简直要疯了。我有一个简单的应用程序,它应该在 TabControl 中列出一堆文本数据(通常在单行到几段文本之间)。其他一切工作顺利,但我尝试动态调整用于显示文本的 Label / TextBox 控件的大小却惨遭失败。

这是我尝试过的方法:

/*
Variables: 
s = The text intended for the TextBox
NewTB = TextBox object
width = Intended width
whiteSpaceAdjustment = amount of pixels per line to adjust "wasted" whitespace due to wrapping
*/

String[] linesArray = s.Replace(Environment.NewLine, "\n").Split(new char[] { '\n' });

int lines = 0;

int lineHeight = g.MeasureString(
        s.Replace("\n", "").Replace("\r", ""),
        LabelFont
    ).ToSize().Height;

foreach (String str in linesArray) {
    if (str.Length == 0) {
        lines++;
        continue;
    }
    szz = g.MeasureString(str, LabelFont).ToSize();
    lines += szz.Width / (width - whiteSpaceAdjustment);
    lines += (szz.Width % width) != 0 ? 1 : 0;
}
NewTB.Height = lines * lineHeight;
NewTB.Width = width;

...但问题是 whiteSpaceAdjustment 所需的范围太大。当它足够大以实际处理最极端的情况(段落主要由非常长的单词组成)时,大多数框最终都会太高一两行。

我可能不得不自己实现自动换行,但在我去那里之前,有没有人为此准备好一个干净的解决方案?

我将永远感激不尽!

【问题讨论】:

    标签: c# .net compact-framework


    【解决方案1】:

    试试这篇文章

    www.mobilepractices.com/2007/12/multi-line-graphicsmeasurestring.html

    确保您还查看了文章底部的链接,以便能够使用不同的字体。

    如果您使用的是 .Net CF 3.5,您可以将他们的示例转换为扩展方法。否则,我建议您创建一个继承自框架控件的新控件。

    【讨论】:

    • 别担心,我自己也有同样的问题。
    【解决方案2】:

    这是我为在 WinCE 中自动调整标签宽度而开发的。

    /// <summary>
    /// This class provides dynamic size labels, i.e. as the text grows lable width will grow with it. 
    /// </summary>
    public partial class AutoSizeLabel : UserControl
    {
        private string _strText;
        private const int padding = 10;
    
        public AutoSizeLabel()
        {
            InitializeComponent();
        }
    
        public override string Text
        {
            get
            {
                return _strText;
            }
            set
            {
                _strText = value;
                Refresh();
            }
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            SizeF size = pe.Graphics.MeasureString(this.Text, this.Font);
            this.Size = new Size((int)size.Width + padding, this.Height);
    
            if (this.Text.Length > 0)
            {
                pe.Graphics.DrawString(this.Text,
                    this.Font,
                    new SolidBrush(this.ForeColor),
                    (this.ClientSize.Width - size.Width) / 2,
                    (this.ClientSize.Height - size.Height) / 2);
            }
            // Calling the base class OnPaint
            base.OnPaint(pe);
        }
    }
    

    【讨论】:

    • 这对宽度很有效,谢谢分享。关于自动调整高度的任何线索?我必须明确设置它,因为没有它,标签的高度是 150 而不是 20。
    • 我发现从 this.Controls[i].Width 访问时宽度是正确的,仅供参考。
    • @Zacho:你想要固定宽度和动态高度的标签吗?
    • 不,我希望它完全动态。但是当我使用此代码时,默认情况下高度为 150。奇怪...
    • @Zacho:我不是 winCE 编程方面的专家(对此感到抱歉),但我认为您应该让一个字段固定以太宽度或高度,以便另一个字段可以是动态的。在给定的代码中,高度是固定的,并且在宽度上调整大小。
    猜你喜欢
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2011-01-03
    • 2010-10-09
    相关资源
    最近更新 更多