MyDaftQuestions
MyDaftQuestions

Reputation: 4701

Textbox SpellCheck.IsEnabled - How to count

My Textbox needs to work out the number of spelling errors occur in a textbox.

My research has shown me how to get the spelling errors to work, using

<TextBox Text="{Binding Content}" SpellCheck.IsEnabled="True" Language="en-GB" />

I was slightly annoyed that I can't have IsReadOnly set to true but, I guess I have to live with it.

What I can't find out is how to know how many spelling issues/errors are in the Textbox. All I can find is http://msdn.microsoft.com/en-us/library/system.windows.controls.spellcheck%28v=vs.110%29.aspx which doesn't say it does but I'm not losing hope!

I tried to add

        TextBox tx = new TextBox();
        tx.SpellCheck.IsEnabled = true;
        tx.Text = "saf and tre";

        var split = tx.Text.Split(' ');
        var errors = 0;
        foreach (var s in split)
        {
            var tempTb = new TextBox();
            tempTb.Text = s;

            SpellingError e = tempTb.GetSpellingError(0); // always null
            var a = tempTb.GetSpellingErrorLength(0);
            var b = tempTb.GetSpellingError(0);
            var c = tempTb.GetSpellingErrorStart(0);

            if ( tempTb.GetSpellingErrorLength(0) >= 0)
                errors++;
        }

If I update the code from

            SpellingError e = tempTb.GetSpellingError(0); // always null

to

            SpellingError e = tx.GetSpellingError(0); // not null

Then it provides suggestions which then informs me it's wrong (and I can perform a count).

To get around the issue I'm having to do

        TextBox tx = new TextBox();
        tx.SpellCheck.IsEnabled = true;
        tx.Text = "saf many tre further more i sense taht nothing is what is";

        var split = tx.Text.Split(' ');
        var errors = 0;
        var start = 0;
        foreach (var s in split)
        {
            var tempTb = new TextBox();
            tempTb.Text = s;                

            SpellingError f = tx.GetSpellingError(start);

            start += s.Length + 1;

            if (f!=null)
                errors++;
        }

Why does it not work for tempTb?

Upvotes: 2

Views: 982

Answers (2)

jordanhill123
jordanhill123

Reputation: 4182

It appears from my debugging that @EdSF is correct and SpellCheck.IsEnabled must be set for the temporary TextBox

Code used to reproduce this:

void initTest()
{
    TextBox tx = new TextBox();
    tx.SpellCheck.IsEnabled = true;
    tx.Text = "saf and tre";

    var split = tx.Text.Split(' ');
    var errors = 0;
    foreach (var s in split)
    {
        var tempTb = new TextBox();
        tempTb.SpellCheck.IsEnabled = true;  // Added this line
        tempTb.Text = s;

        SpellingError e = tempTb.GetSpellingError(0); // no longer always null
        var a = tempTb.GetSpellingErrorLength(0);
        var b = tempTb.GetSpellingError(0);
        var c = tempTb.GetSpellingErrorStart(0);

        //if (tempTb.GetSpellingErrorLength(0) >= 0)  //doesn't appear to be correct 
        if (e != null)
        {
            errors++;
        }
    }
}

Upvotes: 2

MyDaftQuestions
MyDaftQuestions

Reputation: 4701

I found it after I posted

There is

GetSpellingErrorStart()
GetSpellingError()
GetSpellingErrorLength()
SpellingError e = tempTb.GetSpellingError(0);

EG

        TextBox tx = new TextBox();
        tx.SpellCheck.IsEnabled = true;
        tx.Text = "saf";
        var reslt = tx.GetSpellingErrorStart(0);            

Upvotes: 0

Related Questions