1. <i id='zFYJQ'><tr id='zFYJQ'><dt id='zFYJQ'><q id='zFYJQ'><span id='zFYJQ'><b id='zFYJQ'><form id='zFYJQ'><ins id='zFYJQ'></ins><ul id='zFYJQ'></ul><sub id='zFYJQ'></sub></form><legend id='zFYJQ'></legend><bdo id='zFYJQ'><pre id='zFYJQ'><center id='zFYJQ'></center></pre></bdo></b><th id='zFYJQ'></th></span></q></dt></tr></i><div id='zFYJQ'><tfoot id='zFYJQ'></tfoot><dl id='zFYJQ'><fieldset id='zFYJQ'></fieldset></dl></div>

    <small id='zFYJQ'></small><noframes id='zFYJQ'>

  2. <tfoot id='zFYJQ'></tfoot>

    • <bdo id='zFYJQ'></bdo><ul id='zFYJQ'></ul>
    1. <legend id='zFYJQ'><style id='zFYJQ'><dir id='zFYJQ'><q id='zFYJQ'></q></dir></style></legend>

      C# WinForms - 智能文本框控件根据文本框宽度自动格式化路径长度

      时间:2023-06-10
      • <bdo id='JBmmP'></bdo><ul id='JBmmP'></ul>

      • <tfoot id='JBmmP'></tfoot>
            <tbody id='JBmmP'></tbody>

          • <legend id='JBmmP'><style id='JBmmP'><dir id='JBmmP'><q id='JBmmP'></q></dir></style></legend>

            1. <small id='JBmmP'></small><noframes id='JBmmP'>

              1. <i id='JBmmP'><tr id='JBmmP'><dt id='JBmmP'><q id='JBmmP'><span id='JBmmP'><b id='JBmmP'><form id='JBmmP'><ins id='JBmmP'></ins><ul id='JBmmP'></ul><sub id='JBmmP'></sub></form><legend id='JBmmP'></legend><bdo id='JBmmP'><pre id='JBmmP'><center id='JBmmP'></center></pre></bdo></b><th id='JBmmP'></th></span></q></dt></tr></i><div id='JBmmP'><tfoot id='JBmmP'></tfoot><dl id='JBmmP'><fieldset id='JBmmP'></fieldset></dl></div>
                本文介绍了C# WinForms - 智能文本框控件根据文本框宽度自动格式化路径长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否存在可以根据文本框宽度显示路径的智能文本框控件 (WinForms).例如,如果路径很短,它将显示整个路径 (C:myfile.txt),但如果路径很长,它将显示开始和结束 (C:SomeFolder...fooMyFile.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...fooMyFile.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 这样做是不合适的,用户无法合理地编辑这样的缩写路径,您将不知道原始路径可能是什么.Label 是最好的控件.

                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.

                向您的项目添加一个新类并粘贴如下所示的代码.编译.将新控件从工具箱顶部拖放到表单上.不要太小.

                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 - 智能文本框控件根据文本框宽度自动格式化路径长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:WPF:从 WPF 文本框派生的控件中没有为空格键调用 OnKeyDown() 下一篇:.NET TextBox - 处理 Enter 键

                相关文章

                1. <legend id='eWEi9'><style id='eWEi9'><dir id='eWEi9'><q id='eWEi9'></q></dir></style></legend>
                2. <i id='eWEi9'><tr id='eWEi9'><dt id='eWEi9'><q id='eWEi9'><span id='eWEi9'><b id='eWEi9'><form id='eWEi9'><ins id='eWEi9'></ins><ul id='eWEi9'></ul><sub id='eWEi9'></sub></form><legend id='eWEi9'></legend><bdo id='eWEi9'><pre id='eWEi9'><center id='eWEi9'></center></pre></bdo></b><th id='eWEi9'></th></span></q></dt></tr></i><div id='eWEi9'><tfoot id='eWEi9'></tfoot><dl id='eWEi9'><fieldset id='eWEi9'></fieldset></dl></div>

                  • <bdo id='eWEi9'></bdo><ul id='eWEi9'></ul>

                    <small id='eWEi9'></small><noframes id='eWEi9'>

                  1. <tfoot id='eWEi9'></tfoot>