ComboBox - Set first display item in drop down list

I

ian_jacobsen

I have a ComboBox that contains values 0, 50, 100, 150, 200, 250, 300,
350, 400, 450, 500. Let's say the user types in a value of 425, and
then clicks the drop down list. When the list drops down, the first
item in the list is 0. How can I set the first item in the list to the
nearest value, say 400, while retaining the value 425 in the Text
property? I know that this is done in Outlook (on the Time fields when
adding a new appointment to the calendar), so one should be able to do
this with a standard ComboBox control.
 
S

stephen.mcfall

You are looking for autocomplete functionality, you could inherit from
the ComboBox and write something like this ..

public void MyComboBox_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if(Char.IsControl(e.KeyChar))
{
return;
}

string toFind = this.Text.Substring(0, this.SelectionStart) +
e.KeyChar;
int index = this.FindStringExact(toFind);

if(index == -1)
{
index = this.FindString(toFind);
LastSelectedIndex = index;
}

if (index == -1)
{
return;
}

this.SelectedIndex = index;
this.SelectionStart = toFind.Length;
this.SelectionLength = this.Text.Length - this.SelectionStart;
e.Handled = true;
}

Or you could use the excellent Genghis library at
http://www.sellsbrothers.com/tools/genghis/ built by Chris Sells and
his mates
 
I

ian_jacobsen

Unfortunately I'm not looking for autocomplete functionality. 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.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

ian_jacobsen

Ok, I stumbled upon the solution myself. I'm using it as a time picker
control.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top