Autor Beitrag
Arne Danikowski
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194



BeitragVerfasst: Mi 06.05.09 08:55 
Hallo,

ich möchte in einer Combobox Jahreszahlen zur Laufzeit eintragen
folgendes habe ich bereits

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure Twartungdrucken.FormActivate(Sender: TObject);
VAR Jahr1,Jahr2,Jahr3,Jahr4,Jahr5,Jahr6:TDATE;
begin
DecodeDate(now, Year, Month, Day);
Jahr1:=Year;
Jahr2:=(Year - 1);
Jahr3:=(year - 2);
Jahr4:=(year + 1);
Jahr5:=(year + 2);
Jahr6:=(year + 3);

JahrCombo.Items.Add(DateToStr(Jahr3));
JahrCombo.Items.Add(DateToStr(Jahr2));
JahrCombo.Items.Add(DateToStr(Jahr1));
JahrCombo.Items.Add(DateToStr(Jahr4));
JahrCombo.Items.Add(DateToStr(Jahr5));
JahrCombo.Items.Add(DateToStr(Jahr6));


Das funktioniert soweit, allerdings bekomme ich immer das komplette Datum ausgegeben und nicht nur die Jahreszahl. Es scheint am DecodateDate zu liegen. Die WORD Variable Year enthält nicht nur das Jahr.
Nersgatt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1581
Erhaltene Danke: 279


Delphi 10 Seattle Prof.
BeitragVerfasst: Mi 06.05.09 09:10 
Deklarier mal Jahr1 - Jahr6 als Word und benutze unten IntToStr statt DateToStr.

_________________
Gruß, Jens
Zuerst ignorieren sie dich, dann lachen sie über dich, dann bekämpfen sie dich und dann gewinnst du. (Mahatma Gandhi)
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 06.05.09 09:36 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
    y,m,d: word;
begin
  DecodeDate(date,y,m,d);
  for I := -2 to 3 do
    if i <> 0 then
      ComboBox1.Items.Add(IntToStr(y + i));
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 06.05.09 10:46 
Moin!
ausblenden Delphi-Quelltext
1:
2:
3:
4:
uses
  ..., DateUtils;

ShowMessage(IntToStr(YearOf(Now)));
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 06.05.09 10:57 
Öhm... ja. Und?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 06.05.09 11:12 
Moin!

OK, dann die lange Version... :roll:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
uses
  ..., DateUtils;

  var
    i: Integer;
begin
  for i := -2 to 3 do
    ComboBox1.Items.Add(IntToStr(YearOf(Now) +i));
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 06.05.09 11:15 
OK, aber wo liegt der Mehrwert? Mit DecodeDate bekomme ich das Jahr auch und muss nicht innerhalb einer Schleife ständig eine Funktion aufrufen. Außerdem spare ich eine Unit in der uses-Klausel ;)
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 06.05.09 11:17 
DecodeDate verbräte unnötig Stack und liefert zudem Werte, die nicht benötigt werden?

Und bzgl. der Schleife: Aktuelles Jahr einmal berechnen, Cachen und wiederverwenden in Berechnungen ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 06.05.09 11:17 
user profile iconDeddyH hat folgendes geschrieben Zum zitierten Posting springen:
OK, aber wo liegt der Mehrwert? Mit DecodeDate bekomme ich das Jahr auch und muss nicht innerhalb einer Schleife ständig eine Funktion aufrufen.

Könnte man auch vor der Schleife einmal aufrufen und dann merken ;)


user profile iconDeddyH hat folgendes geschrieben Zum zitierten Posting springen:
Außerdem spare ich eine Unit in der uses-Klausel ;)

Dafür hast du zwei unnötige Variablen m und d. ;)
Denk mal, kann man drehen wie man will. Ist wohl eher eine Geschmacksfrage.

_________________
PROGRAMMER: A device for converting coffee into software.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 06.05.09 11:20 
Xentar hat folgendes geschrieben:
Ist wohl eher eine Geschmacksfrage.

Das sehe ich auch so, schließlich führen viele Wege nach Rom. Außerdem ging es mir primär darum, eine Lösung mit einer Schleife aufzuzeigen statt der ganzen lokalen Variablen wie im Ausgangspost.
Arne Danikowski Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194



BeitragVerfasst: Do 07.05.09 11:58 
Hi danke für die vielen Antworten.

Ich habe mich für die Variante

ausblenden Delphi-Quelltext
1:
2:
3:
for I := -2 to 0 do  //aktuelles Jahr und 2 Jahre davor
   JahrCombo.Items.Add(IntToStr(YearOf(Now) +i)); //Jahr des Datums in Combobox
   JahrCombo.ItemIndex:=2//Standard Auswahl aktuelles Jahr



