Autor Beitrag
Calculon
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 676

Win XP Professional
Delphi 7 PE, Delphi 3 PRO
BeitragVerfasst: Mo 21.05.07 14:32 
Hi Leute,

Folgendermaßen wird der Eintrag in meiner Listbox markiert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.ListBox1Click(Sender: TObject);
var
  index: integer;
begin
  index := listbox1.ItemIndex;
  if index <> -1 then
    begin
      to_file := listbox1.Items.Strings[index];
      .
      .
      .
    end;
end;


wobei die Variable to_file global deklariert wurde. Nun, erst nach der Markierung kann ein PopUp-Fenster, dessen Einträge sich auf to_file beziehen "was machen".

Also erst Linksklick (Markieren) dann Rechtsklick (PopUpMenu). Mich würde interessieren, ob ich auch mit der rechten Maustaste direkt auf ein unmarkiertes Element in der Listbox klicken kann und diese dann sofort markiert wird (to_file wird beschrieben) und dann das PopUp-Fenster erscheint.

Gruß

Calculon
--
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Mo 21.05.07 14:51 
mal zum markieren an sich:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var idx:integer;
begin
  idx:=(sender as TListbox).ItemAtPos(point(x,y),true);
  if idx>-1 then
    (sender as TListbox).itemindex:=idx;
end;

nur OnClick wird in dem Fall nicht getriggert, aber du könntest deine Variable ja auch in der Mousedown-Methode setzen, oder nicht?

HTH Frank
Calculon Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 676

Win XP Professional
Delphi 7 PE, Delphi 3 PRO
BeitragVerfasst: Mo 21.05.07 15:09 
_frank_ hat folgendes geschrieben:
nur OnClick wird in dem Fall nicht getriggert, aber du könntest deine Variable ja auch in der Mousedown-Methode setzen, oder nicht?

Jepp! Super, genau was ich gesucht hab'. Danke!

Gruß

Calculon
--
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 21.05.07 15:21 
Hallo,

genau, und der ListBox nicht das PopUp-Menü im OI zuweisen sondern in MouseDown direkt aufrufen, damit das PopUp nur erscheint wenn auch auf ein Item geklickt wurde. Den ItemIndex kann man im OnClick des Menüitems auswerten, damit keine globale Variable erforderlich ist:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
//ListBox.OnMouseDown
var i : Integer;
    aPoint : TPoint;
begin
  if ssRight in Shift then//sonst auch bei Links-Klick PopUp-Menü :-)
    begin
    i := ListBox.ItemAtPos(Point(X,Y),True);
    if i <> -1 then
      begin
      ListBox.ItemIndex := i;
      aPoint := ListBox.ClientToScreen(Point(X,Y));
      PopUpMenu.PopUp(aPoint.X,aPoint.y);
      end;
    end;
//MenuItem.onClick
begin
  MachWasMit(ListBox.Items[ListBox.ItemIndex]);

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Calculon Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 676

Win XP Professional
Delphi 7 PE, Delphi 3 PRO
BeitragVerfasst: Mo 21.05.07 16:50 
@Lannes

Hab's so übernommen, danke für die Hinweise. Was mir noch aufgefallen ist:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
//MenuItem.onClick 
     begin MachWasMit(ListBox.Items[ListBox.ItemIndex]);

     <>

procedure TForm1.ListBox1Click(Sender: TObject);


Wenn ich mit dem Cursor auf- und abmarkiere, dann wird procedure TForm1.ListBox1Click(Sender: TObject); ebenfalls ausgeführt, was sehr praktisch ist. Bei procedure TForm1.ListBox1MouseDown(Sender: TObject;... allerdings nicht.

Habe deshalb diesen Effekt so hinzugefügt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
index: integer;
dif: integer;
begin
if listbox1.Count > 0 then
  begin
    if (Key = VK_UP) or (Key = VK_LEFT) then dif := -1;
    if (key = VK_DOWN) or (Key = VK_RIGHT) then dif := + 1;
    index := listbox1.ItemIndex + dif;
      if (index <> -1) and (index <= listbox1.Count - 1) then
        begin
          MachWasMit(ListBox.Items[ListBox.ItemIndex]);
        end;
  end;
end;



Gruß

Calculon
--
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Mo 21.05.07 17:28 
warum nicht einfach
listbox1click(listbox1);
im mousedown?
oder die variablensetzung in eine separate methode, welche von beiden events aus aufgerufen wird...

HTH Frank