简体   繁体   English

可变大小的所有者绘制ComboBox; 列表没有正确调整大小

[英]Variable-size owner draw ComboBox; list not sizing correctly

I derived a class from ComboBox, made it an owner drawn list box (DrawMode.OwnerDrawVariable style), and by overriding OnMeasureItem and OnDrawItem I add special items to the drop-down list (separators, for example) which are of a smaller/larger size than the normal items that inhabit the control. 我从ComboBox派生了一个类,使其成为一个所有者绘制的列表框(DrawMode.OwnerDrawVariable样式),并通过重写OnMeasureItem和OnDrawItem,我将特殊项添加到下拉列表(例如,分隔符)中,它们更小/更大大小比控制中的常规项目大。

Now, the problem I'm having and can't seem to effectively remedy is the sizing of the drop-down list: there's an ugly empty space at the end of the list when it drops down. 现在,我遇到并且似乎无法有效解决的问题是下拉列表的大小调整:当列表下降时,列表末尾有一个丑陋的空白区域。 I thought I tackled this by setting the DropDownHeight property of the ComboBox to the sum of all the items' height, but this doesn't seem to work all the time. 我想我通过设置组合框的所有项目高度的总和的DropDownHeight财产解决这一问题,但是似乎并没有工作, 所有的时间。 Sometimes, on a random number of items, the Empty White Space of Doom returns. 有时,在随机数量的项目上,空白空间的厄运会返回。 This is an unusual problem, but hopefully a common and easily fixed one. 这是一个不寻常的问题,但希望是一个常见且易于修复的问题。

How can I get the ComboBox's drop-down list to size precisely to the size occupied by the items inside of it? 如何让ComboBox的下拉列表精确调整到其内部项目占用的大小?

I'm seeing a pretty silly bug in the ComboBox.UpdateDropDownHeight() method. 我在ComboBox.UpdateDropDownHeight()方法中看到了一个非常愚蠢的错误。 When the DropDownHeight property matches the default value, it calculates a custom height to fit the dropdown to the number of items. 当DropDownHeight属性与默认值匹配时,它会计算自定义高度以适合项目数的下拉列表。 It does this even when you changed the DrawMode, that's plain wrong. 即使您更改了DrawMode,它也会这样做,这是完全错误的。

The workaround: 解决方法:

  int height = ...; // Your code here
  if (height == 106) ++height;
  comboBox1.DropDownHeight = height;

You'll get a one pixel gap, you should be able to hide that in your OnDrawItem() overload. 您将获得一个像素间隙,您应该能够在OnDrawItem()重载中隐藏它。

I spent a long time battling this same problem. 我花了很长时间来解决同样的问题。

When you are adding custom items to the combobox, the DropDownHeight will not set properly. 将自定义项添加到组合框时,DropDownHeight将无法正确设置。 In order to guarantee that you get it set right every time, you need to hijack a Windows Message. 为了保证每次都能正确设置,您需要劫持Windows消息。

This post shows how. 这篇文章说明了如何。 Just keep track of the height of all the items (standard and custom) in your combobox, and then set the total height of the dropdown portion like shown in the example. 只需跟踪组合框中所有项目(标准和自定义)的高度,然后设置下拉部分的总高度,如示例中所示。

I guess the problem in the way how you're calculating your dropdown list height, I guess it's smth like this: 我猜你计算你的下拉列表高度的方式存在问题,我想这就像这样:

comboBox.DropDownHeight = N_of_items * item_height;

the total height of the dropdown list should also include its top and bottom border height, so if you would do smth like this: 下拉列表的总高度还应该包括它的顶部和底部边框高度,所以如果你像这样做smth:

comboBox.DropDownHeight = N_of_items * item_height + SystemInformation.BorderSize.Height*2;

it should do the trick and show the dropdown list without white areas 它应该做的技巧,并显示没有白色区域的下拉列表

hope this helps, regards 希望这有帮助,问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中将包含另一个可变大小的对象列表的对象的可变大小的列表绑定到DataGridView - How to bind a variable-size list of objects containing another variable-size list of objects of to DataGridView in C# 封送嵌套在结构中的可变大小的结构数组 - Marshal a variable-size array of structures nested within a structure 解组包含结构的可变大小数组的结构 - Unmarshaling a structure containing a variable-size array of a structure 如何将控件A的底部停靠到可变大小控件B的位置 - How to dock bottom of control A to location of a variable-size control B IList实现分为三类:只读,固定大小,可变大小 - IList implementations fall into three categories: read-only, fixed-size, variable-size Predection Engine-模式不匹配:“ R4的预期标量或已知大小的向量,得到了可变大小的向量” - Predection Engine - schema mismatch: “expected scalar or known-size vector of R4, got variable-size vector” ComboBox和KeyValuePair列表无法正常工作 - ComboBox and KeyValuePair List not working correctly C# Combobox 未从列表中正确填充 - C# Combobox not populating from list correctly 在组合框中正确显示列表项并对选项做出反应 - Correctly displaying list items in a combobox and reacting to an item selection C#ComboBox大小调整和在DataGridViewComboBoxColumn内的放置 - C# ComboBox sizing and placement within DataGridViewComboBoxColumn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM