TextBox.TextAlign 右侧对齐在某些情况下没有效果? [英] TextBox.TextAlign right-side alignment has no effect in certain conditions?

查看:40
本文介绍了TextBox.TextAlign 右侧对齐在某些情况下没有效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a path selector in my Visual C# Express 2010 form application.

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

I do it using a FolderBrowserDialog and a (single line) TextBox, to show the selected path. Using the following line in my UI refresh code.

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

ReadOnly 属性设置为 trueTextAlign 属性设置为 Right 使用表单设计器,因为选择的路径通常比 TextBox 长,我更喜欢显示路径的右侧.表单设计器生成以下内容:

The ReadOnly property is set to true and TextAlign property is set to Right using the form designer, because the selected path is often longer than the TextBox, and I prefer to show the right-side of the path. The forms designer generates this:

// 
// 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;

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

Whenever the chosen path is shorter than the textbox size, the Right alignment works. (But this is not really important)

Whenever the chosen path is longer than the textbox size, the Right alignment has no effect, the string in the textbox is displayed such that the left-most character is visible, and right-most are hidden.

Whenever the chosen path is longer than the textbox size, the Right alignment has no effect, the string in the textbox is displayed such that the left-most character is visible, and right-most are hidden.

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

I know that in a normal single line TextBox (ReadOnly = false), when an overly-long string is typed in by hand, the right most chars are visible, even when focus goes away, regardless of whether TextAlign is set to Left / Right / Center!

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

In other words, my goal is, when TextBox.Text is programmatically set (as opposed to typed in), and the string is longer than the width of the TextBox, how do I get the right-most chars to be visible?

推荐答案

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

Instead of setting the TextAlign property, you should move the caret to the last character:

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

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

如果您可以使用 Label 代替文本框,则可以使用 Hans 创建的 Passant here 使用 TextFormatFlags.PathEllipses 标记,同时绘制文本.

If you can use a Label instead of a text box, you can use the one created by Hans Passant here that uses TextFormatFlags.PathEllipses flag while drawing the text.

这篇关于TextBox.TextAlign 右侧对齐在某些情况下没有效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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