vb.net 如何在设置为只读的 .NET TextBox 中选择(突出显示)文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18907302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:08:44  来源:igfitidea点击:

How to select (highlight) text in a .NET TextBox that is set for ReadOnly?

.netvb.nettextboxselection

提问by gonzobrains

My VB.NET programs displays text to the user that is loaded from a text file. I want to highlight specific parts automatically at various intervals depending on what the application is doing.

我的 VB.NET 程序向用户显示从文本文件加载的文本。我想根据应用程序在做什么,以不同的时间间隔自动突出显示特定部分。

I can manually set the highlighted text portion using the TextBoxcontrol's SelectionStartand SelectionLengthmembers. Unfortunately, this doesn't work if the TextBoxcontrol has its ReadOnlymember set to True. What is strange is that my program allows the user to manually click-drag selected text, but text does not get highlighted if the selection is performed programmatically.

我可以使用TextBox控件SelectionStartSelectionLength成员手动设置突出显示的文本部分。不幸的是,如果TextBox控件将其ReadOnly成员设置为,这将不起作用True。奇怪的是,我的程序允许用户手动单击并拖动所选文本,但如果以编程方式执行选择,则文本不会突出显示。

The text must be read-only since I do not want the user to modify it. Is there a solution to this?

文本必须是只读的,因为我不希望用户修改它。这个问题有方法解决吗?

回答by LarsTech

You can use the TextBox SelectionStart and SelectionLength properties, but I'm guessing since the TextBox is ReadOnly, it probably doesn't have the focus.

您可以使用 TextBox SelectionStart 和 SelectionLength 属性,但我猜因为 TextBox 是只读的,它可能没有焦点。

Try setting this property:

尝试设置此属性:

TextBox1.HideSelection = False

It should work as you expect to now.

它应该像你现在期望的那样工作。