Knowledgebase Home Page  >  RapidSpell Desktop .NET  >  Behavior Customization
Search the Knowledge Base
How can I spell check the exact user selection only? (C#)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=108&catId=62

Options

Print this page
Email this to a friend
By default, RapidSpellDialog will spell check the users selection (adjusted around the closest word boundaries) first and then give them option to continue checking the rest of the text.  This is standard behaviour, however if you wish to have the spell checker check only the selected text, and not to adjust the selection to the word boundaries, you need to do some behavior customization.
 
 
First thing to understand is that we need to wrap the text box with our own class, this way we can return what we want to check (just the selection);

So add this class to your project;

using System;

using System.Windows.Forms;

using Keyoti.RapidSpell;

 

namespace GUIExample

{

///<summary>This class simply wraps a TextBoxBase object in an ISpellCheckableTextComponent interface.</summary>

public class SelectionOnlyInterface: ISpellCheckableTextComponent

{

 

//The text component, as a TextBoxBase.

TextBoxBase tbb;

int originalSelStart = 0, originalSelLen=0;

///<summary>Creates a new proxy with a TextBoxBase</summary>

public SelectionOnlyInterface(TextBoxBase textBox)

{

this.tbb = textBox;

this.tbb.Enter += new EventHandler(this.FocusPiper);

}

public void Dispose()

{

if(this.tbb != null)

this.tbb.Enter -= new EventHandler(this.FocusPiper);

}

///<summary></summary>

public bool HideSelection

{

get { return tbb.HideSelection; }

set { tbb.HideSelection = value;}

}

///<summary></summary>

public string Text

{

get {

originalSelStart = tbb.SelectionStart;

originalSelLen = tbb.SelectionLength;

return tbb.SelectedText;

}

set {

//this isn't actually called.

tbb.Text = value;

}

}

///<summary></summary>

public int SelectionStart

{

get { return tbb.SelectionStart - originalSelStart; }

set { tbb.SelectionStart = originalSelStart + value; }

}

///<summary></summary>

public int SelectionLength

{

get { return tbb.SelectionLength; }

set { tbb.SelectionLength = value; }

}

///<summary></summary>

public string SelectedText

{

get { return tbb.SelectedText; }

set { tbb.SelectedText = value; }

}

///<summary></summary>

public System.Windows.Forms.Control Parent

{

get { return tbb.Parent; }

set { tbb.Parent = value; }

}

///<summary></summary>

public void ScrollToCaret()

{

tbb.ScrollToCaret();

}

///<summary></summary>

public event EventHandler GotFocus;

///<summary>Called when tbb receives focus, passes event onto any GotFocus listeners of this obj.</summary>

public void FocusPiper(object sndr, EventArgs args)

{

if (GotFocus!=null) GotFocus(this, args);

}

}




If you do this, and then set the spell checker to work with this class instead of the text box,
 
eg.

private void Form1_Load(object sender, System.EventArgs e) {

    this.rapidSpellDialog1.UserInterfaceFormProvider = new CustomUserInterfaceFormProvider();

}

private void OnCheckSpellingClicked(object sender, System.EventArgs e) {

    this.rapidSpellDialog1.Check();

}


then it will spell check only the selection, however the problem is, it will then ask if you want to spell check the rest of the text, and then it will crash if the user says yes. To prevent that, you need to disable this behaviour, you do that by subclassing RapidSpellGUI

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

namespace GUIExample

{

    class CustomUserInterfaceFormProvider : Keyoti.RapidSpell.IUserInterfaceFormProvider

    {

        public Keyoti.RapidSpell.IUserInterfaceForm CreateUserInterfaceForm(){ return new CustomGUI(); }

    }

 
 

    public class CustomGUI : Keyoti.RapidSpell.RapidSpellGUI{

        protected override void ProceedFromEnd(){

            End();

        }

    }

}


and then by activating your subclass, with;

this.rapidSpellDialog1.ThirdPartyTextComponentToCheck = new SelectionOnlyInterface(this.textBox1);


eg.


private void Form1_Load(object sender, System.EventArgs e) {

    this.rapidSpellDialog1.ThirdPartyTextComponentToCheck = new SelectionOnlyInterface(this.textBox1);

    this.rapidSpellDialog1.UserInterfaceFormProvider = new CustomUserInterfaceFormProvider();

}

 
 
That should do it, if you have trouble, please post a comment or email support@keyoti.com
 
 

Related Questions:

Attachments:

No attachments were found.