Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2005

Tip: Looking for answers? Try searching our database.

ComboBox - Set first display item in drop down list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ian_jacobsen@hotmail.com - 11 Oct 2005 21:46 GMT
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.
stephen.mcfall@gmail.com - 12 Oct 2005 02:11 GMT
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
ian_jacobsen@hotmail.com - 12 Oct 2005 15:14 GMT
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[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
ian_jacobsen@hotmail.com - 14 Oct 2005 21:45 GMT
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[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);
        }       
    }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.