C#的WinForms - 智能TextBox控件自动格式路径长度基于文本框的宽度 [英] C# WinForms - Smart TextBox Control to auto-Format Path length based on Textbox width

查看:274
本文介绍了C#的WinForms - 智能TextBox控件自动格式路径长度基于文本框的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道一个聪明的TextBox控件(的WinForms)的存在,可以显示这取决于文本框的宽度的路径。例如,如果路径短,将显示整个路径(C:\\ myfile.txt的),但如果道路是漫长它会显示开始和结束(C:\\ SomeFolder ... \\富\\ MyFile.txt的)。显示的字符的长度应使用其宽度文本框(动态地)计算出来的。任何商业或开源的建议,欢迎。非常感谢你。

Does a smart textbox control (WinForms) exists that can display a path depending on the textbox width. For example, if the path is short it will display the entire path (C:\myfile.txt), but if the path is long it will display the start and end (C:\SomeFolder...\foo\MyFile.txt). The length of the characters displayed should be calculated (dynamically) by the textbox using its width. Any commercial or open source suggestions are welcome. Thank you very much.

推荐答案

是的,这是TextRenderer.DrawText()方法的内置功能。它的一个重载接受TextFormatFlags参数,你可以通过TextFormatFlags.PathEllipsis。这样做的一个TextBox是不恰当的,用户不能合理编辑这样的缩写的路径,你会不知道原来的路径可能是。 A标签是最好的控制。

Yes, it's a built-in capability of the TextRenderer.DrawText() method. One of its overloads accepts a TextFormatFlags argument, you can pass TextFormatFlags.PathEllipsis. Doing this for a TextBox is not appropriate, the user cannot reasonably edit such an abbreviated path, you would have no idea what the original path might be. A Label is the best control.

添加一个新类到您的项目并粘贴如下所示的code。编译。从工具箱的上方新的控制到窗体。不要让它太小了。

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Don't make it too small.

using System;
using System.ComponentModel;
using System.Windows.Forms;

class PathLabel : Label {
  [Browsable(false)]
  public override bool AutoSize {
    get { return base.AutoSize; }
    set { base.AutoSize = false; }
  }
  protected override void OnPaint(PaintEventArgs e) {
    TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.PathEllipsis;
    TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, flags);
  }
}

这篇关于C#的WinForms - 智能TextBox控件自动格式路径长度基于文本框的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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