TComboBox排序匹配与乱码的问题

1、TComboBox乱码的问题:

procedure ComboBox_XPMan_KeyPress(Sender: TObject;
  var Key: Char);
  function HasSelectedText(CB_HWND:HWND; var StartPos, EndPos: DWORD): Boolean;
  begin
    SendMessage(CB_HWND, CB_GETEDITSEL, Integer(@StartPos),
    Integer(@EndPos));
    Result := EndPos > StartPos;
  end;
  function IsNTSystem: Boolean;
  {功能:检测是否为NT系统}
  var
    info: OSVERSIONINFO;
  begin
    info.dwOSVersionInfoSize := sizeof(info);
    GetVersionEx(info);
    Result := info.dwPlatformId = VER_PLATFORM_WIN32_NT;
  end;
var
  StartPos, EndPos: DWORD;
  OldText, SaveText: WideString; //关键在此,中间字符串要设为宽字符串型
  LastByte: Integer;
  TheCB:TComboBox;
  CBHandle:HWND;
begin
  if not IsNTSystem then Exit; //非NT系统则退出
  TheCB:=TComboBox(Sender);
  CBHandle:=TheCB.Handle;
  if not TheCB.AutoComplete then exit;
  if ord(Key) = VK_BACK then
  begin
    SaveText := TheCB.Text;
    if HasSelectedText(CBHandle, StartPos, EndPos) then
    begin
      SendMessage(CBHandle,CB_GETEDITSEL,Integer(@StartPos),Integer(@EndPos));
      Delete(SaveText, StartPos + 1, EndPos - StartPos);
      SendMessage(CBHandle, CB_SETCURSEL, -1, 0);
      TheCB.Text := SaveText;
      SendMessage(CBHandle,CB_SETEDITSEL,0,MakeLParam(StartPos,StartPos));
      Key := #0;
    end
    else
      if(TheCB.Style in [csDropDown,csSimple]) and (Length(SaveText)>0) then
      begin
        LastByte := StartPos;
        OldText := Copy(SaveText, 1, LastByte - 1);
        SendMessage(CBHandle, CB_SETCURSEL, -1, 0);
        TheCB.Text := OldText + Copy(SaveText, EndPos + 1, MaxInt);
        SendMessage(CBHandle, CB_SETEDITSEL, 0, MakeLParam(LastByte - 1,
        LastByte - 1));
        Key := #0;
      end;
  end;
end;


在TComboBox的KeyPress事件中调用ComboBox_XPMan_KeyPress(Sender,Key);


2、排序问题

         在赋值item时先进行排序即可。将属性AutoComplete属性置true即可。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值