Knowledgebase Home Page  >  RapidSpell Desktop .NET  >  3rd Party Ctrls
Search the Knowledge Base
(C#) How can I spell check with RapidSpellDialog in the SyntaxBox control from Compona?
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=44&catId=61

Options

Print this page
Email this to a friend

(This article is intended for use with the RapidSpell Desktop .NET spell checker)

To use RapidSpellDialog with SyntaxBox a simple wrapper class is required (please see additional code at the bottom of this page for an improved 'code-aware' wrapper), which will implement the ISpellCheckableTextComponent interface - this process is described generally in the user guide PDF.

Create a new class, called SyntaxBoxAdapter;

using System;

using Keyoti.RapidSpell;

using System.ComponentModel;

namespace ComponaSyntaxBox_RSDT.NET

{

///

/// Wraps SyntaxBox in an interface that works with RapidSpell Desktop .NET

///

[ToolboxItem(true), DesignTimeVisible(true), Designer(typeof(System.ComponentModel.Design.ComponentDesigner)),

DefaultProperty("TextControl")]

public class SyntaxBoxAdapter : Keyoti.RapidSpell.ISpellCheckableTextComponent

{

Compona.Windows.Forms.SyntaxBoxControl textBox;

public Compona.Windows.Forms.SyntaxBoxControl TextControl

{

get{return textBox;}

set{textBox=value;

if(textBox!=null)

textBox.Enter+= new EventHandler(TBGotFocus);

}

}

 

///

Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public bool HideSelection

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return true;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

//can't do anything, not supported

}

}

 

///

Gets or sets the current text in the text box.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public string Text

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Document.Text; }

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Document.Text = value;

textBox.Refresh ();

}

}

 

///

Gets or sets a value indicating the currently selected text in the control.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public String SelectedText

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Selection.Text;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Selection.Text = value;

textBox.Refresh ();

}

}

///

Gets or sets the starting point of text selected in the text box.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public int SelectionStart

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Selection.SelStart;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Selection.ClearSelection();

textBox.Selection.SelStart = value;

textBox.Selection.SelLength=0;

//ensure the rows is visible (unfold collapsed sections)

Compona.SourceCode.Row row=textBox.Document[textBox.Selection.Bounds.FirstRow];

if (row == null) throw new InvalidOperationException ("Selection is out of bounds");

row.EnsureVisible ();

//place caret at selectionstart

textBox.Caret.SetPos (new Compona.SourceCode.TextPoint(this.textBox.Selection.Bounds.FirstColumn,this.textBox.Selection.Bounds.FirstRow));

textBox.Refresh ();

}

}

///

Gets or sets the number of characters selected in the text box.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public int SelectionLength

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Selection.SelLength;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Selection.SelLength = value;

textBox.Refresh ();

}

}

///

Scrolls to caret.

[Browsable(false)]

public void ScrollToCaret()

{

textBox.ScrollIntoView();

textBox.Refresh ();

}

///

Occurs when the control receives focus.

public event EventHandler GotFocus;

///

Called when the control receives focus.

[Browsable(false)]

public void TBGotFocus(object sender, EventArgs e)

{

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

}

}

}

Note that it implements every member of ISpellCheckableTextComponent and maps it to the equivalent in SyntaxBox.

Once this class has been added it is very simple to interface this with RapidSpellDialog.

Instantiate the adapter as a class member;

SyntaxBoxAdapter adapter = new SyntaxBoxAdapter();

In the Form constructor, add the following;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

adapter.TextControl = this.syntaxBoxControl1;

this.rapidSpellDialog1.ThirdPartyTextComponentToCheck = adapter;

}

these calls first setup the adapter and then set RapidSpellDialog to check the adapter.

 

That's it, please see the attachment for a full project (you will need to re-reference the project DLLs with copies on your machine).

 

Code Aware Wrapper

This wrapper was created by Compona to enable developers to restrict spell checking to certain blocks of code (comments, strings etc).

Note: It should used be with the "csharp.syn" syntax file, eg:

adapter.TextControl = this.syntaxBoxControl1;

this.syntaxBoxControl1.Document.SyntaxFile = @"C:\Program Files\Compona\Demos\SyntaxBox\csharp.syn";

adapter.AddCheckedBlockType ("CS Multiline Comment");

