RichTextBox.AutoWordSelection broken?
I am writing a windows forms application in C# and I create a RichTextBox (via code, not the designer). I am setting the AutoWordSelection property to false, but when I highlight stuff in the box, it开发者_运维知识库 still jumps to the boundaries of words, plus a space. Is this a flaw in .NET or am I doing it wrong?
Using .NET 3.5 i still have this issue. This was reported to Microsoft and flagged as a "Wont Fix" in 2005. This is the latest news i can find on the issue.
Here is the MS Connect bug report: http://connect.microsoft.com/VisualStudio/feedback/details/115441/richtextboxs-autowordselection-property-does-not-work-correctly#details
Here is a more recent 2010 post about another person who noticed the problem: http://sonicflare.net/2010/01/10/shipped-bug-feature/#more-192
----------UPDATE-------------
I've made it work by placing an extra AutoWordSelection = False in the Form's Load event.
public Form1()
{
InitializeComponent();
rich = new RichTextBox();
rich.Size = new Size(150, 50);
rich.Text = "Ignoring a bug for five years does not make it a undocumented feature.";
rich.Location = new Point(20, 20);
rich.AutoWordSelection = false;
this.Controls.Add(rich);
}
private void Form1_Load(object sender, EventArgs e)
{
this.BeginInvoke(new EventHandler(delegate
{
rich.AutoWordSelection = false;
}));
}
Same problem here with RichTextBox in TabControl. It didn't matter if it was created in Designer or dynamically. The solution was, as Roast suggested in the comment below his answer, to use one of the tab events. After setting AutoWordSelection
to False
there, the problem would intermittently return when changing tabs. What fixed that was setting it to True
and then False
in the tab event.
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
RichTextBox1.AutoWordSelection = True
RichTextBox1.AutoWordSelection = False
End Sub
I also am dynamically creating rich text boxes and had the same selection problem. It took some doing, but I worked around it by essentially doing my own selection routine. There is some noticeable flicker whenever my routine disagrees with the control's default selection, but it's not too bad.
I created a private integer that keeps track of the selection's starting position. It's -1 by default. Then I implemented a MouseDown event handler to handle the pressing of the left mouse button. It discovers the character position at the mouse pointer and if it's not already inside the current selection, it sets the private integer to the current character position.
The MouseMove event handler then checks that the left mouse button is still pressed and updates the control's SelectionStart and SelectionLength properties, according to the saved starting position and the current character position. Note that the starting position is always the left side of the selection, so it follows the mouse when selecting text backwards.
It gets only slightly tricky if you want to also support drag-and-drop and selection margin.
I also encountered this, but with a tabbed editor with multiple RTBs. In this case, you can implement the workaround by setting the AutoWordSelection
property to False
in the code block that creates the RichTextBox. Like so:
Private Sub CreateNewRTBObject(ByVal items() As String)
Try
For Each s As String In items
If Not FilePaths.Contains(s) Then
rtb = New myRTB(s)
rtb.AutoWordSelection = False
End If
Next
tabs.SetTabWidth()
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Exclamation, title)
End Try
End Sub
精彩评论