RichTextBox AutoWordSelection-Does this Work???

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Mike in Paradise (anonymous_at_discussions.microsoft.com)
Date: 03/20/04


Date: Sat, 20 Mar 2004 08:56:09 -0800

Does the RichTextBox.AutoWordSelection work???

Run the program below to duplicate the problem and it seems to have no effect.

The default behaviour seems to select a word + THE SPACE AFTER IT??? Does this make sense?
 
Adding a FindReplace dialog to this is a PITA as when you pick up the AutoSelectedWord it is not really a word but:

 a word +<SPACE>

Now you don't know if this was the operators intention or not so you can't just strip off the space. Which causes a couple of problems.

1) This forces the Operator to enter the new word as newWord+<SPACE>
2) if the word is at the end of a line, it does not have a space after it so having the operator do 1)
is still a problem..

If you use this as an argument in the RichTextBox.Find() and use the:

 RichTextBoxFinds.WholeWord

then this does not match what the AutoWordSelection Picked up as the Selected Text in the first place..

Should not the AutoWordSelection=true and the RichTextBoxFinds.WholeWord work together as a default....

This means that the operator has to manually select a word in order for you to be able to properly determine if they
really want a word + SPACE for the find. So what is the point of doing an autowordselection at all???

I must be missing something here or the logic of this completely fails me!
Can anyone explain the logic of this to me..

PUZZLED in Paradise..

//************************* Code to Duplicate this *************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RichTextBoxSelection
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox wholeWord;
private System.ComponentModel.Container components = null;

public Form1()
{
        InitializeComponent();
        richTextBox1.HideSelection = false;
        richTextBox1.DataBindings.Add("AutoWordSelection",this.checkBox1,"Checked");
        String NL = Environment.NewLine;
        this.richTextBox1.Text
                = "Double click on -> Bug to see if a (skips this) Bug"
                + "\nPrevents the find using RichTextBoxFinds.WholeWord"
                + "\nfrom finding the next word Bug when you "
                + "\nPress the Find Next button"
                + "\n\n Now change the WholeWord and Try it again"
                + "\n\n Now change the AutoWordSelection and Try it again";
                

}
protected override void Dispose( bool disposing )
{
        if( disposing )
        {
                if (components != null)
                {
                        components.Dispose();
                }
        }
        base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.checkBox1 = new System.Windows.Forms.CheckBox();
        this.button1 = new System.Windows.Forms.Button();
        this.wholeWord = new System.Windows.Forms.CheckBox();
        this.SuspendLayout();
        //
        // richTextBox1
        //
        this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
        this.richTextBox1.Location = new System.Drawing.Point(0, 0);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(292, 168);
        this.richTextBox1.TabIndex = 0;
        this.richTextBox1.Text = "";
        //
        // checkBox1
        //
        this.checkBox1.Location = new System.Drawing.Point(8, 176);
        this.checkBox1.Name = "checkBox1";
        this.checkBox1.Size = new System.Drawing.Size(136, 24);
        this.checkBox1.TabIndex = 1;
        this.checkBox1.Text = "AutoWordSelection";
        this.checkBox1.CheckedChanged
                += new System.EventHandler(this.checkBox1_CheckedChanged);
        //
        // button1
        //
        this.button1.Location = new System.Drawing.Point(176, 216);
        this.button1.Name = "button1";
        this.button1.TabIndex = 2;
        this.button1.Text = "Find Next";
        this.button1.Click
                += new System.EventHandler(this.button1_Click);
        //
        // wholeWord
        //
        this.wholeWord.Location = new System.Drawing.Point(8, 200);
        this.wholeWord.Name = "wholeWord";
        this.wholeWord.Size = new System.Drawing.Size(136, 24);
        this.wholeWord.TabIndex = 3;
        this.wholeWord.Text = "Find Whole Word";
        this.wholeWord.Checked = true;
        //
        // Form1
        //
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.wholeWord);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.checkBox1);
        this.Controls.Add(this.richTextBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.TopMost = true;
        this.ResumeLayout(false);

}
#endregion
[STAThread]
static void Main()
{
        Application.Run(new Form1());
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
        bool x = this.richTextBox1.AutoWordSelection;
}
private void button1_Click(object sender, System.EventArgs e)
{
        RichTextBoxFinds findFlags = RichTextBoxFinds.None;
        if (this.wholeWord.Checked)
                findFlags = findFlags | RichTextBoxFinds.WholeWord;

        if (richTextBox1.SelectedText.Length == 0) return;
        int length = richTextBox1.SelectedText.Length;
        int start = this.richTextBox1.Find(
                richTextBox1.SelectedText //Text to Find
                ,richTextBox1.SelectionStart + 1//Starting Position for find
                ,richTextBox1.Text.Length //End Position for find
                ,findFlags //Find Flags
                );
        if (start > 0)
                richTextBox1.Select(start,length);
}
}
}


Quantcast