过滤内容时出现下拉列表大小问题

时间:2017-01-04 09:31:19

标签: c# combobox dropdown

我的下拉列表大小有问题。当我手动打开列表时,默认高度设置为250,原始列表有一个很好的滚动条,根据下面的打印屏幕。

enter image description here

当我开始输入以查找匹配的记录时,问题就开始了。一旦我开始输入(不打开列表),下拉列表的大小就会改变如下: enter image description here

这很好。然而,这导致了一个真正的问题。如果选择了已过滤的项目,它将按预期显示在组合框中。但是,当我打开下拉列表时,前一个选择的内容仍然在组合框中,列表的大小很小,只包含匹配的记录。

enter image description here

虽然小列表仍然打开只有3条记录,但我删除了内容,我的下拉列表的大小保持不变,但列表包含所有记录,RHS上有一个小滚动条而不是重置为原始大小。删除内容后,我需要按照第一个打印屏幕显示完整列表。

enter image description here

我有以下代码,我试图将下拉列表大小重置为原来的250但没有运气:请帮助!!!

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;

}

1 个答案:

答案 0 :(得分:1)

您在IntegralHeight事件中将false设置为SelectJobComboBox_TextUpdate

将其设置为true,您的原始高度将可见

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

**编辑:**

问题在ComboBox.DropDownHeight文档中的备注中进行了解释:

  

设置DropDownHeight属性会将IntegralHeight属性重置为false。

所以你的第一个if条件应该是这样的:

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;

    }

这将确保下拉发生在您希望的范围内。 因此,您需要选择,将IntegralHeight设置为true,然后让组合框决定或设置DropDownHeight并自行决定。