Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Общие вопросы > ComboBox пищит


Автор: MacTep 18.5.2005, 11:54
Использую следующий код для того, чтобы добавлять новые строки в ComboBox1. Но после нажатия на Enter прога издает звук, который мне не нравится. Как его отключить?

Код

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then Combobox1.Items.Add(ComboBox1.Text);
end;

Автор: Medved 18.5.2005, 12:05
Код

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


Код

procedure TfrmMain.ComboBox1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then if (Sender is TCombobox) then (Sender as TCombobox).Items.Add((Sender as TCombobox).Text);
end;

Добавлено @ 12:07
Событие OnKeyDown вызывается перед событием OnKeyPress. Что делает код, думаю понять не сложно.

Автор: drinkOut 18.5.2005, 17:26
Цитата(Pegas @ 18.5.2005, 12:05)
(Sender in TCombobox)

Только по-моему надо:
Код

(Sender is TCombobox)

Автор: MacTep 18.5.2005, 18:08
Pegas, спасибо. Но почему же происходит подача звукового сигнала? Реакция на событие OnKeyDown? Или я что-то не понимаю?

Автор: Medved 18.5.2005, 19:26
А самому в исходниках посмотреть не хочется?

Автор: MacTep 21.5.2005, 12:42
Посмотрел в исходниках... Нашел в StdCtrls.pas следующее:
Код

procedure TCustomComboBox.KeyPress(var Key: Char);

  function HasSelectedText(var StartPos, EndPos: DWORD): Boolean;
  begin
    SendMessage(Handle, CB_GETEDITSEL, Integer(@StartPos), Integer(@EndPos));
    Result := EndPos > StartPos;
  end;

  procedure DeleteSelectedText;
  var
    StartPos, EndPos: DWORD;
    OldText: String;
  begin
    OldText := Text;
    SendMessage(Handle, CB_GETEDITSEL, Integer(@StartPos), Integer(@EndPos));
    Delete(OldText, StartPos + 1, EndPos - StartPos);
    SendMessage(Handle, CB_SETCURSEL, -1, 0);
    Text := OldText;
    SendMessage(Handle, CB_SETEDITSEL, 0, MakeLParam(StartPos, StartPos));
  end;

var
  StartPos: DWORD;
  EndPos: DWORD;
  OldText: String;
  SaveText: String;
  Msg : TMSG;
  LastByte: Integer;
begin
  inherited KeyPress(Key);
  if not AutoComplete then exit;
  if Style in [csDropDown, csSimple] then
    FFilter := Text
  else
  begin
   if GetTickCount - FLastTime >= 500 then
      FFilter := '';
    FLastTime := GetTickCount;
  end;
  case Ord(Key) of
    VK_ESCAPE: exit;
    VK_TAB:
      if FAutoDropDown and DroppedDown then
        DroppedDown := False;
    VK_BACK:
      begin
        if HasSelectedText(StartPos, EndPos) then
          DeleteSelectedText
        else
          if (Style in [csDropDown, csSimple]) and (Length(Text) > 0) then
          begin
            SaveText := Text;
            LastByte := StartPos;
            while ByteType(SaveText, LastByte) = mbTrailByte do Dec(LastByte);
            OldText := Copy(SaveText, 1, LastByte - 1);
            SendMessage(Handle, CB_SETCURSEL, -1, 0);
            Text := OldText + Copy(SaveText, EndPos + 1, MaxInt);
            SendMessage(Handle, CB_SETEDITSEL, 0, MakeLParam(LastByte - 1, LastByte - 1));
            FFilter := Text;
          end
          else
          begin
            while ByteType(FFilter, Length(FFilter)) = mbTrailByte do
              Delete(FFilter, Length(FFilter), 1);
            Delete(FFilter, Length(FFilter), 1);
          end;
        Key := #0;
        Change;
      end;
  else // case
    if FAutoDropDown and not DroppedDown then
      DroppedDown := True;
    if HasSelectedText(StartPos, EndPos) then
      SaveText := Copy(FFilter, 1, StartPos) + Key
    else
      SaveText := FFilter + Key;

    if Key in LeadBytes then
    begin
      if PeekMessage(Msg, Handle, 0, 0, PM_NOREMOVE) and (Msg.Message = WM_CHAR) then
      begin
        if SelectItem(SaveText + Char(Msg.wParam)) then
        begin
          PeekMessage(Msg, Handle, 0, 0, PM_REMOVE);
          Key := #0
        end;
      end;
    end
    else
      if SelectItem(SaveText) then
        Key := #0
  end; // case
end;

Из этого мне не совсем понятно, откуда берется звук! Странно... Кто объяснит непонимающему?

Автор: Medved 21.5.2005, 13:15
А ты не пробовал пошаговую отладку?

Автор: MacTep 23.5.2005, 11:04
Цитата
А ты не пробовал пошаговую отладку?
Я попробовал при помощи F7, но мне это ничего не дало. Сначала дохожу до слова end, а после этого уже раздается звуковой сигнал. И так и не понял! Может я дико туплю? Скажи, как надо делать?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)