entschieden. Es scheint mit doch die eleganteste Methode zu sein. Damit wäre meine erste combobox fertig Juhu :)

Nun zur Zweiten. Hat immer noch was mit datum zu tun. Die zweite Combobox enthält vier feste Werte (1,2,3,4). Diese sollen die Quatale eines Jahres darstellen.
nun möchte ich das zutreffende Quatal automatisch in der Combobax aktivieren.
Die Quatale ergeben sich so

1.1.xx - 31.03.xx
1.4.xx - 31.06.xx
1.7.xx - 30.09.xx
1.10.xx - 31.12.xx

Die xx sollen dabei durch den aktuell ausgewählten Wert aus der ersten Combobox mit den Jahreszahlen ersetzt werden.


So Wei bin ich gekommen:
ausblenden 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:
DecodeDate(now, Year, Month, Day);// Datum zerlegen
   s1:=IntToStr(day);
   s2:=IntToStr(Month);
   s3:=JahrCombo.Text;
   s4:=(S1 +'.'+S2 +'.'+s3);
   
   d1:=StrToDate(S4); //Aktuelles Datum

   s5:='1';
   s6:='4';
   s7:=(S5 +'.'+S6 +'.'+s3); //Datum 2.Quatal ab 01.04.
   d2:=StrToDate(S7);

   s8:='7';
   s9:=(S5 +'.'+S8 +'.'+s3); //Datum 3.Quatat ab 01.07
   d3:=StrToDate(S9);

   s10:='10';
   s11:=(S5 +'.'+S10 +'.'+s3); //Datum 4.Quatal  ab 01.10
   d4:=StrToDate(S11);

   if d1 < d2 then qualtalcombo.ItemIndex:=0;
   if d1 > d2 then qualtalcombo.ItemIndex:=1;


Allerdings habe ich nun Probleme mit dem 3.Quatal.

if d3 < d4 then qualtalcombo.ItemIndex:=3; Geht nicht, dass trifft ja immer zu.
if d3 > d1 then qualtalcombo.ItemIndex:=3; Geht nicht kann auch auf 2 Quatale zutreffen;

hat da jemand ne idee?
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Do 07.05.09 12:07 
1. Eigentlich handelt es sich hier um ein neues Thema... ;)
2. Warum so kompliziert..?? Die Tage können dir doch egal sein, es kommt doch nur auf den Monat an. Und wieder zusammenpuzzeln musst du das Datum doch auch nicht mehr.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
DecodeDate(now, Year, Month, Day);// Datum zerlegen
if month in [1..3then ComboBox1.ItemIndex := 0;
if month in [4..6then ComboBox1.ItemIndex := 1;
if month in [7..9then ComboBox1.ItemIndex := 2;
if month in [10..12then ComboBox1.ItemIndex := 3;

So in der Art.

Edit: Das ganze könnte man natürlich auch noch als case-Abfrage schreiben..
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
case month of
  1..3// 1
  4..6// 2
  // usw.
end;

_________________
PROGRAMMER: A device for converting coffee into software.


Zuletzt bearbeitet von Xentar am Do 07.05.09 12:10, insgesamt 1-mal bearbeitet
Mike19
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 256

Win XP, Vista, Win 7
Delphi 2005, Turbo Delphi
BeitragVerfasst: Do 07.05.09 12:07 
Zitat:
for I := -2 to 0 do //aktuelles Jahr und 2 Jahre davor
JahrCombo.Items.Add(IntToStr(YearOf(Now) +i)); //Jahr des Datums in Combobox
JahrCombo.ItemIndex:=2; //Standard Auswahl aktuelles Jahr



was spricht gegen

ausblenden Delphi-Quelltext
1:
2:
3:
4:
for I := year(now) to year(+10do
begin
JahrCombo.Items.Add(i);
end
Arne Danikowski Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194



BeitragVerfasst: Do 07.05.09 12:23 
Hallo,
und erst einmal wieder vielen dank für die schnellen Antworten.


Danke Xentar da haste Wahr :D Deine Lösung ist schöner wie meine.

Zu Mike. Es spricht nichts dagegen, aber es stand am Anfang die Auswah
ob Decodedate zu verwenden oder nicht. Das hat sich nun erledigt, da ich für das zweite Problem
Decodate brauche.
Nun werde ich mir Deinen Lösungsvorschlag mal ansehen
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 07.05.09 13:12 
Kürzer geht das Month-->Quartal so: quartal := (month -1div 3;
Dann hat man gleich den ItemIndex, den man auswählen muss, ansonsten +1 rechnen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.