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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open form based on user selection 2

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have a main form where the user enters a citation number and selects the type of citation (cbCitationType) it is: Misdemeanor, Traffic, TRACS. I have three forms that have the layout of the different citation types: fMisdemeanor, fTraffic and fTRACS. Depending on what the user has selected in cbCitationType I would like to open the corresponding form. Can someone give me an example of how I could accomplish this?

Code:
procedure TfMain.bFindImageClick(Sender: TObject);
begin
  if cbCitationType.ItemIndex < 0 then
  begin
    ShowMessage('Please select the citation type!');
    Exit;
  end;

  if eCitationNumber.Text = '' then
  begin
    ShowMessage('Please enter the citation number!');
    Exit;
  end;

  //so here - take the value from the comboBox, prepend an 'f' and open that form

end;

Leslie

Have you met Hardy Heron?
 
If the number of citation types is likely to be low (say less than 10) then I would suggest taking the simple approach:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  citation: string;
begin
  citation := cbCitationType.Text;
  if citation = 'Misdemeanor' then 
    fMisdemeanor.Show
  else if citation = 'Traffic' then 
    fTraffic.Show
  else if citation = 'TRACS' then 
    fTRACS.Show
  else
    ShowMessage('Please select the citation type!');
  ;
end;

Andrew
Hampshire, UK
 
Hi CADTenchy,

if the forms are already created, you can add the references to the combobox:

Code:
 ...
 cbCitationType.AddItem('Misdemeanor' , fMisdemeanor);
 cbCitationType.AddItem('Traffic' , fTraffic);
 cbCitationType.AddItem('TRACS' , fTRACS);
 ...

procedure TfMain.bFindImageClick(Sender: TObject);
begin
  if cbCitationType.ItemIndex < 0 then
  begin
    ShowMessage('Please select the citation type!');
    Exit;
  end;

  if eCitationNumber.Text = '' then
  begin
    ShowMessage('Please enter the citation number!');
    Exit;
  end;
 // show the associated form
 TForm(cbCitationType.Items.Objects[cbCitationType.ItemIndex]).Show;
 
end;

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks for the info Daddy (and it's leslie not CADTenchy!)

I thought that this looked promising:
Code:
TForm(cbCitationType.Items.Objects[cbCitationType.ItemIndex]).Show;

but I get an access violation.

All the forms are auto created so they are available....not sure what's not correct....

Thanks!
Leslie
 
oops, sorry Leslie (had wrong thread in mind)

did you add them to the combo?

like I showed you:

cbCitationType.AddItem('Misdemeanor' , fMisdemeanor);
cbCitationType.AddItem('Traffic' , fTraffic);
cbCitationType.AddItem('TRACS' , fTRACS);

you can put this in the formcreate event of the form where the code resides...

/Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Could the
TForm(cbCitationType.Items.Objects[cbCitationType.ItemIndex]).Show;
be wrong?

Usually you only use, for example, Form1.Show;
or Form2.Show;
Not TForm1.Show;

Atleast what I know :)
 
yes I added the objects to the combo box, I did it in the Form Create, but I don't think that should affect it....here's what I've got:

Code:
unit Main;

interface

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

type
  TfMain = class(TForm)
    eCitationNumber: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    cbCitationType: TComboBox;
    bFindImage: TButton;
    MainMenu1: TMainMenu;
    Images1: TMenuItem;
    DeactivateImage1: TMenuItem;
    UnprocessedImageCount1: TMenuItem;
    procedure bFindImageClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    strSavePath : string;
    function GetSavePath() : String;
  end;

var
  fMain: TfMain;

implementation

uses DMod, Misdemeanor, Traffic, TRACS;

{$R *.dfm}

procedure TfMain.bFindImageClick(Sender: TObject);
var
citation : string;
begin

  if eCitationNumber.Text = '' then
  begin
    ShowMessage('Please enter the citation number!');
    Exit;
  end;

    if cbCitationType.ItemIndex < 0 then
  begin
    ShowMessage('Please select the citation type!');
    Exit;
  end
  else
    citation := cbCitationType.Text;


  // show the associated form
  TForm(cbCitationType.Items.Objects[cbCitationType.ItemIndex]).Show;

end;

procedure TfMain.FormCreate(Sender: TObject);
begin
  DMCit.qStatutes.Active := True;
  cbCitationType.AddItem('Misdemeanor' , fMisdemeanor);
  cbCitationType.AddItem('Traffic' , fTraffic);
  cbCitationType.AddItem('TRACS' , fTRACS);
end;

procedure TfMain.FormShow(Sender: TObject);
begin
  strSavepath := GetSavePath;
end;

function TfMain.GetSavePath() : String;
begin
  result := '\\fileandprint\private\landrews\images\00000082\';
end;

end.

Leslie

Have you met Hardy Heron?
 
Ante,
thanks for the suggestion, however when I remove the T and just have:
Code:
Form(cbCitationType.Items.Objects[cbCitationType.ItemIndex]).Show;

I get an error in the IDE: Undeclared identifier: 'Form'

Leslie
 
I suspect the problem is that when
Code:
  cbCitationType.AddItem('Misdemeanor' , fMisdemeanor);
  cbCitationType.AddItem('Traffic' , fTraffic);
  cbCitationType.AddItem('TRACS' , fTRACS);
is being executed in the FormCreate procedure that fMideameanor, fTraffic and FTRACS are still nil as those forms have not been created.

If you put that code at some other point, say, in the FormActivate handler then they will be set up and your program will work.

On the other hand you might find my original solution to be a lot clearer and simpler :)

Andrew
Hampshire, UK
 
Agreed that it is more clear, but with an increasing number of forms my solution is more 'flexible' :). It's all about trade off.

Oh, and if you want to make the code a bit more clear and simple (readable), you can do this:

Code:
procedure TfMain.bFindImageClick(Sender: TObject);
var
citation : string;
CitationForm : TForm;

begin

  if eCitationNumber.Text = '' then
  begin
    ShowMessage('Please enter the citation number!');
    Exit;
  end;

    if cbCitationType.ItemIndex < 0 then
  begin
    ShowMessage('Please select the citation type!');
    Exit;
  end
  else
    citation := cbCitationType.Text;


  // show the associated form, linked through the citation combo box
  CitationForm := TForm(cbCitationType.Items.Objects[cbCitationType.ItemIndex]);
  if Assigned(CitationForm) then
   CitationForm.Show
  else
   ShowMessages('Error showing Citation form');

end;

you can find the most flexible solution (and not the most simple) solution in the link posted above.

Anyway, Leslie, I think Andrew is right, test it like this:

Code:
 if not Assigned(fMisdemeanor) then ShowMessage('KABOOM');
  cbCitationType.AddItem('Misdemeanor' , fMisdemeanor);
  cbCitationType.AddItem('Traffic' , fTraffic);
  cbCitationType.AddItem('TRACS' , fTRACS);

if you get KABOOM your forms are not created...

I tend to create my forms when they are needed, so I'm always sure they are created the moment I manipulate them.

/Daddy




-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
that was absolutely the problem! I moved them to the FormShow event and it works perfectly! I'll tweak it some to get to not run every time the form shows....

awesome way to do this! thanks for a new trick up my sleeve!

leslie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top