如何在listView中显示下拉菜单

如何在listView中显示下拉菜单,第1张

先说明一点,本事例单击是在listview的每一次第一列出一个下拉列表框,要在其他列出现,可能要用GetCursorPos(pos)来确置了,你自己完善吧,哈哈!

unit Unit1

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

ComCtrls, StdCtrls

type

TForm1 = class(TForm)

ListView1: TListView

ComboBox1: TComboBox

procedure FormDestroy(Sender: TObject)

procedure ListView1Click(Sender: TObject)

private

FComboBox: TComboBox

{ Private declarations }

procedure CreateCombobox(var aComboBox:TComboboxaLeft, aTop: integer)

procedure MyComboBoxChange(Sender: TObject)

procedure MyComboBoxExit(Sender: TObject)

public

{ Public declarations }

end

var

Form1: TForm1

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.CreateCombobox(var aComboBox: TCombobox

aLeft, aTop: integer)

begin

aComboBox := TComboBox.Create(Self)

aComboBox.Parent := Self

aComboBox.OnChange := MyComboBoxChange

aComboBox.OnExit := MyComboBoxExit

aComboBox.DropDownCount := 5

aComboBox.Style := csDropDown

aComboBox.Items.Add('字段')

aComboBox.Items.Add('aaa')

aComboBox.Items.Add('bbb')

aComboBox.Items.Add('ccc')

aComboBox.Items.Add('其他类型')

aComboBox.Left := aLeft + ListView1.Left + 1

aComboBox.Top := aTop + ListView1.Top + 1

aComboBox.Width := 50

aComboBox.BringToFront

aComboBox.SetFocus

end

procedure TForm1.MyComboBoxChange(Sender: TObject)

begin

if Assigned(ListView1.Selected) then

ListView1.Selected.Caption := TCombobox(Sender).Text

end

procedure TForm1.MyComboBoxExit(Sender: TObject)

begin

try

FComboBox.Free

except

end

FComboBox := nil

end

procedure TForm1.FormDestroy(Sender: TObject)

var

lp: integer

begin

for lp := ComponentCount - 1 downto 0 do

begin

if Components[lp] is TCombobox then

begin

TCombobox(Components[lp]).Free

end

end

FComboBox := nil

end

procedure TForm1.ListView1Click(Sender: TObject)

begin

FormDestroy(Sender)

if Assigned(ListView1.Selected) then

CreateCombobox(FComboBox, ListView1.Selected.Left, ListView1.Selected.Top)

end

end.

在设计页面的时候设置一个下拉式的组合框,并初始化可不见,在单击listview某一行时将下拉式的组合框可见,并根据listview某一行赋值组合框位置及内容,即可。虽然方法不是很好,但应该可以实现。

希望对你有用,请采纳

你的采纳就是我回答的动力!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/8018651.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存