11

I am making a C# Windows Form Application in Visual Studio 2012. I want add a textbox with spell checking capabilities. Could you please explain me the process for it ?

leppie
  • 115,091
  • 17
  • 196
  • 297
user3218743
  • 579
  • 2
  • 8
  • 28
  • 1
    I've successfully used NHunspell plus a GUI component I found on CodeProject (I think this one: http://www.codeproject.com/Articles/73802/NHunspell-Component-for-Visual-Studio). – adv12 May 23 '14 at 16:36

5 Answers5

12

If you are using .net4 you can add the References System.Xaml and WindowsFormsIntegration to your Winforms project.

This allows you to find the ElementHost in you Toolbox. By using the ElementHost you can use WPF objects in your Winfroms project.

System.Windows.Forms.Integration.ElementHost elementHost1 = new System.Windows.Forms.Integration.ElementHost();
System.Windows.Controls.TextBox textBox = new System.Windows.Controls.TextBox();
textBox.SpellCheck.IsEnabled = true;
elementHost1.Child = textBox;
vicklik
  • 246
  • 1
  • 8
7

There is no built-in spellcheck capability on the Windows Forms textbox.

The best thing you can do is probably embed an WPF textbox in your form. Hans Passant gives a very thorough answer in this post on how to achieve that.

Community
  • 1
  • 1
julealgon
  • 7,072
  • 3
  • 32
  • 77
  • The problem with this is 4k monitor support. WPF and winforms don't play well together. I think WPF is more DPI aware, I'm not sure, but I do know when I added this component the project shrinks massively (to about 25% of original) when the component is called. I don't have a solution yet. – Rob Jun 24 '19 at 11:39
  • @Rob did you ever find a solution for this? I'm running into the same issue. – Phillip Schmidt Jan 30 '20 at 21:04
  • @PhillipSchmidt I added the Cspelling library - there is a link from CodeProject. – Rob Feb 14 '20 at 18:28
1

There is no WinForms capability for that. But, if you want to reuse it as a text box, Create a WPF UserControl and put a WPF TextBox in there. enable spell check. If you drag and drop an element host once, it will automatically add necessary references, after that, you will be able to see your user controls in the toolbox. once the usercontrol is visible, all you have to do is drag and drop it, it will automatically create an element host for you and put the wpf usercontrol in it.

Buzzzzzzz
  • 1,074
  • 13
  • 17
1

I know old link but Buzzzzz is correct. WinForms can't do it but it is really easy to create a wpf textbox or richtext box control and add it to your WinForms. Finding that darn property to tell the textbox to spell check is tricking but seriously,

  1. open or create a new winforms project,
  2. then menu project add new item,
  3. click wpf on the left and select select wpf control on the right **glitter, screen magic and you are looking at the weird layout if you are not familiar with WPF,
  4. select tools,
  5. select wpf textbox , drag and drop, BAM, sizing is a bit trial and error if you are new.
  6. Add references too WindowsBase, WindowsFormIntegration, UIAutomationProvider.
  7. Add PresentationCore and PresentationFramework if not already added, should have came in with the add new. Oh maybe System.Xaml but I think it is automatic too.
  8. Save it and rebuild.

You now have a control in your toolbox named whatever you names it. Drag and drop the control from the toolbox and there you go. To talk to it the defaults would be userControl1.TextBox.Text

Oh almost forgot. The Winforms and WPF are not all that friendly with each other and you will have to compile to remove the red line squiggles if they appear.

-1

Basically, you just need to set the SpellCheck.IsEnabled property to 'true'. Like this:

TextBox textBox = new TextBox();
textBox.SpellCheck.IsEnabled = true;

You can find this property in the System.Windows.Controls namespace, and reference it like this:

using System.Windows.Controls;

Editorial: I would strongly suggest using WPF over Winforms if that is an option you can explore. Winforms had its day once, but for more modern development, WPF is a much more powerful platform.

Brandon
  • 645
  • 5
  • 13
  • 1
    How can I get "System.Windows.Controls.TextBox". How to import that. It does not show me when typing "using.Windows.Controls..." ? – user3218743 May 23 '14 at 16:22
  • Thanks for the edit and the WPF suggestion. But when I add the line "using System.Windows.Controls;" it shows underlined in red. Do I have to refer it from anywhere or am I doing something wrong ? – user3218743 May 23 '14 at 16:31
  • 5
    There is no such property in WinForms, which was in the question title and the tags. This answer is only applicable if the OP completely rewrites his/her app in WPF. – adv12 May 23 '14 at 16:31
  • It is correctly working in WPF. Thank you. But what are the options if I explicitly do it on winforms. – user3218743 May 23 '14 at 16:36
  • @user3218743 - Please reject my answer as I wasn't able to post the link to embedding a WPF textbox into your Winforms app. julealgon's answer below is the more correct one. – Brandon May 23 '14 at 16:45