Autor Beitrag
DD
ontopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 41

Win 2000
D6 Pers
BeitragVerfasst: Mi 16.07.03 14:37 
Folgendes: Ich habe eine Combobox in der eine Menge Strings drin sind. Jetzt möchte ich, dass alle Strings gelöscht werden ausser ein bestimmter, der an irgendeiner Position in der Combobox ist.
Bisher sieht das alles so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
for zaehler:=combobox1.items.Count-1 downto 0 do
      begin
       if combobox1.items[zaehler]<>'xyz'
       then combobox1.Items.delete(zaehler);
       (..)

Warum funktioniert das nicht? Delphi meckert nicht aber löscht immer alle Strings in der Combobox.

Danke im Vorraus
DD

Edit: Danke fürs Moderieren, jedoch stimmte der Titel nicht mehr ganz. Wie man einzelne Einträge löscht weiss ich. Habe den Titel nochmal geändert.

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt & Titel geändert.

_________________
Never change a running system!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 16.07.03 15:14 
Wäre es nicht viel einfacher, es so zu machen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var temp : String;
begin
  temp := ComboBox1.Items[ComboBox1.Items.IndexOf('xyz')];
  ComboBox1.Items.Clear;
  ComboBox1.Items.Add(temp);
end;


So in etwa. Habe leider kein Delphi drauf und seit 'ner Weile nicht mehr geproggt, werden also ein paar Fehler drin sein, aber irgendwie so sollte es gehen.

MfG
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
DD Threadstarter
ontopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 41

Win 2000
D6 Pers
BeitragVerfasst: Mi 16.07.03 16:12 
Hmm gibt es da nicht eine Variante so wie ich mir das vorgestellt habe?

Aber schonmal danke für den Tip. Hat zwar bisher noch nicht so ganz funktoniert aber trotzdem.

Hat hier niemand sonst ne Lösung?

DD

_________________
Never change a running system!
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mi 16.07.03 16:24 
Ich kann bei deinem Code keinen Fehler endecken. Ich hab es deshalb selbst einmal ausprobiert und es funktioniert auf jeden Fall mit der folgenden Procedure:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
var
  Idx: Integer;
begin
  For Idx := ListBox1.Items.Count -1 Downto 0 Do
    If ListBox1.Items [Idx] <> 'abc' Then
      ListBox1.Items.Delete (Idx);

Gruß
Tino
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 16.07.03 16:26 
Habe inzwischen mal Delphi installiert:

(1) Dein Quelltext funktioniert bei mir.
(2) Auch mein Quelltext funktioniert bei mir.

Der Fehler muss also an anderer Stelle liegen. Mein Testprogramm sieht so aus: 1 ComboBox, 1 Editfeld und 1 Button. Es sollen alle Einträge gelöscht werden, außer dem, der den gleichen Text besitzt wie der Text, der im Edit-Feld steht.

Der Quelltext sieht dann so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button1Click(Sender: TObject);
var temp : String;
begin
  temp := ComboBox1.Items[ComboBox1.Items.IndexOf(edit1.text)];
  ComboBox1.Items.Clear;
  ComboBox1.Items.Add(temp);
end;

oder so
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
VAR zaehler : Integer;
begin
  for zaehler:=combobox1.items.Count-1 downto 0 do
  begin
    if combobox1.items[zaehler]<>edit1.text
    then combobox1.Items.delete(zaehler);
  end;
end;

aus.

Und beides funktioniert.

MfG
Peter

//edit: zu langsam ... :(

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
StefanH
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1144

Win XP
D5 Standard, D7 Pers, D2005 Pers
BeitragVerfasst: Mi 16.07.03 16:28 
Drehs mal um:
ausblenden Delphi-Quelltext
1:
2:
for i:=0 to ComboBox1.count-1 do
[...]


Sonst weiß ich nichts

@Peter Lustig: das geht nicht, denn wenn es 2mal 'xyz' gibt, was dann?

Stefan

_________________
"Als es noch keine Computer gab, war das Programmieren noch relativ einfach."(Edsger W. Dijkstra)
"Ich bin nicht von Sinnen, sondern ich rede wahre und vernünftige Worte." (Paulus)
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mi 16.07.03 16:35 
StefanH hat folgendes geschrieben:
Drehs mal um:

Das wird dann aber nicht funktionieren. Wenn du z. B. das Item mit dem Index 3 löscht verschieben sich alle folgenden Items eine Stelle nach oben. Das heißt das das nächste Item wieder den Index 3 hat. Die Schleife ist aber schon bei 4. Es wird also ein Item übersprungen.

