Autor Beitrag
MaxMara
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 10.04.08 08:04 
Hallo!
Ich hab ein kleines Problem mit einer Listbox. Per Doppelklick will ich Einträge daraus entfernen. Wenn man auf eine leere Stelle doppelklickt kommt eine "Listindex out of bounds" Exception.
Bitte um Hilfe!
Hier mein Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm3.ListBoxDblClick(Sender: TObject);
begin
  if ListBox.Items.Count > 0 then
  begin
    ListBox.Items.Delete(ListBox.ItemIndex);
    if ListBox.Items.Count = 0 then
    begin
      ApplyButton.Enabled := FALSE;
      FourCCCodec.ItemIndex := -1;
      FourCCDesc.ItemIndex := -1;
    end;
  end;
end;


Bin wirklich für jeden Hinweis dankbar!

Grüße aus Wien
Christian


Moderiert von user profile iconGausi: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Do 10.04.2008 um 08:13
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Do 10.04.08 08:08 
Frage ab, ob eine Eintrag ausgewählt ist:
ausblenden Delphi-Quelltext
1:
If (Sender as TListBox).ItemIndex <> -1 then					

_________________
Markus Kinzler.
MaxMara Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 10.04.08 08:12 
Funktioniert leider auch nicht:
Es kommt immer wieder: Listenindex überschreitet das Maximum (-1)
iKilledKenny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 8

Win XP
D5 Prof, C# Express 2005
BeitragVerfasst: Do 10.04.08 08:16 
Du musst mit

ausblenden Delphi-Quelltext
1:
ListBox.ItemAtPos					


abfragen, ob dort, wo du geklickt hast, auch ein Item ist.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Do 10.04.08 08:17 
Ich frage mich wie der Index -1 sein kann wenn er den Wert -1 hat

_________________
Markus Kinzler.
MaxMara Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 10.04.08 08:36 
user profile iconmkinzler hat folgendes geschrieben:
Ich frage mich wie der Index -1 sein kann wenn er den Wert -1 hat

Tja darauf weiss ich leider keine Antwort ;)

Ich hab jetzt dank iKilledKenny folgendes gemacht:
ausblenden Delphi-Quelltext
1:
if ListBox.ItemAtPos( Point(X,Y), True ) > -1 then					


Jetzt reagiert das Programm leider gar nicht mehr auf Doppelklicks in die Listbox.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Do 10.04.08 08:40 
Natürlich kannst du das eingebaute Verhalten auch nachbauen, ich sehe aber keinen Sinn darin. Zeig mal den jetzigen Code

_________________
Markus Kinzler.
MaxMara Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 10.04.08 08:43 
Derzeit sieht es so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm3.ListBoxDblClick(Sender: TObject);
var
  x, y: integer;
begin
  if ListBox.ItemAtPos( Point(X,Y), True ) > -1 then
  begin
    ListBox.Items.Delete(ListBox.ItemIndex);
    if ListBox.Items.Count = 0 then
    begin
      ApplyButton.Enabled := FALSE;
      FourCCCodec.ItemIndex := -1;
      FourCCDesc.ItemIndex := -1;
    end;
  end;
end;


EDIT: Mir ist gerade aufgefallen, dass X und Y keine Werte haben können. Ich denk mal hier ist das Problem...
iKilledKenny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 8

Win XP
D5 Prof, C# Express 2005
BeitragVerfasst: Do 10.04.08 08:56 
Du musst natürlich erst den richtigen Punkt bestimmen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
pt := Mouse.CursorPos;
pt := ListBox.ScreenToClient (pt);
ix := ListBox.ItemAtPos (pt, true);
if ix >= 0 then
  begin
    ListBox.Items.Delete(ix);
    if ListBox.Items.Count = 0 then
    begin
      ApplyButton.Enabled := FALSE;
      FourCCCodec.ItemIndex := -1;
      FourCCDesc.ItemIndex := -1;
  end;
MaxMara Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 10.04.08 09:05 
Ich hab jetzt iKilledKennys Code verwendet und das Verhalten beim Doubleclick ist sehr seltsam jetzt:
Wenn ich die Einträge nacheinander "wegklicke" funktioniert es. Wenn ich jedoch einen Eintrag in der Mitte wegklicke und anschliessend in den leeren Bereich der ListBox clicke kommt wieder die Exception. :(
Regan
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2157
Erhaltene Danke: 72


Java (Eclipse), Python (Sublimetext 3)
BeitragVerfasst: Do 10.04.08 09:22 
Moin,
ich würde dir folgenden Code vorschlagen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm3.ListBoxDblClick(Sender: TObject);  
var  
  x, y: integer;  
begin  
  if ListBox.Itemindex > -1 then
  begin  
    ListBox.Items.Delete(ListBox.ItemIndex);  
    if ListBox.Items.Count = 0 then  
    begin  
      ApplyButton.Enabled := FALSE;  
      FourCCCodec.ItemIndex := -1;  
      FourCCDesc.ItemIndex := -1;  
    end;  
  end;  
end;

Wenn das nicht geht, dann kannst du ja mal mit F8 den Code durchgehen und uns dann die Stelle sagen, an der der Fehler kommt.

MfG
Regan
MaxMara Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Do 10.04.08 09:38 
Hallo!
Danke für eure Tipps!
iKilledKennys Code hat doch funktioniert. Was ich jedoch übersehen habe, war das bei onClick auch eine ähnliche procedure aufgerufen wurde, die dann den Fehler ausgelöst hat. Nach Anpassen der procedure funktioniert das Programm einwandfrei.

Nochmals vielen Dank!

Grüße Christian.