Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the 'case' statement and changes to the form at runtime

Status
Not open for further replies.

mjsof84

Programmer
Mar 23, 2003
22
GB
right...last question today, promise. ;-)

the application i'm doing requires several very different screens on the same form. to get round this i thought to set up a case statement as so..

---
case section of
0: begin
Form1.MainMenu1.items[1].Enabled := False;
Form1.MainMenu1.items[2].Enabled := False;
edit1.visible := false;
button1.visible := true;
end;
1: begin
Form1.MainMenu1.items[1].Enabled := True;
Form1.MainMenu1.items[2].Enabled := True;
edit1.visible := true;
button1.visible := false;
end;
end;
---

where section is a variable that changes according to which section of the program the user is in. (this is obviously going to get a lot more complex).

anyway - the problem i'm belatedly faced with is how to execute this bit of code. originally thought to put it in the procedure TForm1.FormCreate but i've seen the error of doing that.

so, is what i'm describing possible or am I being a bit naive/inexperienced/stupid etc?

cheers.
 
Should be no problem at all, assuming section is an integer variable defined in the public section of TForm1. Simply set the value before showing the form and put the case code in the forms OnShow event handler.
e.g. in the form that invokes the display of Form1, I would expect to see something like this:
Code:
  Form1.section := 1;
  Form1.ShowModal;
Or, if you want to do this from within Form1 while it's active, just set the value you want in section.

You say it's going to get complicated. In that case, I would consider setting up something like this for better documentation / ease of use:
Code:
type
  TDisplayMode = (dmThis, dmThat, dmTheOther);

  TForm1 = class(TForm)
    :
    :
  public
    FDisplayMode: TDisplayMode;
    :
    :
:
:
implementation
:
:
  case FDisplayMode of
    dmThis:
      begin
        MainMenu1.items[1].Enabled := False;
        MainMenu1.items[2].Enabled := False;
        :
        :
      end;
    dmThat:
      begin
        MainMenu1.items[1].Enabled := True;
        MainMenu1.items[2].Enabled := True;
        :
        :
      end;
  end; // case
:
:
 
ok..

i've got it working, kinda.

however, given that i'm trying to call the change from within the same form, i need to do this..

begin
section := 1;
form1.Visible := false;
form1.Visible := true;
end;

before the prog will realise it has to chance, given that the case code is on the onShow event.

any ideas of how to get around this?
 
Don't put the code in OnShow. Put it in its own procedure, and invoke that same procedure from both the OnShow event handler and from wherever you change the section value.

 
what would be the syntax for envoking the procedure, and how would you declare a new procedure?

just like..

procedure MyProc (Sender: TObject);

?

thanks a lot
 
declare:

procedure ChangeDisplay;


in code

procedure tForm1.ChangeDisplay;
begin

//code here

end;


Then just call ChangeDisplay on the form show event and wherever else it needs to be.
 
You can declare your own procedures and functions in many ways. The following is specific to the situation you have (as I understand it), and should not be taken as THE way to declare a procedure.

Since you want access to various components that you placed on TForm1, it will be easiest to make the procedure part of TForm1 as well. That way you don't have to qualify the references with "Form1." when setting the properties. The parameter list is up to you. There is usually no need to have "Sender: TObject" in your own private procedures.
Code:
type
  TForm1 = class(TForm)
    :
    :
  public
    FSection: integer;
    :
    :
  private
    procedure SetUpForNewSection( ASection: integer );
    :
:
:
implementation
:
:
procedure TForm1.SetUpForNewSection( ASection: integer );
begin
  case ASection of
    0: begin
        :
       end;
    1: begin
        :
       end;
   end;
  // Save current section value for the next FormShow event
  FSection := ASection;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FSection := 1;  // Initial section value is 1.
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  SetUpForNewSection( FSection );
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetUpForNewSection( 0 );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SetUpForNewSection( 1 );
end;
A procedure may be thought of as simply a function without a result.

Of course, you should set the initial value in FormCreate to whatever the initial value shoule be for your application.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top