ComboBox - Set first display item in dropdown list
-
I have a value in the ComboBox that does not appear in its drop down list. When I drop down the list, I want the nearest match to be selected without changing the current value. So far I have a custom control that I inherited from System.Windows.Forms.ComboBox, and I have the following code attempting to do this. protected override void OnDropDown(EventArgs e) { string strValue = this.Text; int nValue = Convert.ToInt32(strValue); for (int i = 0; i < this.Items.Count; i++) { int nItem = Convert.ToInt32(this.Items[i].ToString()); if (nValue >= nItem && nValue < nItem + 50) { this.SelectedIndex = i; break; } } base.OnDropDown(e); this.Text = strValue; } If I remove "this.Text = strValue;", the nearest value is selected however the Text property will now contain this new selected value. I want to select the nearest value when the list drops down (for display purposes), but at the same time retain the value in the Text property. --IAN
-
I have a value in the ComboBox that does not appear in its drop down list. When I drop down the list, I want the nearest match to be selected without changing the current value. So far I have a custom control that I inherited from System.Windows.Forms.ComboBox, and I have the following code attempting to do this. protected override void OnDropDown(EventArgs e) { string strValue = this.Text; int nValue = Convert.ToInt32(strValue); for (int i = 0; i < this.Items.Count; i++) { int nItem = Convert.ToInt32(this.Items[i].ToString()); if (nValue >= nItem && nValue < nItem + 50) { this.SelectedIndex = i; break; } } base.OnDropDown(e); this.Text = strValue; } If I remove "this.Text = strValue;", the nearest value is selected however the Text property will now contain this new selected value. I want to select the nearest value when the list drops down (for display purposes), but at the same time retain the value in the Text property. --IAN
Hope this helps........... private string buffer = String.Empty; private Timer timer = null; const int MAX_BUFFERSIZE = 1024; this.timer = new Timer(); this.timer.Interval=1000; this.timer.Tick+=new EventHandler(this.TimerTick); this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPress); public void TimerTick(object sender,EventArgs eArgs) { this.buffer = String.Empty; this.timer.Stop(); } protected void OnKeyPress(object sender, KeyPressEventArgs e) { try { e.Handled = true; if ( this.buffer.Length == 0 ) { timer.Start(); } if ( buffer.Length < MAX_BUFFERSIZE ) { buffer += e.KeyChar.ToString(); Console.WriteLine(buffer); } int startIndex = 0; if ( this.SelectedIndex != -1 ) { startIndex = this.SelectedIndex; } bool found = false; for ( int index = startIndex; index <= this.Items.Count - 1; index++) { string text = String.Empty; if ( this.DisplayMember != String.Empty ) { PropertyInfo propertyInfo = base.Items[index].GetType().GetProperty(base.DisplayMember); text = propertyInfo.GetValue(base.Items[index], null).ToString(); } else { text = base.Items[index].ToString(); } if ( text.Length > this.buffer.Length ) { if ( text.ToUpper().StartsWith(buffer.ToUpper())) { found = true; this.SelectedIndex = index; break; } } } if ( !found ) { for ( int index = 0 ; index < startIndex; index++) { string text = String.Empty; if ( this.DisplayMember != String.Empty ) { PropertyInfo propertyInfo = base.Items[index].GetType().GetProperty(base.DisplayMember); text = propertyInfo.GetValue(base.Items[index], null).ToString(); } else { text = base.Items[index].ToString(); } if ( text.Length > this.buffer.Length ) { if ( text.ToUpper().StartsWith(buffer.ToUpper())) { this.SelectedIndex = index; break; } } } } } catch ( Exception ex ) { Console.WriteLine(ex.Message); } }
-
Hope this helps........... private string buffer = String.Empty; private Timer timer = null; const int MAX_BUFFERSIZE = 1024; this.timer = new Timer(); this.timer.Interval=1000; this.timer.Tick+=new EventHandler(this.TimerTick); this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPress); public void TimerTick(object sender,EventArgs eArgs) { this.buffer = String.Empty; this.timer.Stop(); } protected void OnKeyPress(object sender, KeyPressEventArgs e) { try { e.Handled = true; if ( this.buffer.Length == 0 ) { timer.Start(); } if ( buffer.Length < MAX_BUFFERSIZE ) { buffer += e.KeyChar.ToString(); Console.WriteLine(buffer); } int startIndex = 0; if ( this.SelectedIndex != -1 ) { startIndex = this.SelectedIndex; } bool found = false; for ( int index = startIndex; index <= this.Items.Count - 1; index++) { string text = String.Empty; if ( this.DisplayMember != String.Empty ) { PropertyInfo propertyInfo = base.Items[index].GetType().GetProperty(base.DisplayMember); text = propertyInfo.GetValue(base.Items[index], null).ToString(); } else { text = base.Items[index].ToString(); } if ( text.Length > this.buffer.Length ) { if ( text.ToUpper().StartsWith(buffer.ToUpper())) { found = true; this.SelectedIndex = index; break; } } } if ( !found ) { for ( int index = 0 ; index < startIndex; index++) { string text = String.Empty; if ( this.DisplayMember != String.Empty ) { PropertyInfo propertyInfo = base.Items[index].GetType().GetProperty(base.DisplayMember); text = propertyInfo.GetValue(base.Items[index], null).ToString(); } else { text = base.Items[index].ToString(); } if ( text.Length > this.buffer.Length ) { if ( text.ToUpper().StartsWith(buffer.ToUpper())) { this.SelectedIndex = index; break; } } } } } catch ( Exception ex ) { Console.WriteLine(ex.Message); } }
Unfortunately I'm not looking for autocomplete type of functionality. Let's say that combo box list contains times of the day in half hour increments (12:00 am, 12:30 am, 1:00 am, ...). This allows for easy selection of a time. However the user needs to be allowed to enter a time that does not appear in the list, say 1:09 am (and actually by default will be loaded with the current time). This is all easy to do, and does not require any extraordinary coding. Now for the tricky part. Let's say the combo box has a value of, say 3:12 pm. When the user clicks to drop down the list, the value 3:12 pm will not be found in the list, so it will be positioned with 12:00 am as the first item in the drop down list. Just as a nice to have, I'd like to see the list "auto-scroll" to the nearest value contained in the list when the list is dropped down. In this case it would be 3:00 pm. In addition, at the same time as we are "auto-scrolling", we need to retain the original value of 3:12 pm in the combo box. I know, quite a challenge. Microsoft currently has this type of functionality in Outlook when setting up a new appointment on the Calendar. If they can do, should we be able to do it? --Ian
-
Unfortunately I'm not looking for autocomplete type of functionality. Let's say that combo box list contains times of the day in half hour increments (12:00 am, 12:30 am, 1:00 am, ...). This allows for easy selection of a time. However the user needs to be allowed to enter a time that does not appear in the list, say 1:09 am (and actually by default will be loaded with the current time). This is all easy to do, and does not require any extraordinary coding. Now for the tricky part. Let's say the combo box has a value of, say 3:12 pm. When the user clicks to drop down the list, the value 3:12 pm will not be found in the list, so it will be positioned with 12:00 am as the first item in the drop down list. Just as a nice to have, I'd like to see the list "auto-scroll" to the nearest value contained in the list when the list is dropped down. In this case it would be 3:00 pm. In addition, at the same time as we are "auto-scrolling", we need to retain the original value of 3:12 pm in the combo box. I know, quite a challenge. Microsoft currently has this type of functionality in Outlook when setting up a new appointment on the Calendar. If they can do, should we be able to do it? --Ian
I have a solution to my post, and here it is.
public class TimePicker : ComboBox { bool m_fIsDropDown = false; string m_strTime = string.Empty; int m_nSelectedIndex = -1; public TimePicker() : base() { } private void LoadTimes() { if (this.Items.Count < 1) { DateTime t = new DateTime(2005, 10, 11, 0, 0, 0, 0); while (t < new DateTime(2005, 10, 12)) { this.Items.Add(t.ToShortTimeString()); t = t.AddMinutes(30); } } } protected override void OnCreateControl() { base.OnCreateControl(); LoadTimes(); } protected override void OnDropDown(EventArgs e) { base.OnDropDown(e); m_strTime = this.Text; DateTime dtmValue = DateTime.Now; try { dtmValue = Convert.ToDateTime(m_strTime); } catch { return; } for (int i = 0; i < this.Items.Count; i++) { DateTime dtmItem = Convert.ToDateTime(this.Items[i].ToString()); if (dtmValue >= dtmItem && dtmValue < dtmItem.AddMinutes(30)) { this.SelectedIndex = i; m_nSelectedIndex = i; break; } } m_fIsDropDown = true; } protected override void OnMouseDown(MouseEventArgs e) { if (m_fIsDropDown == true) { this.SelectedIndex = m_nSelectedIndex; this.Text = m_strTime; } else { base.OnMouseDown(e); } } protected override void OnMouseUp(MouseEventArgs e) { if (m_fIsDropDown == true) { this.SelectedIndex = m_nSelectedIndex; this.Text = m_strTime; m_fIsDropDown = false; } else { base.OnMouseUp(e); } } }
--IAN