2008-09-05 41 views
9

Tôi đang làm việc trên ứng dụng C# winforms (VS.NET 2008, .NET 3.5 sp 1). Tôi có một trường tìm kiếm trên biểu mẫu và thay vì có nhãn bên cạnh trường tìm kiếm, tôi muốn hiển thị một số văn bản màu xám trong nền của trường tìm kiếm (ví dụ: 'Cụm từ tìm kiếm'). Khi người dùng bắt đầu nhập văn bản vào trường tìm kiếm, văn bản sẽ biến mất. Làm thế nào tôi có thể đạt được điều này?Hiển thị gợi ý cho điều khiển chỉnh sửa winforms C#

Trả lời

9

Bạn sẽ cần phải sử dụng một số mã tương tác P/Inovke để thực hiện việc này. Hãy tìm hàm Win32 API SendMessage và thông báo EM_SETCUEBANNER.

0

Có chức năng tích hợp trong điều khiển hộp văn bản - AutoCompleteModeAutoCompleteSource. Chúng có thể đáng để thử trước khi bạn đi vào tùy chỉnh hoặc kiểm soát của bên thứ ba.

2

Tôi nghĩ rằng cách này thường được thực hiện là để thiết lập màu sắc của văn bản sang màu xám và điền trước nó với văn bản gợi ý của bạn.

Sau đó, viết trình xử lý cho các sự kiện tiêu điểm của trường văn bản, sửa đổi nội dung của trường và màu sắc dựa trên tiêu điểm được lấy và mất. Dưới đây là một số giả (xin lỗi, nó hoàn toàn không mã C# Tôi đã Actionscript trên não ngay bây giờ.):

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; 
    } 
} 

Hãy nhớ rằng, bạn chỉ muốn thay đổi các văn bản trên tập trung bị mất nếu người dùng đã không tự thay đổi bản văn. Sử dụng một boolean riêng để theo dõi nếu bạn đang hiển thị gợi ý hay không để bạn có thể phân biệt giữa người dùng bằng cách nhập văn bản "gợi ý" theo cách thủ công của bạn dưới dạng nội dung thực tế. Tương tự như vậy, bạn chỉ muốn xóa nội dung của hộp nhập liệu nếu gợi ý đang được hiển thị để bạn không bị vô tình loại bỏ đầu vào của người dùng.

4

Tốt hơn nên đăng mã thay vì liên kết. Tôi đang đăng nội dung này từ 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(); 
       } 
      } 
Các vấn đề liên quan