adapter.AddCheckedBlockType ("CS Singleline Comment");

adapter.AddCheckedBlockType ("CS String");

adapter.AddCheckedBlockType ("XML String");

this.rapidSpellDialog1.ThirdPartyTextComponentToCheck = adapter;

 

Wrapper Class

using System;

using Keyoti.RapidSpell;

using System.ComponentModel;

namespace ComponaSyntaxBox_RSDT.NET

{

///

/// Wraps SyntaxBox in an interface that works with RapidSpell Desktop .NET

///

[ToolboxItem(true), DesignTimeVisible(true), Designer(typeof(System.ComponentModel.Design.ComponentDesigner)),

DefaultProperty("TextControl")]

public class SyntaxBoxAdapter : Keyoti.RapidSpell.ISpellCheckableTextComponent

{

Compona.Windows.Forms.SyntaxBoxControl textBox;

public Compona.Windows.Forms.SyntaxBoxControl TextControl

{

get{return textBox;}

set

{

textBox=value;

if(textBox!=null)

textBox.Enter+= new EventHandler(TBGotFocus);

}

}

 

///

Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public bool HideSelection

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return true;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

//can't do anything, not supported

}

}

///

Gets or sets the current text in the text box.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public string Text

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

//spellcheck all blocktypes

if (blocksToCheck.Count == 0)

{

return textBox.Document.Text;

}

else

{

textBox.Document.ParseAll (true);

System.Text.StringBuilder sb=new System.Text.StringBuilder();

foreach (Compona.SourceCode.Row row in textBox.Document)

{

foreach (Compona.SourceCode.Word word in row)

{

if (word.Segment != null && blocksToCheck.Contains (word.Segment.BlockType))

{

sb.Append (word.Text);

}

else

{

string blank=new string (' ',word.Text.Length);

sb.Append (blank);

}

}

sb.Append (Environment.NewLine);

}

return sb.ToString ();

}

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Document.Text = value;

textBox.Refresh ();

}

}

///

Gets or sets a value indicating the currently selected text in the control.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public String SelectedText

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Selection.Text;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Selection.Text = value;

textBox.Refresh ();

textBox.Document.ParseAll (true);

}

}

///

Gets or sets the starting point of text selected in the text box.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public int SelectionStart

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Selection.SelStart;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Selection.ClearSelection();

textBox.Selection.SelStart = value;

textBox.Selection.SelLength=0;

//ensure the rows is visible (unfold collapsed sections)

Compona.SourceCode.Row row=textBox.Document[textBox.Selection.Bounds.FirstRow];

if (row == null) throw new InvalidOperationException ("Selection is out of bounds");

row.EnsureVisible ();

//place caret at selectionstart

textBox.Caret.SetPos (new Compona.SourceCode.TextPoint(this.textBox.Selection.Bounds.FirstColumn,this.textBox.Selection.Bounds.FirstRow));

textBox.Refresh ();

}

}

///

Gets or sets the number of characters selected in the text box.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public int SelectionLength

{

get

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

return textBox.Selection.SelLength;

}

set

{

if(textBox==null) throw new InvalidOperationException("SyntaxBoxAdapter.TextControl property not set to (non-null) textBox instance (set TextControl property before starting spell check).");

textBox.Selection.SelLength = value;

textBox.Refresh ();

}

}

///

Scrolls to caret.

[Browsable(false)]

public void ScrollToCaret()

{

textBox.ScrollIntoView();

textBox.Refresh ();

}

///

Occurs when the control receives focus.

public event EventHandler GotFocus;

///

Called when the control receives focus.

[Browsable(false)]

public void TBGotFocus(object sender, EventArgs e)

{

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

}

private System.Collections.ArrayList blocksToCheck = new System.Collections.ArrayList();

public void AddCheckedBlockType(string BlockName)

{

Compona.SourceCode.BlockType[] blocks = textBox.Document.Parser.Language.Blocks;

foreach (Compona.SourceCode.BlockType block in blocks)

{

if (block.Name.ToLower () == BlockName.ToLower ())

{

AddCheckedBlockType(block);

}

}

}

public void AddCheckedBlockType(Compona.SourceCode.BlockType BlockType)

{

blocksToCheck.Add (BlockType);

}

}

}

 

 

 

 


Related Questions:

Attachments: