组合框样式“csDropDownList”在德尔福
我在 delphi 7 中创建了一个表单,并在其上添加了一个组合框。组合框包含项目列表。我不希望用户可以在 Combobox 中输入值,所以我已经设置了
combobox.style := csDropDownList;
但通过代码我想使用 combobox.text := 'New Item';
但它不起作用。请注意,我想要显示的文本不在项目列表中,并且我不想将其添加到那里。请问这个有什么解决办法吗?
I have created one form in delphi 7 and added one combobox on it. The combobox contains the list of items. I dont want that user can enter the value to Combobox so i have set
combobox.style := csDropDownList;
But thorugh code i want to use combobox.text := 'New Item';
but its not working. Note that the text I want to show is not in the list of items and I don't want to add it there. Please is any solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,这根本不是 Windows 组合框控件的工作方式。
但是,如果您坚持,并且不关心用户会感到困惑,您可以将
Style
设置为csDropDown
,然后按照组合框的
OnKeyPress 进行操作事件。那么用户就无法手动输入文本,而只能从列表中的项目中进行选择。但是,您仍然可以通过设置
Text
属性将文本设置为您喜欢的任何内容(即使它不在列表中):No, this is simply not the way the Windows combobox control works.
However, if you insist, and you don't care that your users will get confused, you can set
Style
tocsDropDown
and then doas the combobox'
OnKeyPress
event. Then the user cannot enter text manually, but can only choose from the items in the list. However, you can still set the text to anything you like (even if it isn't in the list) by setting theText
property:设置
ItemIndex
属性。如果您还不知道,您可以使用ComboBox.Items.IndexOf('New Item')
来获取该文本的索引。Set the
ItemIndex
property. You can getComboBox.Items.IndexOf('New Item')
to get the index of that text, if you don't already know it.下面的示例代码演示了如何绘制自定义文本以响应发送到 ComboBox 控件的父窗口的
WM_DRAWITEM
消息(这应该是示例工作的形式,否则子类化控件或完整绘制项目的控制是必要的)。要接收此消息,请将控件的
Style
属性设置为“csOwnerDrawFixed”,但不要为OnDrawItem
事件放置处理程序,以便在所有其他中应用默认绘制。我们干预绘图的情况。该示例在
ItemIndex
为 -1 时设置文本,但可以在其他情况下进行调整/调整。请注意,绘图代码并不完整或不准确,该示例只是演示了一种实现方法:Below sample code demonstrates how you can draw custom text in response to a
WM_DRAWITEM
message sent to the ComboBox control's parent window (this should be the form for the sample to work, otherwise subclassing controls or full drawing of items of the control would be necessary).To receive this message set the
Style
property of the control to 'csOwnerDrawFixed', but do not put a handler for theOnDrawItem
event so that default drawing should be applied in all other cases that we intervene drawing.The sample sets a text when
ItemIndex
is -1, but it can be adapted/tweaked otherwise. Note that the drawing code is not complete or accurate, the sample just demonstrates a way how it can be done:我认为您想要正常的事情,即在尚未进行选择时在组合框中显示某些内容。空白矩形的瞬间。想象一个充满空白组合框的表单...;)
我所看到的大多数程序员所做的是将第一项作为标题显示在组合框中。
因此,在 FormCreate 中(填充 ComboBox 后),将其 ItemIndex 设置为 0,这将显示标题。
在其 OnChange 事件中,如果选择了项目 0,您可以选择不采取任何操作(“真实”项目的索引基数为 1),或者如果 <<,则获取 ItemIndex-1 并跳过操作。 0.
对于每个第一次使用 Combobox 的人来说,这一定是一个非常普遍的抱怨。我不明白为什么没有一个编码员认识到它。
Borland 等人所要做的就是用 ItemIndex=0 初始化一个新的 ComboBox,这样混乱就会消失。必须设置索引 0 当然并不明显 - 因为单击时会看到空行,所以逻辑结论是它的索引为 0。可能他们希望为设计人员提供添加标签的选项而是在组合框之外。
I think you want the normal thing, to display something in the ComboBox when no selection has yet been made. Instant of a blank rectangle. Imagine a form full of blank comboboxes... ;)
What I've seen most programmers do is have the first item as the title to display in the ComboBox.
So, in FormCreate (after you've populated the ComboBox), you set its ItemIndex to 0, and this displays the title.
In its OnChange event you can choose to take no action if item 0 is selected ("real" items then have base 1 for index), or get ItemIndex-1 and skip action if < 0.
Must be a super common complaint from everyone who has used Comboboxes the first time. I can't understand how none of the coders recognize it.
All Borland et al would have had to do was to initialize a new ComboBox with ItemIndex=0 and the confusion would have been gone. It's certainly not obvious that you have to set index 0 - since you see the blank line when clicked, the logical conclusion is that it has index 0. Probably they wanted to give designers the option to add a label outside the combobox instead.