Also immer bei Lösch-Aktionen rückwärts zählen lassen!

Gruß
Tino
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 16.07.03 16:36 
@StefanH: Im ersten Posting verwendet DD die Formulierung "ein bestimmter" Eintrag. Deswegen bin ich davon ausgegangen, dass es nicht mehrere gibt.

Dein Quelltext wird jedoch nicht funktionieren, da dann Items übersprungen werden.

MfG
Peter

//edit: @Tino: so langsam wird's zur Gewohnheit

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
DD Threadstarter
ontopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 41

Win 2000
D6 Pers
BeitragVerfasst: Mi 16.07.03 16:52 
Also es gibt mehrere Items (um genau zu sein 23).
Löschen geht nur rückwärts wie schon gesagt wurde. Deswegen kann ich das nicht umdrehen. Ich verstehe auch echt nicht, wo da ein Fehler sein soll bei mir im Quelltext, da es eigentlich ganz genau das ist was Tino geschrieben hat.
Bin ratlos...woran könnte es denn dann liegen????

MfG DD

_________________
Never change a running system!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 16.07.03 17:01 
Zitat:
Also es gibt mehrere Items (um genau zu sein 23).
War schon klar. StefanH und ich bezogen uns darauf, ob es genau ein Item gibt, welches stehen bleiben soll oder ob es mehrere Items gibt, die verschont werden sollen.

Zitat:
Ich verstehe auch echt nicht, wo da ein Fehler sein soll bei mir im Quelltext, da es eigentlich ganz genau das ist was Tino geschrieben hat.
Wie ich auch schon geschrieben habe, liegt der Fehler definitiv nicht in dem geposteten Quelltext.

Am besten postest Du mal ein bisschen mehr Quelltext drum herum, vielleicht zeigt sich dann etwas!

MfG
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
DD Threadstarter
ontopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 41

