locked
Programatically turning off autocompletion for a form or application RRS feed

  • Question

  • Hi,

    Was wondering if it's possible to programatically turn off autocompletion for all text boxes on a particular form. If not is it possible to do for a whole application? I would like to avoid turning it off for the whole device and was wondering if there's any way to control it from a C# CF application or is this purely a (global) device / OS setting.
    Robert Wilczynski
    Friday, February 26, 2010 8:09 PM

Answers

  • Have a nice present for You :).

    public static class InputContext
    {
    	private enum SHIC_FEATURE : uint
    	{
    		RESTOREDEFAULT = 0,
    		AUTOCORRECT = 1,
    		AUTOSUGGEST = 2,
    		HAVETRAILER = 3,
    		CLASS = 4
    	}
    
    	public enum CONTEXT_CLASS
    	{
    		DEFAULT,
    		EMAIL,
    		URL,
    		PHONE,
    		NAME,
    		PHONEANDEMAIL
    	}
    
    	[DllImport("aygshell.dll")]
    	private static extern int SHSetInputContext(IntPtr hwnd, SHIC_FEATURE dwFeature, ref bool lpValue);
    
    	[DllImport("aygshell.dll")]
    	private static extern int SHSetInputContext(IntPtr hwnd, SHIC_FEATURE dwFeature, string lpValue);
    
    	public static void SetClass(IntPtr handle, CONTEXT_CLASS cclass)
    	{
    		string className = "";
    			
    		switch(cclass)
    		{
    			case CONTEXT_CLASS.EMAIL:
    				className = "email";
    				break;
    
    			case CONTEXT_CLASS.URL:
    				className = "url";
    				break;
    
    			case CONTEXT_CLASS.PHONE:
    				className = "phone";
    				break;
    
    			case CONTEXT_CLASS.NAME:
    				className = "name";
    				break;
    
    			case CONTEXT_CLASS.PHONEANDEMAIL:
    				className = "phoneAndEmail";
    				break;
    		}
    
    		SHSetInputContext(handle, SHIC_FEATURE.CLASS, className); 
    	}
    
    	public static void SetHaveTrailer(IntPtr handle, bool enable)
    	{
    		SHSetInputContext(handle, SHIC_FEATURE.HAVETRAILER, ref enable);
    	}
    
    	public static void SetAutoSuggestion(IntPtr handle, bool enable)
    	{
    		SHSetInputContext(handle, SHIC_FEATURE.AUTOSUGGEST, ref enable);
    	}
    
    	public static void SetAutoCorrect(IntPtr handle, bool enable)
    	{
    		SHSetInputContext(handle, SHIC_FEATURE.AUTOCORRECT, ref enable);
    	}
    }

    Usage of this class is fairly simple:

    InputContext.SetAutoSuggestion(textBox1.Handle, false);   // disables word suggestions
    InputContext.SetAutoCorrect(textBox1.Handle, false);        // disables auto correction mechanism only

    You can also set the suggestion to a correct type. For example if You'd like to see only an email address suggestion in a particular textbox, You just need to:

    InputContext.SetClass(textBox1.Handle, CONTEXTCLASS.EMAIL);

    I hope You'll like it, because I've spend many hours hunting this solution and creating a simple class for my own purposes.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    • Proposed as answer by Mal Loth Friday, February 26, 2010 8:51 PM
    • Marked as answer by warrentang Wednesday, March 3, 2010 6:07 AM
    • Edited by Michel VerhagenMVP Saturday, March 17, 2018 9:25 PM Made code more readable
    Friday, February 26, 2010 8:51 PM

All replies

  • Have a nice present for You :).

    public static class InputContext
    {
    	private enum SHIC_FEATURE : uint
    	{
    		RESTOREDEFAULT = 0,
    		AUTOCORRECT = 1,
    		AUTOSUGGEST = 2,
    		HAVETRAILER = 3,
    		CLASS = 4
    	}
    
    	public enum CONTEXT_CLASS
    	{
    		DEFAULT,
    		EMAIL,
    		URL,
    		PHONE,
    		NAME,
    		PHONEANDEMAIL
    	}
    
    	[DllImport("aygshell.dll")]
    	private static extern int SHSetInputContext(IntPtr hwnd, SHIC_FEATURE dwFeature, ref bool lpValue);
    
    	[DllImport("aygshell.dll")]
    	private static extern int SHSetInputContext(IntPtr hwnd, SHIC_FEATURE dwFeature, string lpValue);
    
    	public static void SetClass(IntPtr handle, CONTEXT_CLASS cclass)
    	{
    		string className = "";
    			
    		switch(cclass)
    		{
    			case CONTEXT_CLASS.EMAIL:
    				className = "email";
    				break;
    
    			case CONTEXT_CLASS.URL:
    				className = "url";
    				break;
    
    			case CONTEXT_CLASS.PHONE:
    				className = "phone";
    				break;
    
    			case CONTEXT_CLASS.NAME:
    				className = "name";
    				break;
    
    			case CONTEXT_CLASS.PHONEANDEMAIL:
    				className = "phoneAndEmail";
    				break;
    		}
    
    		SHSetInputContext(handle, SHIC_FEATURE.CLASS, className); 
    	}
    
    	public static void SetHaveTrailer(IntPtr handle, bool enable)
    	{
    		SHSetInputContext(handle, SHIC_FEATURE.HAVETRAILER, ref enable);
    	}
    
    	public static void SetAutoSuggestion(IntPtr handle, bool enable)
    	{
    		SHSetInputContext(handle, SHIC_FEATURE.AUTOSUGGEST, ref enable);
    	}
    
    	public static void SetAutoCorrect(IntPtr handle, bool enable)
    	{
    		SHSetInputContext(handle, SHIC_FEATURE.AUTOCORRECT, ref enable);
    	}
    }

    Usage of this class is fairly simple:

    InputContext.SetAutoSuggestion(textBox1.Handle, false);   // disables word suggestions
    InputContext.SetAutoCorrect(textBox1.Handle, false);        // disables auto correction mechanism only

    You can also set the suggestion to a correct type. For example if You'd like to see only an email address suggestion in a particular textbox, You just need to:

    InputContext.SetClass(textBox1.Handle, CONTEXTCLASS.EMAIL);

    I hope You'll like it, because I've spend many hours hunting this solution and creating a simple class for my own purposes.

    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting.

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    • Proposed as answer by Mal Loth Friday, February 26, 2010 8:51 PM
    • Marked as answer by warrentang Wednesday, March 3, 2010 6:07 AM
    • Edited by Michel VerhagenMVP Saturday, March 17, 2018 9:25 PM Made code more readable
    Friday, February 26, 2010 8:51 PM
  • Thanks for your help - seems it's way more than I needed :)
    Robert Wilczynski
    Wednesday, March 3, 2010 11:00 AM
  • Thanks very much Mol Loth,

    This is hugely appreciated

    I have been hunting for months to find a solution to this. It was so easy in previous
    versions of WM just a tick box in the Input settings but you know Microsoft,
    they know best and removed this option.


    Thursday, July 5, 2012 5:25 PM
  • I'm trying to do the reverse process i.e. I an autosuggestion textbox in my windows ce application but is unable in doing so using this.

    so can you help me that what places I need to make the changes so to make this code work for me.

    like if I start to type sunday it should automatically provide me with the suggestion. Is this possible?

    Saturday, March 17, 2018 11:26 AM