组合框样式“csDropDownList”在德尔福

发布于 2024-12-01 10:59:54 字数 257 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╭ゆ眷念 2024-12-08 10:59:54

不,这根本不是 Windows 组合框控件的工作方式。

但是,如果您坚持,并且不关心用户会感到困惑,您可以将 Style 设置为 csDropDown,然后

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  Key := #0;
end;

按照组合框的 OnKeyPress 进行操作事件。那么用户就无法手动输入文本,而只能从列表中的项目中进行选择。但是,仍然可以通过设置 Text 属性将文本设置为您喜欢的任何内容(即使它不在列表中):

ComboBox1.Text := 'Sample';

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 to csDropDown and then do

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  Key := #0;
end;

as 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 the Text property:

ComboBox1.Text := 'Sample';
在风中等你 2024-12-08 10:59:54

设置 ItemIndex 属性。如果您还不知道,您可以使用 ComboBox.Items.IndexOf('New Item') 来获取该文本的索引。

Combobox.ItemIndex := Combobox.Items.IndexOf('New item');

Set the ItemIndex property. You can get ComboBox.Items.IndexOf('New Item') to get the index of that text, if you don't already know it.

Combobox.ItemIndex := Combobox.Items.IndexOf('New item');
倾其所爱 2024-12-08 10:59:54

下面的示例代码演示了如何绘制自定义文本以响应发送到 ComboBox 控件的父窗口的 WM_DRAWITEM 消息(这应该是示例工作的形式,否则子类化控件或完整绘制项目的控制是必要的)。

要接收此消息,请将控件的 Style 属性设置为“csOwnerDrawFixed”,但不要为 OnDrawItem 事件放置处理程序,以便在所有其他中应用默认绘制。我们干预绘图的情况。

该示例在 ItemIndex 为 -1 时设置文本,但可以在其他情况下进行调整/调整。请注意,绘图代码并不完整或不准确,该示例只是演示了一种实现方法:

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    [..]
  private
    procedure WMDrawItem(var Msg: TWMDrawItem); message WM_DRAWITEM;
  end;

[...]

procedure TForm1.WMDrawItem(var Msg: TWMDrawItem);
var
  Font: HFONT;
begin
  inherited;
  if (Msg.Ctl = ComboBox1.Handle) and (Msg.DrawItemStruct.itemID = $FFFFFFFF) and
      ((Msg.DrawItemStruct.itemAction and ODA_DRAWENTIRE) = ODA_DRAWENTIRE) then begin

    Font := SelectObject(Msg.DrawItemStruct.hDC, ComboBox1.Canvas.Font.Handle);
    SelectObject(Msg.DrawItemStruct.hDC, GetStockObject(DC_BRUSH));

    if (Msg.DrawItemStruct.itemState and ODS_SELECTED) = ODS_SELECTED then begin
      SetDCBrushColor(Msg.DrawItemStruct.hDC, ColorToRGB(clHighlight));
      SetBkColor(Msg.DrawItemStruct.hDC, ColorToRGB(clHighlight));
      SetTextColor(Msg.DrawItemStruct.hDC, ColorToRGB(clHighlightText));
    end else begin
      SetDCBrushColor(Msg.DrawItemStruct.hDC, ColorToRGB(clWindow));
      SetBkColor(Msg.DrawItemStruct.hDC, ColorToRGB(clWindow));
      SetTextColor(Msg.DrawItemStruct.hDC, ColorToRGB(clWindowText));
    end;

    FillRect(Msg.DrawItemStruct.hDC, Msg.DrawItemStruct.rcItem, 0);
    TextOut(Msg.DrawItemStruct.hDC, 4, 4, '_no_selected_item_', 18);

    SelectObject(Msg.DrawItemStruct.hDC, Font);
  end;
end;

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 the OnDrawItem 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:

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    [..]
  private
    procedure WMDrawItem(var Msg: TWMDrawItem); message WM_DRAWITEM;
  end;

[...]

procedure TForm1.WMDrawItem(var Msg: TWMDrawItem);
var
  Font: HFONT;
begin
  inherited;
  if (Msg.Ctl = ComboBox1.Handle) and (Msg.DrawItemStruct.itemID = $FFFFFFFF) and
      ((Msg.DrawItemStruct.itemAction and ODA_DRAWENTIRE) = ODA_DRAWENTIRE) then begin

    Font := SelectObject(Msg.DrawItemStruct.hDC, ComboBox1.Canvas.Font.Handle);
    SelectObject(Msg.DrawItemStruct.hDC, GetStockObject(DC_BRUSH));

    if (Msg.DrawItemStruct.itemState and ODS_SELECTED) = ODS_SELECTED then begin
      SetDCBrushColor(Msg.DrawItemStruct.hDC, ColorToRGB(clHighlight));
      SetBkColor(Msg.DrawItemStruct.hDC, ColorToRGB(clHighlight));
      SetTextColor(Msg.DrawItemStruct.hDC, ColorToRGB(clHighlightText));
    end else begin
      SetDCBrushColor(Msg.DrawItemStruct.hDC, ColorToRGB(clWindow));
      SetBkColor(Msg.DrawItemStruct.hDC, ColorToRGB(clWindow));
      SetTextColor(Msg.DrawItemStruct.hDC, ColorToRGB(clWindowText));
    end;

    FillRect(Msg.DrawItemStruct.hDC, Msg.DrawItemStruct.rcItem, 0);
    TextOut(Msg.DrawItemStruct.hDC, 4, 4, '_no_selected_item_', 18);

    SelectObject(Msg.DrawItemStruct.hDC, Font);
  end;
end;
乖不如嘢 2024-12-08 10:59:54

我认为您想要正常的事情,即在尚未进行选择时在组合框中显示某些内容。空白矩形的瞬间。想象一个充满空白组合框的表单...;)

我所看到的大多数程序员所做的是将第一项作为标题显示在组合框中。

因此,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文