gpt4 book ai didi

c# - TextBox.TextAlign 右侧对齐在某些情况下不起作用?

转载 作者:行者123 更新时间:2023-12-02 17:45:26 24 4
gpt4 key购买 nike

我的 Visual C# Express 2010 表单应用程序中有一个路径选择器。

我使用 FolderBrowserDialog 和(单行)TextBox 来显示所选路径。在我的 UI 刷新代码中使用以下行。

this.textBoxFolder.Text = this.folderBrowserDialog1.SelectedPath;

使用表单设计器将 ReadOnly 属性设置为 true 并将 TextAlign 属性设置为 Right,因为选择的路径通常比 TextBox 长,我更喜欢显示路径的右侧。表单设计器生成这个:

// 
// textBoxFolder
//
this.textBoxFolder.Location = new System.Drawing.Point(40, 72);
this.textBoxFolder.Name = "textBoxFolder";
this.textBoxFolder.ReadOnly = true;
this.textBoxFolder.Size = new System.Drawing.Size(160, 20);
this.textBoxFolder.TabIndex = 13;
this.textBoxFolder.TabStop = false;
this.textBoxFolder.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

只要所选路径短于文本框大小时,右对齐就会起作用。 (但这并不重要)

只要所选路径长于文本框大小时,右对齐无效,文本框中的字符串显示为最左边的字符可见,最右边的字符隐藏。

我知道在普通的单行 TextBox (ReadOnly = false) 中,当手动输入过长的字符串时,即使焦点消失,最右边的字符也是可见的, 不管TextAlign 是否设置为左/右/居中!

换句话说,我的目标是,当 TextBox.Text 以编程方式设置(而不是键入),并且字符串长于 TextBox 的宽度时,我如何让最右边的字符可见?

最佳答案

而不是设置 TextAlign 属性,您应该将插入符号移动到最后一个字符:

textBoxFolder.Text = this.folderBrowserDialog1.SelectedPath;
textBoxFolder.SelectionStart = textBox1.Text.Length - 1;

设置SelectionStart实际上将插入符号移动到指定位置。这使得该位置的字符在 TextBox 中可见。

如果可以使用 Label 而不是文本框,则可以使用 one created by Hans Passant here使用 TextFormatFlags.PathEllipses绘制文本时标记。

关于c# - TextBox.TextAlign 右侧对齐在某些情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15379731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com