TextBox, ComboBox (input controls) đều có hỗ trợ thuộc tính AutoComplete source. Hiện Fox cũng đang thực hiện một công việc tương tụ như bạn, gõ code tiền tệ sẽ hiện một ContextMenu suggest lên. Có đoạn code như sau:
Mã:
// Lấy DataSource cho input controls (ComboBox) - Chưa được tối ưu về performance...private void FillBomboBoxDataSource(){ AutoCompleteStringCollection autocomplete = new AutoCompleteStringCollection(); foreach (SupportedCurrency currency in mAdapter.SupportCurrencies) { autocomplete.Add(currency.ToString()); fromCurrencyComboBox.Items.Add(currency); fromCurrencyComboBox.AutoCompleteMode = AutoCompleteMode.Suggest; fromCurrencyComboBox.AutoCompleteCustomSource = autocomplete; toCurrencycomboBox.Items.Add(currency); toCurrencycomboBox.AutoCompleteMode = AutoCompleteMode.Suggest; toCurrencycomboBox.AutoCompleteCustomSource = autocomplete; } } // Khi gõ xong một phím, sẽ báo cho comboBox tìm những giá trị suggest private void AutoCompleteComboBoxKeyUp(ComboBox comboBox, KeyEventArgs e) { Keys ignoreKeys = e.KeyCode; if (ignoreKeys == Keys.Back || ignoreKeys == Keys.Up || ignoreKeys == Keys.Left || ignoreKeys == Keys.Right || ignoreKeys == Keys.Delete || ignoreKeys == Keys.Down) return; string typedText = comboBox.Text; int foundIndex = comboBox.FindString(typedText); if (foundIndex > 0) { object foundItem = comboBox.Items[foundIndex]; string foundText = comboBox.GetItemText(foundItem); string appendText = foundText.Substring(typedText.Length); comboBox.Text = typedText + appendText; comboBox.SelectionStart = typedText.Length; comboBox.SelectionLength = appendText.Length; } } // Khi chọn được giá trị suggest mong muốn, hiển thị nó lên nền Text của ComboBoxprivate void AutoCompleteComboBox(ComboBox comboBox) { int index = comboBox.FindStringExact(comboBox.Text); comboBox.SelectedIndex = index; } // Đăng kí sự kiện OnLeave cho ComboBoxprivate void FromCurrencyComboBoxOnLeave(object sender, EventArgs e) { this.AutoCompleteComboBox(fromCurrencyComboBox); } //Đăng kí sự kiện KeyUp cho ComboBoxprivate void FromCurrencyComboBoxOnKeyUp(object sender, KeyEventArgs e) { this.AutoCompleteComboBoxKeyUp(fromCurrencyComboBox, e); } // Ở đây nếu bạn muốn bắt chước theo kiểu FireFox là vẫn hiện ContextMenu Suggest khi gõ ta sẽ dùng hàm API sau [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); // Sau đó đăng kí sự kiện GotFocus của ComboBox - Do GotFocus không phải lạ sự kiện mặc định trong designer của ComboBox nên phải code tay ở constructor của Form chứa ComboBox ...public MainForm() {...fromCurrencyComboBox.GotFocus += new EventHandler(FromCurrencyComboBoxGotFocus); // Chỗ này ngay sau keyword 'new' nhấn phím 'Tab' 2 lần cho IDE tự sinh luôn nếu bạn dùng VS......} // Đây là hàm để CallBack - nhớ là tên hàm phải giống như khi đăng kí nhaprivate void FromCurrencyComboBoxGotFocus(object sender, EventArgs e) { IntPtr ptr = SendMessage(fromCurrencyComboBox.Handle, 0x14f, new IntPtr(-1), IntPtr.Zero); }
Ok đây là cách mình thực hiện và chạy rất êm, tuy là vấn đề của Fox, nhưng cũng mong bạn sẽ nhìn ra hướng giải quyết cho vấn đề.
Cheer
View more random threads:
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