// selects the current item private bool SelectItem() { try { if (GridView) { if (Utility.isValidGrid(_grid.grdListDrug)) { //string[] arrValues = listBox.SelectedItem.ToString().Trim().Split('-'); //// set the Text of the TextBox to the selected item of the ListBox //if (arrValues.Length > 1) // this.Text = arrValues[1];//this.listBox.SelectedItem.ToString(); //else _Text = _grid.grdListDrug.GetValue(DmucThuoc.Columns.TenThuoc).ToString(); this.Select(this.Text.Length, 0); AllowTextChanged = false; _grid.AllowedChanged = AllowTextChanged; _grid.grdListDrug.MoveFirst(); AllowTextChanged = true; } } else { // if the ListBox is not empty if (((this.listBox.Items.Count > 0) && (this.SelectedIndex > -1))) { //string[] arrValues = listBox.SelectedItem.ToString().Trim().Split('-'); //// set the Text of the TextBox to the selected item of the ListBox //if (arrValues.Length > 1) // this.Text = arrValues[1];//this.listBox.SelectedItem.ToString(); //else this.Text = this.listBox.SelectedItem.ToString(); this.Select(this.Text.Length, 0); SelectedIndex = 0; } } // and hide the ListBox this.HideSuggestionListBox(); return(true); } catch { return(false); } finally { CurrentAutoCompleteList.Clear(); CurrentAutoCompleteList_IDAndCode.Clear(); CurrentAutoCompleteList_IDThuockho.Clear(); } }
public void ClearMe() { AllowTextChanged = false; this.Clear(); AllowChangedListBox = false; listBox.SelectedIndex = -1; AllowChangedListBox = true; CurrentAutoCompleteList.Clear(); CurrentAutoCompleteList_IDAndCode.Clear(); CurrentAutoCompleteList_IDThuockho.Clear(); setDefaultValue(); AllowTextChanged = true; this.HideSuggestionListBox(); }
// selects the current item private bool SelectItem() { try { if (GridView) { if (Utility.isValidGrid(_grid.grdListDrug)) { //string[] arrValues = listBox.SelectedItem.ToString().Trim().Split('-'); //// set the Text of the TextBox to the selected item of the ListBox //if (arrValues.Length > 1) // this._Text = arrValues[1];//this.listBox.SelectedItem.ToString(); //else _Text = _grid.grdListDrug.GetValue(DmucThuoc.Columns.TenThuoc).ToString(); MyID = Utility.Int32Dbnull(_grid.grdListDrug.GetValue("id_thuoc"), "-1"); MyCode = Utility.sDbnull(_grid.grdListDrug.GetValue("id_thuoc"), "-1"); id_thuockho = Utility.Int32Dbnull(_grid.grdListDrug.CurrentRow.Cells["id_thuockho"].Value, -1); this.Select(this.Text.Length, 0); AllowTextChanged = false; _grid.AllowedChanged = AllowTextChanged; _grid.grdListDrug.MoveFirst(); AllowTextChanged = true; } } else { // if the ListBox is not empty if (((this.listBox.Items.Count > 0) && (this.SelectedIndex > -1))) { if (CurrentAutoCompleteList.Count != CurrentAutoCompleteList_IDAndCode.Count || listBox.SelectedIndex < 0) { return(false); } string[] arrValues = CurrentAutoCompleteList_IDAndCode[listBox.SelectedIndex].ToString().Trim().Split(splitCharIDAndCode); // set the Text of the TextBox to the selected item of the ListBox if (arrValues.Length == 2) { if (txtMyID != null) { txtMyID.Text = arrValues[0]; } if (txtMyID_Edit != null) { txtMyID_Edit.Text = arrValues[0]; } if (txtMyCode != null) { txtMyCode.Text = arrValues[1]; } if (txtMyCode_Edit != null) { txtMyCode_Edit.Text = arrValues[1]; } MyID = arrValues[0]; MyCode = arrValues[1]; //find id_thuockho //DataRow[] arrDr = dtData.Select("id_thuoc=" + MyID); //if (arrDr.Length > 0) id_thuockho = Utility.Int32Dbnull(CurrentAutoCompleteList_IDThuockho[listBox.SelectedIndex], -1); if (txtMyName != null) { txtMyName.Text = listBox.SelectedItem.ToString(); } if (txtMyName_Edit != null) { txtMyName_Edit.Text = listBox.SelectedItem.ToString(); } } this._Text = this.listBox.SelectedItem.ToString(); this.Select(this.Text.Length, 0); SelectedIndex = 0; } } // and hide the ListBox this.HideSuggestionListBox(); return(true); } catch { return(false); } finally { CurrentAutoCompleteList.Clear(); CurrentAutoCompleteList_IDAndCode.Clear(); CurrentAutoCompleteList_IDThuockho.Clear(); } }
// This is a timecritical part // Fills/ refreshed the CurrentAutoCompleteList with appropreate candidates private void UpdateCurrentAutoCompleteList() { if (GridView) { LoadFilterDrugCode(this.Text); } else { string myText = Utility.DoTrim(this.Text); if (myText == "") { myText = " "; } // the list of suggestions has to be refreshed so clear it CurrentAutoCompleteList.Clear(); CurrentAutoCompleteList_IDAndCode.Clear(); CurrentAutoCompleteList_IDThuockho.Clear(); // an find appropreate candidates for the new CurrentAutoCompleteList in AutoCompleteList foreach (string Str in AutoCompleteList) { string _value = Str; if (CompareNoID && Str.IndexOf("#") >= 0) { _value = Str.Split('#')[1]; } // be casesensitive if (CaseSensitive) { // search for the substring (equal to SQL Like Command) if ((_value.IndexOf(myText) > -1)) // Add candidates to new CurrentAutoCompleteList { CurrentAutoCompleteList.Add(Str.Split(splitChar)[1]); CurrentAutoCompleteList_IDAndCode.Add(Str.Split(splitChar)[0]); CurrentAutoCompleteList_IDThuockho.Add(Str.Substring(Str.LastIndexOf("$") + 1)); } } // or ignore case else { // and search for the substring (equal to SQL Like Command) if ((_value.ToLower().IndexOf(myText.ToLower()) > -1)) // Add candidates to new CurrentAutoCompleteList { CurrentAutoCompleteList.Add(Str.Split(splitChar)[1]); CurrentAutoCompleteList_IDAndCode.Add(Str.Split(splitChar)[0]); CurrentAutoCompleteList_IDThuockho.Add(Str.Substring(Str.LastIndexOf("$") + 1)); } } } } #region Excursus: Performance measuring of Linq queries // This is a simple example of speedmeasurement for Linq querries /* * CurrentAutoCompleteList.Clear(); * Stopwatch stopWatch = new Stopwatch(); * stopWatch.Start(); * // using Linq query seems to be slower (twice as slow) * var query = * from expression in this.AutoCompleteList * where expression.ToLower().Contains(this.Text.ToLower()) * select expression; * foreach (string searchTerm in query) * { * CurrentAutoCompleteList.Add(searchTerm); * } * stopWatch.Stop(); * // method (see below) for printing the performance values to console * PrintStopwatch(stopWatch, "Linq - Contains"); */ #endregion Excursus: Performance measuring of Linq queries // countinue to update the ListBox - the visual part UpdateListBoxItems(); }
// shows the suggestions in ListBox beneath the TextBox // and fitting it into the TopLevelControl private void ShowSuggests() { if (!AllowTextChanged) { return; } // show only if MinTypedCharacters have been typed if (this.Text.Length >= MinTypedCharacters) { // prevent overlapping problems with other controls // while loading data there is nothing to draw, so suspend layout panel.SuspendLayout(); // user is typing forward, char has been added at the end of the former input if ((this.Text.Length > 0) && (this.oldText == this.Text.Substring(0, this.Text.Length - 1))) { //handle forward typing with refresh UpdateCurrentAutoCompleteList(); } // user is typing backward - char bas been removed else if ((this.oldText.Length > 0) && (this.Text == this.oldText.Substring(0, this.oldText.Length - 1))) { //handle backward typing with refresh UpdateCurrentAutoCompleteList(); } // something within has changed else { // handle other things like corrections in the middle of the input, clipboard pastes, etc. with refresh UpdateCurrentAutoCompleteList(); } if ((GridView && ((DataView)_grid.grdListDrug.DataSource).Count > 0) || ((CurrentAutoCompleteList != null) && CurrentAutoCompleteList.Count > 0)) { // finally show Panel and ListBox // (but after refresh to prevent drawing empty rectangles) panel.Show(); // at the top of all controls panel.BringToFront(); if (GridView) { _grid.grdListDrug.MoveFirst(); } if (!GridView) { listBox_SelectedIndexChanged(listBox, new EventArgs()); } // then give the focuse back to the TextBox (this control) this.Focus(); } // or hide if no results else { this.HideSuggestionListBox(); } // prevent overlapping problems with other controls panel.ResumeLayout(true); if (Utility.DoTrim(this.Text) == "") { setDefaultValue(); } } // hide if typed chars <= MinCharsTyped else { AllowChangedListBox = false; listBox.SelectedIndex = -1; AllowChangedListBox = true; CurrentAutoCompleteList.Clear(); CurrentAutoCompleteList_IDAndCode.Clear(); CurrentAutoCompleteList_IDThuockho.Clear(); setDefaultValue(); this.HideSuggestionListBox(); } if (RaiseEventEnterWhenEmpty && _OnSelectionChanged != null) { _OnSelectionChanged(); } if (Utility.DoTrim(this.Text) == "" && RaiseEventEnterWhenEmpty) { _OnEnterMe(); } }