组合框-DrawItem和拖动滚动条问题 [英] Combobox - DrawItem and dragging scroll bar problem
问题描述
你好.我需要一些有关ComboBox控件的绘图项目的帮助.
在大多数情况下,ComboBox都可以正常绘制.我可以上下滚动,一切都很好.问题是当我拖动滚动条缩略图"或框以更快地通过列表时.项目无法正确显示.
将ComboBox设置为DropDownList和OwnerDrawVariable.
我理解下面的代码不是很有意义,因为我可以使用Windows实现来绘制项目.我正在使用的程序以粗体显示某些项目,因此下面的代码仅说明了我遇到的问题.
感谢您的帮助!
Hi there. I need some help with drawing items for a ComboBox Control.
For the most part the ComboBox draws just fine. I can scroll up and down and everything is good. The problem is when I drag the scrollbar "thumb" or box to get through the list faster. The items do not display correctly.
The ComboBox is set to DropDownList and OwnerDrawVariable.
I understand the code below is not very meaningful as I could use the windows implementation to draw the items. The program I''m working on displays certain items in bold, so the code below just illustrates the problem I have.
Thanks for your help!!
Public Class Form1<br /><br /> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br /><br /> Dim i As Integer<br /><br /> For i = 1 To 200<br /> ComboBox1.Items.Add(i)<br /> Next<br /><br /> End Sub<br /><br /> Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem<br /><br /> If e.Index = -1 Then<br /> Exit Sub<br /> End If<br /><br /> e.DrawBackground()<br /><br /> e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(Color.Black), e.Bounds.X, e.Bounds.Y)<br /><br /> End Sub<br /><br />End Class
推荐答案
不确定是否可以解决该问题,但是可能因为您当前的代码太昂贵:对于绘制的每个项目:
-创建一个新的SolidBrush
-,您不必在其上调用Dispose,而应该在类提供此类方法的情况下调用它.
最好一次创建一个画笔,并将其放在一个方便的位置.类成员,因此您可以将其重新用于所有项目.实际上,您甚至不需要它,因为有一个提供标准画笔的类,因此您只需要Brushes.Black
:)
Hi,
not sure this will fix it, however it might as your current code is too expensive: for every item drawn you:
- create a new SolidBrush
- and you don''t call Dispose on it, which you should as the class offers such method.
It would be better to create a brush once, and keep it handy in a class member so you can reuse it for all items. And actually, you don''t even need this as there is a class offering standard brushes, so all you need is Brushes.Black
:)
大家好.感谢您的帮助.我实际上发现了问题.我的代码没有问题".虽然我知道可以肯定地改进代码.
Hi All. Thank you for your help. I actually discovered the problem. There are no "problems" with the code I have. While I understand the code could be improved definitely.
我对自己遇到的问题感到沮丧,并在带有Vista的Virtual PC上尝试了可执行文件,这没有问题.发现问题出在Windows 7上.我当前正在使用RC.
I was frustrated with the issue I was having, and tried the executable using Virtual PC with Vista and there was no problem. Discovered that the issue was with Windows 7. I am currently using the RC.
在使用OwnerDrawing组合框的其他程序中也发生了同样的问题.
The same issue occurs with in other programs that use OwnerDrawing for comboboxes.
再次感谢.
您真的需要OwnerDrawVariable吗?尝试OwnerDrawFixed. 并且,如果您使用的是从ComboBox派生的用户控件,请通过重写DoubleBuffered属性并返回true来启用Double缓冲.
Do you really need OwnerDrawVariable ? try OwnerDrawFixed. And if you are using a user control derived from ComboBox, enable Double buffering by Overriding DoubleBuffered property and returning true.
这篇关于组合框-DrawItem和拖动滚动条问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!