Autor Beitrag
Dhakiyah
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Fr 20.08.10 09:16 
Habe eine Combobox mit folgenden Werten:

1: Personal
2: EDV
3: Hausmeister

mit Combobox.ItemIndex := 0 stelle ich den Wert 1: Personal ein.

Wenn ich jetzt vorher weiß, dass der Mitarbeiter zur EDV gehört und ich den Wert 2 bzw. EDV habe, wie stelle ich die Combobox so ein, dass sie auf 2: EDV steht?

Wisst ihr was ich meine?

_________________
Es ist soooo flauschig !!!
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 20.08.10 09:19 
Wenn man mit Combobox.ItemIndex := 0 das erste Element anwählt, dann mit Combobox.ItemIndex := 1 das zweite. Aber ich glaube, ich habe das Problem noch nicht richtig verstanden, oder? :gruebel:

_________________
We are, we were and will not be.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Fr 20.08.10 09:22 
Ne...
Ähm, wie erkläre ich das?

So Ähnlich:
Combobox.Itemindex := // 2: EDV

Ich weiß nicht, das EDV den Index 1 hat. Ich weiß nur, dass es 2: EDV ist...

_________________
Es ist soooo flauschig !!!
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Fr 20.08.10 09:24 
Combobox.IndexOf('2: EDV'); lieder den Index für '2: EDV' zurück. (oder so ähnlich)

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 20.08.10 09:25 
Ahso, jetzt wird das klarer. Das sollte über die Eigenschaft Items in etwa so gehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
i := ComboBox.Items.IndexOf('2: EDV');
if i >= 0 then
  ComboBox.ItemIndex := i
else 
  // Fehler

_________________
We are, we were and will not be.
Dhakiyah Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 327
Erhaltene Danke: 5

Windows 7
Delphi XE2
BeitragVerfasst: Fr 20.08.10 09:42 
Jau, das wars. :)

_________________
Es ist soooo flauschig !!!
zuma
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 660
Erhaltene Danke: 21

Win XP, Win7, Win 8
D7 Enterprise, Delphi XE, Interbase (5 - XE)
BeitragVerfasst: Fr 20.08.10 09:47 
du füllst die Combobox mit
ausblenden Delphi-Quelltext
1:
combobox.Add('2: Edv'); ?					


wenn du stattdessen ein
ausblenden Delphi-Quelltext
1:
Combobox.AddObject('2: EDV', TObject(2));					

verwendest, kannst du text-unabhängiger mit der Combobox arbeiten

um die nr des aktuell gewählten eintrags zu holen
ausblenden Delphi-Quelltext
1:
lgewaehlt := Integer(combobox.Items.Objects[combobox.ItemIndex])					

um auf einen bestimmten eintrag zu setzen
ausblenden Delphi-Quelltext
1:
combobox.ItemIndex := combobox.Items.IndexOfObject(TObject(2))					


natürlich ist es dann besser, statt '2' direkt zu schreiben, Konstanten o. ä. zu verwenden ;)

_________________
Ich habe nichts gegen Fremde. Aber diese Fremden sind nicht von hier! (Methusalix)
Warum sich Sorgen ums Leben machen? Keiner überlebts!
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 20.08.10 20:28 
Klar, man kann über den Displayvalue gehen oder TObject(2) als Objekt reinschreiben. Aber, dass das nur gerade die einfachste Hacklösung ist und kein seriöses Design ist, ist hoffentlich auch klar ;)

Ich weiss nicht genau, woher diese 3 Punkte kommen und was du damit alles anstellst, aber idealerweise hast du ein Modellobjekt hinter jedem der 3 Punkte. In dem Modell kann zusätzlich ein technischer Schlüssel drin stehen und noch weitere Informationen und Abhängigkeiten zu anderen Objekten oder was auch immer. Über IndexOfObject kannst du dann den Index rausfinden.

Oder noch besser: Schreib eine class helper Klasse und stelle einige Convenience-Methoden und Properties zur Verfügung (z.B. SelectedObject). Könnte vielleicht so aussehen:
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:
type
  TComboBoxHelper = class helper for TComboBox
  private
    function GetObject: TObject;
    procedure SetObject(const Value: TObject);
  public
    property SelectedObject: TObject read GetObject write SetObject;
  end;

{ TComboBoxHelper }

function TComboBoxHelper.GetObject: TObject;
begin
  if ItemIndex < 0 then
    Exit(nil);

  Result := Items.Objects[ItemIndex];
end;

procedure TComboBoxHelper.SetObject(const Value: TObject);
var
  Index: Integer;
begin
  if Value <> nil then
  begin
    Index := Items.IndexOfObject(Value);
    if Index < 0 then
      raise ERangeError.Create('Value is out of range.');
    ItemIndex := Index;
  end else
    ItemIndex := -1;
end;


Danach kannst du über MyComboBox.SelectedObject := EDV; die gewünschte Selektion machen.

Du kannst zu jeder Zeit den Text ändern oder sogar die Zahl, die vor dem Text steht ändern und kriegst nie ein Durcheinander (der technische Schlüssel bleibt natürlich bestehen, die Zahl, die dem User angezeigt wird ist davon unabhängig). Du musst dir auch keine Sorgen über Eineindeutigkeit des Texts machen und dein Modell kannst du beliebig mit weiteren Properties erweitern.

Das mag für ein Kleinprojekt ein Overkill sein aber wenn das Projekt vielleicht kommerziell eingesetzt wird macht es sicher Sinn.
Tranx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 648
Erhaltene Danke: 85

WIN 2000, WIN XP
D5 Prof
BeitragVerfasst: Sa 21.08.10 16:56 
Im Programm wäre es doch sinnvoll, beim Start den User voreinzustellen. z.B. über eine Ini-Datei.

In der stände dann z.b.

[USER]
Anzahl = 3;
0 = 1: Personal
1 = 2: EDV
2 = 3: Hausmeister

Nr = 1

Dann fragst Du die Inidatei beim Programmstart ab:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
...
Inidatei := TIniDatei.Create(Inidateiname);
with Inidatei do begin
   Anzahl := ReadInteger('USER',Anzahl,0);
   Combobox.Items.Clear;
   for i := 0 to Anzahl-1 do Combobox.Items.add(ReadString('USER',Trim(IntToStr(i)),''));
   Combobox.Itemindex:= ReadInteger('USER','NR',0); 
end;
...


Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt