Autor Beitrag
curtiss
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59



BeitragVerfasst: Mi 16.11.05 13:58 
Hallo !

Ich habe folgendes Problem. In meinem Programm gibt es eine listbox, die 12 items enthalten kann. Der anwender kann dann aus diesen max. 12 vorhandenen items eins auswählen. Allerdings weis ich nicht wie ich abfragen kann welches item ausgewählt ist. Ich denke

if listbox.selected[1] = true then ...
if listbox.selected[2] = true then ...
if listbox.selected[3] = true then ...

ist ein wenig umständlich!?

Danke im Voraus!
smiegel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 992
Erhaltene Danke: 1

WIN 7
D7 Prof., C#, RAD XE Prof.
BeitragVerfasst: Mi 16.11.05 14:37 
Hallo,

auf das aktuell gewählte Item kannst Du mit ListBox1.Items[ListBox1.ItemIndex] zugreifen.

Das Selected funktioniert nur, wenn Multiselect der Listbox aktiviert ist.

_________________
Gruß Smiegel
Ich weiß, daß ich nichts weiß, aber ich weiß mehr als die, die nicht wissen, daß sie nichts wissen. (Sokrates)
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 16.11.05 14:39 
Also willst du die Nummer des Eintrags haben, oder den Inhalt des Eintrags?

Für den ersten Fall:

ausblenden Delphi-Quelltext
1:
showmessage(inttostr(listbox1.itemindex+1)+'. wurde ausgewählt');					


Wenn du den Inhalt des Eintrags der ausgewählt wurde haben willst:

ausblenden Delphi-Quelltext
1:
showmessage(listbox1.items[listbox1.itemindex]);					


Wenns nicht stimmt, bitte um Berichtigung von anderen. Grundsätzlich lässt sich die Nummer des ausgewählten Eintrags mit listbox1.itemindex abfragen, wobei der erste Eintrag den Index 0 hat usw.

Greetz Koller

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
curtiss Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 59



BeitragVerfasst: Mi 16.11.05 14:48 
ich kann es jetzt noch nicht ausprobieren aber ich werds ausprobieren ! danke!
Grishnak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 221

Windows XP Home
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: Mi 16.11.05 15:29 
Vorsicht, wenn kein Element der ListBox ausgewählt wurde. Dann hat .ItemIndex nämlich den Wert -1 und sowas
ListBox1.Items[ListBox1.ItemIndex]kann in die Hose gehen!

_________________
Mach' etwas idiotensicher und irgendjemand erfindet einen besseren Idioten!
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 16.11.05 15:46 
so gehts

ausblenden Delphi-Quelltext
1:
2:
3:
if listbox1.itemindex>-1 then
showmessage(listbox1.items[listbox1.itemindex]) else
showmessage('Kein Eintrag ausgewählt');

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot