سؤال

Trying to have some main template to change visibility of groupboxes based on the number in main editbox. EditDay is the edit box, where only numbers are in it

day:=DayOfTheYear(Now); 
EditDay.Text:=day;

so it's basicaly the day of the year. Anyway, I need a groupbox (with a few memos) for everyday of the year. Since this is a file with records, which another program will read off for everyday different infos, I need that file writer first, so I can even make one. That's what this one is Since I'm doing a record file, there has to be all boxes firstly filled up before I'll write to a file, so I need to have Groupboxes to be visible one at a time, each one for a day I specify in a main TEdit. Right now I'm stuck with setting the visibility of the groupboxes; The code below gives me Access violation error.

x is the number specified in TEdit named EditDay. I wanted to make an y every other day but the day in EditDay box so all but x;

x : Integer;     
y : Integer;

procedure TWriteForm.DayCheckTimer(Sender: TObject);
begin
  x:=StrToInt(EditDay.Text);
  y:=Not x;
  (FindComponent('GroupBox'+IntToStr(x)) as TGroupBox).Visible := True;
  (FindComponent('GroupBox'+IntToStr(y)) as TGroupBox).Visible := False;

Tried to set y:=[1..365] and not x; [1..365] - x; and several others, but none of them worked.

Where am I wrong? .. Any help will be appreciated. :))

[I'm kinda beginner, yes..]

هل كانت مفيدة؟

المحلول 2

Here a small sample project to deal with a lot (365) of records.

unit RecordEdit_ViewU;

interface

uses
  SysUtils,
  Controls, Forms, Dialogs, StdCtrls, System.Classes;

type
  TPerson = record
    Firstname : string[50]; // shortstring !!
    Lastname : string[50];  // shortstring !!
  end;

  TRecordEdit_View = class( TForm )
    Current_Edit : TEdit;
    Data_Firstname_Edit : TEdit;
    Data_Lastname_Edit : TEdit;
    Data_Prev_Button : TButton;
    Data_Next_Button : TButton;
    Data_Save_Button : TButton;
    procedure FormCreate( Sender : TObject );
    procedure Current_EditChange( Sender : TObject );
    procedure Data_Prev_ButtonClick( Sender : TObject );
    procedure Data_Next_ButtonClick( Sender : TObject );
    procedure Data_Save_ButtonClick( Sender : TObject );
  private
    FData :    array [1 .. 365] of TPerson;
    FCurrent : Integer;
    procedure SetCurrent( const Value : Integer );
    procedure InitData;
    procedure StoreCurrent;
    procedure LoadCurrent;
    procedure SaveData;
  public
    property Current : Integer read FCurrent write SetCurrent;
  end;

var
  RecordEdit_View : TRecordEdit_View;

implementation

{$R *.dfm}

procedure TRecordEdit_View.Current_EditChange( Sender : TObject );
begin
  Current := StrToIntDef( Current_Edit.Text, 0 ); // convert text to integer
end;

procedure TRecordEdit_View.Data_Next_ButtonClick( Sender : TObject );
begin
  Current := Current + 1; // next record
end;

procedure TRecordEdit_View.Data_Prev_ButtonClick( Sender : TObject );
begin
  Current := Current - 1; // prev record
end;

procedure TRecordEdit_View.Data_Save_ButtonClick( Sender : TObject );
begin
  SaveData;
end;

procedure TRecordEdit_View.FormCreate( Sender : TObject );
begin
  InitData;
end;

procedure TRecordEdit_View.InitData;
begin
  FCurrent := Low( FData ); // first record
  LoadCurrent;              // load data from record
end;

procedure TRecordEdit_View.LoadCurrent;
begin
  // Data from record to controls
  Data_Firstname_Edit.Text := FData[Current].Firstname;
  Data_Lastname_Edit.Text  := FData[Current].Lastname;
  // Update the Current-Edit
  Current_Edit.Text := IntToStr( Current );
end;

procedure TRecordEdit_View.SaveData;
begin
  ShowMessage( 'Needs to be implemented!' );
end;

procedure TRecordEdit_View.SetCurrent( const Value : Integer );
begin
  // check, if we have a change and if we can change to the new index
  if ( Value <> Current ) and ( Value >= Low( FData ) ) and ( Value <= High( FData ) )
  then
    begin
      StoreCurrent;      // store data from controls
      FCurrent := Value; // change current index
      LoadCurrent;       // load data from record
    end;
end;

procedure TRecordEdit_View.StoreCurrent;
begin
  // Data from controls to record
  FData[Current].Firstname := Data_Firstname_Edit.Text;
  FData[Current].Lastname  := Data_Lastname_Edit.Text;
end;

end.

And the form

object RecordEdit_View: TRecordEdit_View
  Left = 0
  Top = 0
  Caption = 'RecordEdit_View'
  ClientHeight = 337
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Current_Edit: TEdit
    Left = 107
    Top = 16
    Width = 75
    Height = 21
    TabOrder = 0
    Text = 'Current_Edit'
    OnChange = Current_EditChange
  end
  object Data_Firstname_Edit: TEdit
    Left = 80
    Top = 56
    Width = 129
    Height = 21
    MaxLength = 50
    TabOrder = 1
    Text = 'Data_Firstname_Edit'
  end
  object Data_Lastname_Edit: TEdit
    Left = 80
    Top = 83
    Width = 129
    Height = 21
    MaxLength = 50
    TabOrder = 2
    Text = 'Data_Lastname_Edit'
  end
  object Data_Prev_Button: TButton
    Left = 80
    Top = 16
    Width = 21
    Height = 21
    Caption = '<'
    TabOrder = 3
    OnClick = Data_Prev_ButtonClick
  end
  object Data_Next_Button: TButton
    Left = 188
    Top = 16
    Width = 21
    Height = 21
    Caption = '>'
    TabOrder = 4
    OnClick = Data_Next_ButtonClick
  end
  object Data_Save_Button: TButton
    Left = 80
    Top = 118
    Width = 129
    Height = 25
    Caption = 'Save Data'
    TabOrder = 5
    OnClick = Data_Save_ButtonClick
  end
end

You can get the complete Source and Executable here

نصائح أخرى

view y:=Not x; in the debugger x=1 will be y=-2, you won't find a Compoentr with this name.

You will have to iterate over your componets

For i := 1 to mCount

and set visibilty by condtition

(FindComponent('GroupBox'+IntToStr(i)) as TGroupBox).Visible := y = i;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top