我有一个组合框,其样式设置为csDropDown.我试图在OnSelect事件处理程序中执行此操作;
if cboEndTime.ItemIndex > -1 then
cboEndTime.Text := AnsiLeftStr(cboEndTime.Text, 5);
Run Code Online (Sandbox Code Playgroud)
但它没有效果.
组合项看起来像这样;
09:00(0分钟)
09:30(30分钟)
10:00(1小时)
10:30(1.5小时)
......
例如,如果我选择第二项,我希望组合框的文本显示为09:30,即截断.将ItemIndex设置为-1.
我怎么能得到这个?
看来,您Text
在OnSelect
事件期间所做的更改随后会被框架覆盖.无论是Windows API还是VCL,我都没有调查过哪些.
一种解决方案是推迟实际更改,直到原始输入事件的处理完成为止.像这样:
const
WM_COMBOSELECTIONCHANGED = WM_USER;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1Select(Sender: TObject);
protected
procedure WMComboSelectionChanged(var Msg: TMessage); message WM_COMBOSELECTIONCHANGED;
end;
implementation
{$R *.dfm}
procedure TForm1.ComboBox1Select(Sender: TObject);
begin
PostMessage(Handle, WM_COMBOSELECTIONCHANGED, 0, 0);
end;
procedure TForm1.WMComboSelectionChanged(var Msg: TMessage);
begin
if ComboBox1.ItemIndex<>-1 then
begin
ComboBox1.Text := Copy(ComboBox1.Text, 1, 1);
ComboBox1.SelectAll;
end;
end;
Run Code Online (Sandbox Code Playgroud)