使用 AutoSize=false 防止标签中的自动换行 [英] Prevent word wrap in label with AutoSize=false

查看:26
本文介绍了使用 AutoSize=false 防止标签中的自动换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在设计器模式下的标签控件中防止自动换行,而其自动大小设置为false?

How to prevent word wrapping in Label control in designer mode, while it's auto size is set to false?

基本上我想在设计器模式下自由移动和调整标签大小,但它的文本应该总是在一行中(如果需要,可以剪切).

Basically I want to freely move and resize label in designer mode, but its text should be always in one line (cut if needed).

感谢您的帮助!

推荐答案

我能想到的唯一方法是创建一个自定义控件,该控件继承 Label 并覆盖 OnPaint 方法.

The only way I can think to do it is to create a custom control that inherits Label and override the OnPaint method.

public class CustomLabel : Label
{
    public CustomLabel()
    {            
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0);
    }
}

缺点是如果您想考虑 TextAlign、Padding 和 Margin,您需要在 Paint 方法中自行计算.

The downside is that if you want to take into account TextAlign, Padding and Margin you'll need to calculate it yourself in the paint method.

这篇关于使用 AutoSize=false 防止标签中的自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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