<p>I'm working on a C# winforms application (VS.NET 2008, .NET 3.5 sp 1). I have a search field on a form, and rather than have a label next to the search field I'd like to show some grey text in the background of the search field itself ('Search terms', for example). When the user starts entering text in the search field the text should disappear. How can I achieve this?</p>

[{'answer_id': 46866, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 4, 'selected': True, 'text': '<p>You will need to use some P/Inovke interop code to do this. Look for the Win32 API <code>SendMessage</code> function and the <code>EM_SETCUEBANNER</code> message.</p>\n'}, {'answer_id': 46868, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 0, 'selected': False, 'text': '<p>There is built-in functionality in the text box control -- <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode.aspx" rel="nofollow noreferrer">AutoCompleteMode</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletesource.aspx" rel="nofollow noreferrer">AutoCompleteSource</a>.  They may be worth a try before you go into custom or 3rd party controls.</p>\n'}, {'answer_id': 46878, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 2, 'selected': False, 'text': '<p>I think the way this is generally done is to set the color of the text to gray and prefill it with your hint text.</p>\n\n<p>Then, write handlers for the text field\'s focus events, modifying the fields contents and color based when focus is gained and lost.  Here\'s some pseudocode (sorry, it\'s totally not C# code.  I have Actionscript on the brain right now):</p>\n\n<pre><code>TextInput myInput;\nboolean showingHint = true;\n\nmyInput.text = "Search Terms";\nmyInput.color = 0xcccccc;\n\nmyInput.onFocusGained = function() {\n  if(showingHint) {\n    myInput.text = "";\n    myInput.color = 0x000000;\n    showingHint = false;\n  }\n}\n\nmyInput.onFocusLost = function() {\n  if(!showingHint &amp;&amp; myInput.text.length == 0) {\n    myInput.text = "Search Terms";\n    myInput.color = 0xcccccc;\n    showingHint = true;\n  }\n}\n</code></pre>\n\n<p>Remember, you only want to change the text on focus lost if the user hasn\'t manually changed the text.  Use a separate boolean to track if you\'re showing the hint or not so you can differentiate between the user manually typing your "hint" text as actual content.  Similarly, you only want to clear the contents of the input box if the hint is being displayed so you don\'t get rid of user input accidentally.</p>\n'}, {'answer_id': 11971944, 'author': 'Esen', 'author_id': 1221319, 'author_profile': 'https://Stackoverflow.com/users/1221319', 'pm_score': 2, 'selected': False, 'text': '<p>Its better to post the code instead of link. I am posting this from <a href="http://www.ageektrapped.com/blog/wp-content/uploads/code/cuebanner.cs" rel="nofollow">here</a> </p>\n\n<pre><code>            //Copyright (c) 2008 Jason Kemp\n\n            //Permission is hereby granted, free of charge, to any person obtaining a copy\n            //of this software and associated documentation files (the "Software"), to deal\n            //in the Software without restriction, including without limitation the rights\n            //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n            //copies of the Software, and to permit persons to whom the Software is\n            //furnished to do so, subject to the following conditions:\n\n            //The above copyright notice and this permission notice shall be included in\n            //all copies or substantial portions of the Software.\n\n            //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n            //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n            //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n            //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n            //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n            //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n            //THE SOFTWARE.\n\n            using System;\n            using System.Runtime.InteropServices;\n            using System.Windows.Forms;\n            using System.Text;\n\n\n            public static class Win32Utility\n            {\n                [DllImport("user32.dll", CharSet = CharSet.Auto)]\n                private static extern Int32 SendMessage(IntPtr hWnd, int msg,\n                    int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);\n\n                [DllImport("user32.dll")]\n                private static extern bool SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder lParam);\n\n                [DllImport("user32.dll")]\n                private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi);\n\n                [StructLayout(LayoutKind.Sequential)]\n                private struct COMBOBOXINFO\n                {\n                    public int cbSize;\n                    public RECT rcItem;\n                    public RECT rcButton;\n                    public IntPtr stateButton;\n                    public IntPtr hwndCombo;\n                    public IntPtr hwndItem;\n                    public IntPtr hwndList;\n                }\n\n                [StructLayout(LayoutKind.Sequential)]\n                private struct RECT\n                {\n                    public int left;\n                    public int top;\n                    public int right;\n                    public int bottom;\n                }\n\n                private const int EM_SETCUEBANNER = 0x1501;\n                private const int EM_GETCUEBANNER = 0x1502;\n\n                public static void SetCueText(Control control, string text)\n                {\n                    if (control is ComboBox)\n                    {\n                        COMBOBOXINFO info = GetComboBoxInfo(control);\n                        SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text);\n                    }\n                    else\n                    {\n                        SendMessage(control.Handle, EM_SETCUEBANNER, 0, text);\n                    }\n                }\n\n                private static COMBOBOXINFO GetComboBoxInfo(Control control)\n                {\n                    COMBOBOXINFO info = new COMBOBOXINFO();\n                    //a combobox is made up of three controls, a button, a list and textbox;\n                    //we want the textbox\n                    info.cbSize = Marshal.SizeOf(info);\n                    GetComboBoxInfo(control.Handle, ref info);\n                    return info;\n                }\n\n                public static string GetCueText(Control control)\n                {\n                    StringBuilder builder = new StringBuilder();\n                    if (control is ComboBox)\n                    {\n                        COMBOBOXINFO info = new COMBOBOXINFO();\n                        //a combobox is made up of two controls, a list and textbox;\n                        //we want the textbox\n                        info.cbSize = Marshal.SizeOf(info);\n                        GetComboBoxInfo(control.Handle, ref info);\n                        SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder);\n                    }\n                    else\n                    {\n                        SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder);\n                    }\n                    return builder.ToString();\n                }\n            }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/46860', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/826/']