Win 2000
D6 Pers
BeitragVerfasst: Mi 16.07.03 17:13 
So habe jetzt einfach mal den kompletten Quelltext genommen. Die Namen der Komponenten sind jetzt etwas anders also wundert euch nicht, wenn da was von Runelords und Pistol steht, ich brauche das für ein Fantasygameprojekt.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ArmyPointCombo: TComboBox;
    ArmyPointlbl: TLabel;
    RaceCombo: TComboBox;
    Racelbl: TLabel;
    btgoon: TButton;
    ComboBox1: TComboBox;
    Troopselectlbl: TLabel;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    ComboBox5: TComboBox;
    ComboBox6: TComboBox;
    ComboBox7: TComboBox;
    ComboBox8: TComboBox;
    ComboBox9: TComboBox;
    ComboBox10: TComboBox;
    ComboBox11: TComboBox;
    ComboBox12: TComboBox;
    ComboBox13: TComboBox;
    ComboBox14: TComboBox;
    ComboBox15: TComboBox;
    ComboBox16: TComboBox;
    MagicItemsEdit1: TEdit;
    MagicItemsEdit2: TEdit;
    MagicItemsEdit3: TEdit;
    MagicItemsEdit4: TEdit;
    MagicItemsEdit5: TEdit;
    MagicItemsEdit6: TEdit;
    MagicItemsEdit7: TEdit;
    MagicItemsEdit8: TEdit;
    MagicItemsEdit9: TEdit;
    MagicItemsEdit10: TEdit;
    MagicItemsEdit11: TEdit;
    MagicItemsEdit12: TEdit;
    MagicItemsEdit13: TEdit;
    MagicItemsEdit14: TEdit;
    MagicItemsEdit15: TEdit;
    MagicItemsEdit16: TEdit;
    lblmagicitems: TLabel;
    Comboweapon1: TComboBox;
    lblweapons: TLabel;
    Comboweapon2: TComboBox;
    Comboweapon3: TComboBox;
    Comboweapon4: TComboBox;
    Comboweapon5: TComboBox;
    Comboweapon6: TComboBox;
    Comboweapon7: TComboBox;
    Comboweapon8: TComboBox;
    Comboweapon9: TComboBox;
    Comboweapon10: TComboBox;
    Comboweapon11: TComboBox;
    Comboweapon12: TComboBox;
    Comboweapon13: TComboBox;
    Comboweapon14: TComboBox;
    Comboweapon15: TComboBox;
    Comboweapon16: TComboBox;
    Comboarmour1: TComboBox;
    lblarmour: TLabel;
    lblnumber: TLabel;
    Editnumber1: TEdit;
    Editnumber2: TEdit;
    Editnumber3: TEdit;
    Editnumber4: TEdit;
    Editnumber5: TEdit;
    Editnumber6: TEdit;
    Editnumber7: TEdit;
    Editnumber8: TEdit;
    Editnumber9: TEdit;
    Editnumber10: TEdit;
    Editnumber11: TEdit;
    Editnumber12: TEdit;
    Editnumber13: TEdit;
    Editnumber14: TEdit;
    Editnumber15: TEdit;
    Editnumber16: TEdit;
    Comboarmour2: TComboBox;
    Comboarmour3: TComboBox;
    Comboarmour4: TComboBox;
    Comboarmour5: TComboBox;
    Comboarmour6: TComboBox;
    Comboarmour7: TComboBox;
    Comboarmour8: TComboBox;
    Comboarmour9: TComboBox;
    Comboarmour10: TComboBox;
    Comboarmour11: TComboBox;
    Comboarmour12: TComboBox;
    Comboarmour13: TComboBox;
    Comboarmour14: TComboBox;
    Comboarmour15: TComboBox;
    Comboarmour16: TComboBox;
    bttest: TButton;
    procedure btgoonClick(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    procedure zwergeincombo;
    procedure goonvisible;
  end;

var
  Form1: TForm1;
  zaehler,zaehler2: integer;

implementation

{$R *.dfm}



procedure TForm1.btgoonClick(Sender: TObject);
begin
  Showmessage('You choose an army of '+Racecombo.text+' with '+ArmyPointCombo.Text);
  goonvisible;
  if RaceCombo.text='Dwarfs'
  then begin
         zwergeincombo;
       end
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if combobox1.Text='Runelord'
then begin
      showmessage('Runelord ausgewählt');
      for zaehler:=comboweapon1.Items.count-1 downto 2 do
      begin
       comboweapon1.Items.Delete(zaehler);
      end
      {Runelord darf alle Rüstungen tragen}
     end
else if combobox1.text='Lord'
then begin
      showmessage('Lord ausgewählt');
      for zaehler:=comboweapon1.items.Count-1 downto 0 do
      begin
       if comboweapon1.items[zaehler]<>'Pistol'
       then comboweapon1.Items.delete(zaehler); {Hier ist die kritische Stelle}
      end;
   end
end;

procedure Tform1.zwergeincombo;
begin
 for zaehler:=combobox1.Items.count-1 downto 22 do
 begin
  combobox1.Items.delete(zaehler);
 end;
 for zaehler:=combobox2.Items.count-1 downto 22 do
 begin
  combobox2.Items.delete(zaehler);
 end;
 for zaehler:=combobox3.Items.count-1 downto 22 do
 begin
  combobox3.Items.delete(zaehler);
 end ;
 for zaehler:=combobox4.Items.count-1 downto 22 do
 begin
  combobox4.Items.delete(zaehler);
 end ;
 for zaehler:=combobox5.Items.count-1 downto 22 do
 begin
  combobox5.Items.delete(zaehler);
 end ;
 for zaehler:=combobox6.Items.count-1 downto 22 do
 begin
  combobox6.Items.delete(zaehler);
 end ;
 for zaehler:=combobox7.Items.count-1 downto 22 do
 begin
  combobox7.Items.delete(zaehler);
 end ;
 for zaehler:=combobox8.Items.count-1 downto 22 do
 begin
  combobox8.Items.delete(zaehler);
 end ;
 for zaehler:=combobox9.Items.count-1 downto 22 do
 begin
  combobox9.Items.delete(zaehler);
 end ;
 for zaehler:=combobox10.Items.count-1 downto 22 do
 begin
  combobox10.Items.delete(zaehler);
 end ;
 for zaehler:=combobox11.Items.count-1 downto 22 do
 begin
  combobox11.Items.delete(zaehler);
 end ;
 for zaehler:=combobox12.Items.count-1 downto 22 do
 begin
  combobox12.Items.delete(zaehler);
 end ;
 for zaehler:=combobox13.Items.count-1 downto 22 do
 begin
  combobox13.Items.delete(zaehler);
 end ;
 for zaehler:=combobox14.Items.count-1 downto 22 do
 begin
  combobox14.Items.delete(zaehler);
 end ;
 for zaehler:=combobox15.Items.count-1 downto 22 do
 begin
  combobox15.Items.delete(zaehler);
 end ;
 for zaehler:=combobox16.Items.count-1 downto 22 do
 begin
  combobox16.Items.delete(zaehler);
 end
end;

procedure Tform1.goonvisible;
begin
  Combobox1.Visible:=true;
  Combobox2.Visible:=true;
  Combobox3.Visible:=true;
  Combobox4.visible:=true;
  Combobox5.visible:=true;
  Combobox6.visible:=true;
  Combobox7.Visible:=true;
  Combobox8.Visible:=true;
  Combobox9.Visible:=true;
  Combobox10.Visible:=true;
  Combobox11.visible:=true;
  Combobox12.visible:=true;
  Combobox13.visible:=true;
  Combobox14.visible:=true;
  Combobox15.visible:=true;
  Combobox16.visible:=true;
  troopselectlbl.Visible:=true;
  Comboweapon1.visible:=true;
  Comboweapon2.visible:=true;
  Comboweapon3.visible:=true;
  Comboweapon4.visible:=true;
  Comboweapon5.visible:=true;
  Comboweapon6.visible:=true;
  Comboweapon7.visible:=true;
  Comboweapon8.visible:=true;
  Comboweapon9.visible:=true;
  Comboweapon10.visible:=true;
  Comboweapon11.visible:=true;
  Comboweapon12.visible:=true;
  Comboweapon13.visible:=true;
  Comboweapon14.visible:=true;
  Comboweapon15.visible:=true;
  Comboweapon16.visible:=true;
  lblweapons.visible:=true;
  Comboarmour1.visible:=true;
  Comboarmour2.visible:=true;
  Comboarmour3.visible:=true;
  Comboarmour4.visible:=true;
  Comboarmour5.visible:=true;
  Comboarmour6.visible:=true;
  Comboarmour7.visible:=true;
  Comboarmour8.visible:=true;
  Comboarmour9.visible:=true;
  Comboarmour10.visible:=true;
  Comboarmour11.visible:=true;
  Comboarmour12.visible:=true;
  Comboarmour13.visible:=true;
  Comboarmour14.visible:=true;
  Comboarmour15.visible:=true;
  Comboarmour16.visible:=true;
  lblarmour.visible:=true;
  Editnumber1.visible:=true;
  Editnumber2.visible:=true;
  Editnumber3.visible:=true;
  Editnumber4.visible:=true;
  Editnumber5.visible:=true;
  Editnumber6.visible:=true;
  Editnumber7.visible:=true;
  Editnumber8.visible:=true;
  Editnumber9.visible:=true;
  Editnumber10.visible:=true;
  Editnumber11.visible:=true;
  Editnumber12.visible:=true;
  Editnumber13.visible:=true;
  Editnumber14.visible:=true;
  Editnumber15.visible:=true;
  Editnumber16.visible:=true;
  lblnumber.visible:=true;
  MagicItemsEdit1.visible:=true;
  MagicItemsEdit2.visible:=true;
  MagicItemsEdit3.visible:=true;
  MagicItemsEdit4.visible:=true;
  MagicItemsEdit5.visible:=true;
  MagicItemsEdit6.visible:=true;
  MagicItemsEdit7.visible:=true;
  MagicItemsEdit8.visible:=true;
  MagicItemsEdit9.visible:=true;
  MagicItemsEdit10.visible:=true;
  MagicItemsEdit11.visible:=true;
  MagicItemsEdit12.visible:=true;
  MagicItemsEdit13.visible:=true;
  MagicItemsEdit14.visible:=true;
  MagicItemsEdit15.visible:=true;
  MagicItemsEdit16.visible:=true;
  lblmagicitems.visible:=true;
end;





end.


vielleicht entdeckt ja jetzt jmd. woran es liegen kann. Das Programm läuft mit dem Quelltext, jedoch löscht er immer alle Items aus der Combobox namens Comboweapon1

_________________
Never change a running system!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 16.07.03 17:38 
Hm, so auf anhieb sehe ich da nichts. Hast Du mal einen Breakpoint an die Stelle gesetzt und das Ganze im Einzelschrittverfahren getestet?

MfG
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Mi 16.07.03 18:14 
Hallo

welche Werte stehen denn in der Combobox?

dir ist aber klar das combobox1.items[zaehler]<>'xyz' im gegensatz zu indexof groß und klein unterscheidet. dh. wenn in der Combobox xYz steht und du auf xyz prüfst, kommst du mit deinem Vergleich nicht weiter.
wo werden die Werte zugewiesen ? viellecihst schleicht sich auch irgendow ein leerzeichen ein.

mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)