TomTommyTom
TomTommyTom

Reputation: 125

Dropdown list size issues when filtering content

I have an issues with the size of my drop down list. Default height is set to 250 and original list has a nice scroll bar as per print screen below when I open the list manually.

enter image description here

The issue begins when I start typing to find a matching records. As soon as I start typing(without opening the list), the size of the drop down list changes as per below: enter image description here

This is fine. However, this leads to a real problem. If a filtered item was selected, it will appear in the combobox as expected. However, when I open the drop down list with the content of the previous selection still in combobox, the size of the list is small and contains only the matched records.

enter image description here

While the small list is still open with just 3 records, I delete the content, the size of my drop down remains the same but the list contains all records and there is a tiny scroll bar on the RHS rather then resetting itself to the original size. After removing the content i need to display the full list as per 1st print screen.

enter image description here

I have the following code in place where I have tried to reset the Drop down list size to its original 250 but no luck: PLEASE HELP!!!

private void SelectJobComboBox_TextUpdate(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(SelectJobComboBox.Text))
    {
        this.filteredItems.Clear();
        SelectJobComboBox.DataSource = arrProjectList;

        Cursor.Current = Cursors.Default;

        SelectJobComboBox.SelectedIndex = -1;
        RunBTN.Enabled = false;             

    }
    else
    {

        string filter_param = SelectJobComboBox.Text;
       filteredItems = arrProjectList.FindAll(x => x.ToLower().Contains(filter_param.ToLower()));


        if (filteredItems.Count == 0)
        {
            FilterMatchFound = false;
            filteredItems.Insert(0, "No matches found");
            // SelectJobComboBox.DataSource = filteredItems;
            SelectJobComboBox.DropDownHeight = 250;
            SelectJobComboBox.DataSource = arrProjectList;
             SelectJobComboBox.IntegralHeight = true;
            RunBTN.Enabled = false;
            JobNumberTextBox.Text = "";
            JobDescriptiontextBox.Text = "";

        }
        else
        {
            SelectJobComboBox.DataSource = filteredItems; //list all matching items. 
        }

        if (String.IsNullOrWhiteSpace(filter_param))
        {
            SelectJobComboBox.DataSource = arrProjectList;  //assign original list of directories
        }
        SelectJobComboBox.IntegralHeight = false;                
        SelectJobComboBox.DroppedDown = true;
        Cursor.Current = Cursors.Default;

        SelectJobComboBox.SelectedIndex = -1;

        SelectJobComboBox.Text = filter_param;

        SelectJobComboBox.SelectionStart = filter_param.Length;
        SelectJobComboBox.SelectionLength = 0;
    }  
}

private void SelectJobComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
    //Clear variables and lists every time an item is selected from the list
    this.sProjectNameFull = "";

    if (this.FilterMatchFound == true)
    {

        //Split directory name into qualifiers 
        string[] sDirectoryNameSplit = SelectJobComboBox.SelectedItem.ToString().Split(' ');
        this.sProjectNameFull = SelectJobComboBox.SelectedItem.ToString();

        //populate project number text box with 1st part of directory name
        JobNumberTextBox.Text = sDirectoryNameSplit[0];
        this.sProjectNumber = sDirectoryNameSplit[0];

        //concatenate parts of directory name into project name - start after 3rd qualifier
        sbProjectName.Clear();
        for (int j = 3; j < sDirectoryNameSplit.Length; j++)
        {
            sbProjectName.Append(sDirectoryNameSplit[j]).Append(' ');
        }

        //populate project name text box with project name built in the loop
        JobDescriptiontextBox.Text = sbProjectName.ToString();

    }
    else
    {

    }

    //enable run button
    RunBTN.Enabled = true;

}

Upvotes: 1

Views: 424

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

You set the IntegralHeight to false in the SelectJobComboBox_TextUpdate event.

Set it to true and your original height will be visible

if (String.IsNullOrWhiteSpace(filter_param))
{
    SelectJobComboBox.DataSource = arrProjectList;  //assign original list of directories
}
SelectJobComboBox.IntegralHeight = true;
SelectJobComboBox.DroppedDown = true;

**EDIT: **

The problem is explained in the remarks in the ComboBox.DropDownHeight documentation:

Setting the DropDownHeight property resets the IntegralHeight property to false.

So your first if-condition should look like this:

private void comboBox2_TextUpdate(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(SelectJobComboBox.Text))
    {
        this.filteredItems.Clear();                
        SelectJobComboBox.DataSource = arrProjectList;

        SelectJobComboBox.DropDownHeight = 100; // select here how many items you want to be displayed
        SelectJobComboBox.DroppedDown = true;  // and force the combobox to open up

        Cursor.Current = Cursors.Default;
        SelectJobComboBox.SelectedIndex = -1;

    }

this will ensure that the dropdown occurs as far as you wish it to be. So you need to choose, either set IntegralHeight to true and let the combobox decide or set DropDownHeight and decide yourself.

Upvotes: 1

Related Questions