如果修改TComboBox控件下拉列表的宽度?

windofsun 2002-06-12 12:50:19
加精
在程序里面,ComboBox控件的宽度有限制,这样里面的内容就不能全部显示出来。有没有什么方法可以修改它的下拉列表的宽度呢?也就是当用户点击,出现列表以后,这个列表的宽度比ComboBox控件本身要宽很多。
...全文
255 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
日总是我哥 2002-06-12
  • 打赏
  • 举报
回复

??
windofsun 2002-06-12
  • 打赏
  • 举报
回复
是“如何”,又写错了
windofsun 2002-06-12
  • 打赏
  • 举报
回复
sorry,题目应该是“任何如何修改TComboBox控件下拉列表的宽度?”
snjun 2002-06-12
  • 打赏
  • 举报
回复
放在create中可以啊,除非你的combobox里没有项目就不会变宽,只要有项目就对了
windofsun 2002-06-12
  • 打赏
  • 举报
回复
结帐了,多谢各位
Coldbloodedspecter 2002-06-12
  • 打赏
  • 举报
回复
给你一段例程
procedure TForm1.DropDownWidth(Sender: TObject);
var
CBox: TDBComboBox;
Width: Integer;
I, TextLen: Longint;
lf: LOGFONT;
f: HFONT;
begin
CBox := (Sender as TDBComboBox);
Width := CBox.Width;
FillChar(lf,SizeOf(lf),0);
StrPCopy(lf.lfFaceName, CBox.Font.Name);
lf.lfHeight := CBox.Font.Height;
lf.lfWeight := FW_NORMAL;
if fsBold in CBox.Font.Style then
lf.lfWeight := lf.lfWeight or FW_BOLD;

f := CreateFontIndirect(lf);
if (f <> 0) then
try
CBox.Canvas.Handle := GetDC(CBox.Handle);
SelectObject(CBox.Canvas.Handle,f);
try
for i := 0 to CBox.Items.Count-1 do begin
TextLen := CBox.Canvas.TextWidth(CBox.Items[i]);
if CBox.Items.Count-1 > CBox.DropDownCount then
begin
if TextLen > Width-25 then
Width := TextLen +25;
end
else if CBox.Items.Count-1 <= CBox.DropDownCount then
begin
if TextLen > Width-5 then
Width := TextLen+8;
end;
end;
finally
ReleaseDC(CBox.Handle, CBox.Canvas.Handle);
end;
finally
DeleteObject(f);
end;
SendMessage(CBox.Handle, CB_SETDROPPEDWIDTH, Width, 0);
end;
这可以把Combobox的宽度设为最长字符串的长度,
如果能用到的话,给点分好吗?
王集鹄 2002-06-12
  • 打赏
  • 举报
回复
to doer_ljy: c
//Style = lbOwnerDrawVariable
procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
var Height: Integer);
begin
Height := 50;
end;
王集鹄 2002-06-12
  • 打赏
  • 举报
回复
//我也是用这样的方法,可是放在Create中执行,所以就认为不行
//感谢Carfield

//整理后的答案如下
uses Math;

procedure TForm1.ComboBox1DropDown(Sender: TObject);
var
I: Integer;
vMaxWidth: Integer;
begin
vMaxWidth := TComboBox(Sender).Width;
for I := 0 to TComboBox(Sender).Items.Count - 1 do
vMaxWidth := Max(vMaxWidth,
TComboBox(Sender).Canvas.TextWidth(TComboBox(Sender).Items[I]) + 8);
TComboBox(Sender).Perform(CB_SETDROPPEDWIDTH, vMaxWidth, 0);
end;
doer_ljy 2002-06-12
  • 打赏
  • 举报
回复
宽度可以改,改高度是个问题!
windindance 2002-06-12
  • 打赏
  • 举报
回复
可以放在组合框的 OnDropDown 事件中。
王集鹄 2002-06-12
  • 打赏
  • 举报
回复
to Carfield:可以了!,放在Create中执行不行 :)

procedure TForm1.FormShow(Sender: TObject);
begin
ComboBox1.Perform(CB_SETDROPPEDWIDTH, 200, 0);
end;
Carfield 2002-06-12
  • 打赏
  • 举报
回复
試過啊,放在Form.Create或者Button.Click中都可以.//Uses Windows
王集鹄 2002-06-12
  • 打赏
  • 举报
回复
to Carfield & snjun: 你们调试过吗?想法是好的,可行不通 :(
dreamfan 2002-06-12
  • 打赏
  • 举报
回复
好像可以啊
snjun 2002-06-12
  • 打赏
  • 举报
回复
在form的oncreate中执行
ComboBox1.Perform(CB_SETDROPPEDWIDTH, 200, 0);
那个200为宽度
Carfield 2002-06-12
  • 打赏
  • 举报
回复
向Combobox发送一个消息CB_SETDROPPEDWIDTH
comboBox1.Perform(CB_SETDROPPEDWIDTH, 100, 0);
//100为Combobox下拉的宽度.
windofsun 2002-06-12
  • 打赏
  • 举报
回复
TO: surfguy(冲浪小子)
先谢谢你。但如果有可以直接修改下拉宽度的方法的话,我还是想优先用这种方法。

TO:wsdrex(想冲不冲)
你说的这个属性是调整行数的,而不是列宽
wsdrex 2002-06-12
  • 打赏
  • 举报
回复
改变属性 DropDownCount 就可以了
surfguy 2002-06-12
  • 打赏
  • 举报
回复
我以前也遇到过这样的问题,当时我想到两种方法:
1.当鼠标单击combobox的item时,以hint的形式显示其text
2.当鼠标箪单击combobox的时候,combobox的宽度变宽;当mouseup并且
combobox的text<>''时,combobox的宽度恢复原来的值。
不知以上两种方法对你来说行不行?
netlib 2002-06-12
  • 打赏
  • 举报
回复
自己作一个得了,如果你不怕麻烦
用一个listbox和edit在加一个按钮
加载更多回复(2)

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