2008-09-05 11 views
9

私はC#winformsアプリケーション(VS.NET 2008、.NET 3.5 sp 1)を開発中です。フォーム上に検索フィールドがあり、検索フィールドの横にラベルを表示するのではなく、検索フィールド自体の背景にグレーのテキストを表示したいとします(たとえば、「検索用語」など)。ユーザーが検索フィールドにテキストを入力すると、テキストが消えます。どうすればこれを達成できますか?C#winforms編集コントロールのヒントを表示

答えて

9

これを行うには、P/Inovke interopコードを使用する必要があります。 Win32 API SendMessage関数とEM_SETCUEBANNERメッセージを探します。

0

テキストボックスコントロールには、​​とAutoCompleteSourceという組み込みの機能があります。彼らはカスタムやサードパーティのコントロールに入る前に試してみる価値があるかもしれません。

1

このリンクのコードを試してみてください。これは、すぐにすぐに実現することは不可能なので、winformsの機能を拡張します。ソースコードも利用可能です。 Windows XP以降でのみ動作することを覚えておいてください。

http://www.aaronlerch.com/blog/2007/12/01/watermarked-edit-controls/

2

私はこれが一般的に行われている方法は、お使いのヒントテキストでそれをグレーと事前入力するテキストの色を設定することだと思います。

次に、テキストフィールドのフォーカスイベントのハンドラを作成し、フォーカスが取得されて失われたときにフィールドの内容と色に基づいて変更します。ここではいくつかの擬似コードは(。申し訳ありませんが、それは完全にC#コードではありません、私は今、脳にActionScriptを持っている)だ:

TextInput myInput; 
boolean showingHint = true; 

myInput.text = "Search Terms"; 
myInput.color = 0xcccccc; 

myInput.onFocusGained = function() { 
    if(showingHint) { 
    myInput.text = ""; 
    myInput.color = 0x000000; 
    showingHint = false; 
    } 
} 

myInput.onFocusLost = function() { 
    if(!showingHint && myInput.text.length == 0) { 
    myInput.text = "Search Terms"; 
    myInput.color = 0xcccccc; 
    showingHint = true; 
    } 
} 

は、ユーザーが手動で変更されていない場合にのみ、失われたフォーカス上のテキストを変更したい、忘れないでくださいテキスト。ヒントを表示しているかどうかを追跡するには、別のブール値を使用して、ユーザーが手動でヒントテキストを実際のコンテンツとして入力するかどうかを区別することができます。同様に、ヒントが表示されている場合にのみ、入力ボックスの内容を消去して、誤ってユーザー入力を取り除かないようにしたいとします。

4

リンクの代わりにコードを投稿する方が良いです。私はこれを投稿していますhere

  //Copyright (c) 2008 Jason Kemp 

      //Permission is hereby granted, free of charge, to any person obtaining a copy 
      //of this software and associated documentation files (the "Software"), to deal 
      //in the Software without restriction, including without limitation the rights 
      //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
      //copies of the Software, and to permit persons to whom the Software is 
      //furnished to do so, subject to the following conditions: 

      //The above copyright notice and this permission notice shall be included in 
      //all copies or substantial portions of the Software. 

      //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
      //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
      //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
      //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
      //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
      //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
      //THE SOFTWARE. 

      using System; 
      using System.Runtime.InteropServices; 
      using System.Windows.Forms; 
      using System.Text; 


      public static class Win32Utility 
      { 
       [DllImport("user32.dll", CharSet = CharSet.Auto)] 
       private static extern Int32 SendMessage(IntPtr hWnd, int msg, 
        int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); 

       [DllImport("user32.dll")] 
       private static extern bool SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder lParam); 

       [DllImport("user32.dll")] 
       private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi); 

       [StructLayout(LayoutKind.Sequential)] 
       private struct COMBOBOXINFO 
       { 
        public int cbSize; 
        public RECT rcItem; 
        public RECT rcButton; 
        public IntPtr stateButton; 
        public IntPtr hwndCombo; 
        public IntPtr hwndItem; 
        public IntPtr hwndList; 
       } 

       [StructLayout(LayoutKind.Sequential)] 
       private struct RECT 
       { 
        public int left; 
        public int top; 
        public int right; 
        public int bottom; 
       } 

       private const int EM_SETCUEBANNER = 0x1501; 
       private const int EM_GETCUEBANNER = 0x1502; 

       public static void SetCueText(Control control, string text) 
       { 
        if (control is ComboBox) 
        { 
         COMBOBOXINFO info = GetComboBoxInfo(control); 
         SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text); 
        } 
        else 
        { 
         SendMessage(control.Handle, EM_SETCUEBANNER, 0, text); 
        } 
       } 

       private static COMBOBOXINFO GetComboBoxInfo(Control control) 
       { 
        COMBOBOXINFO info = new COMBOBOXINFO(); 
        //a combobox is made up of three controls, a button, a list and textbox; 
        //we want the textbox 
        info.cbSize = Marshal.SizeOf(info); 
        GetComboBoxInfo(control.Handle, ref info); 
        return info; 
       } 

       public static string GetCueText(Control control) 
       { 
        StringBuilder builder = new StringBuilder(); 
        if (control is ComboBox) 
        { 
         COMBOBOXINFO info = new COMBOBOXINFO(); 
         //a combobox is made up of two controls, a list and textbox; 
         //we want the textbox 
         info.cbSize = Marshal.SizeOf(info); 
         GetComboBoxInfo(control.Handle, ref info); 
         SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder); 
        } 
        else 
        { 
         SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder); 
        } 
        return builder.ToString(); 
       } 
      } 
関連する問題