Reputation: 88
I am breaking my head over this all morning.
I created a user control with a textbox and a listbox. The listbox should show all items which contains the text entered in the textbox.
It is supposed to be a combobox with Like Search Capabilities.
All the searching works only the resizing of the listbox fails at a certain point and i cant figure out what the problem is.
The code below is executed perfectly. But after the invalidation of lbresult
the height of lbresults
sets to 4 when the viewableRows = 1
Under the code is the output generated by the console statements
if (this.lbResults.Visible)
{
int viewableRows = this.lbResults.Items.Count;
Console.WriteLine(viewableRows.ToString());
if (viewableRows > this.maxItemsDisplayed)
{
viewableRows = this.maxItemsDisplayed;
}
Console.WriteLine(viewableRows.ToString());
int lbresults_height = ((viewableRows) * 15) ;
Console.WriteLine(lbresults_height);
this.ResizeControl(this.Width, this.txtSearch.Height + lbresults_height);
this.lbResults.Size = new Size(this.lbResults.Width, lbresults_height);
this.lbResults.Invalidate();
Console.WriteLine("Listbox Height: " + lbResults.Height.ToString());
}
else if(!this.lbResults.Visible)
{
this.ResizeControl(this.Width, this.txtSearch.Height);
}
Output:
1
1
15
Listbox Height: 4
I can't find any reason why it should be set to 4...
Please help me.
Upvotes: 1
Views: 318
Reputation:
The behaviour you describe happens, most likely, because IntegralHeight
is set to true (MSDN article). If you want the ListBox
to the get as height exactly the same value that you are inputing, just disable this option:
this.lbResults.IntegralHeight = false;
On the other hand, bear in mind that this is not a bad-to-have feature. When this option is enabled, C# takes the height value you input as a suggestion and adapt it too meet the target: the final height of the ListBox has to be divisible by the height of each item (the real height, that is, ItemHeight + lag between items).
Upvotes: 1