Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    Originally Posted by ninadgac
    Form 2 is not in the Auto Create list , it is in the list of available forms.


    Code:
    EditForm := TForm2.CreateWithParameters(<your LabelPub Value>, <Your IDPub Value>);
      EditForm.Show;
    At this segment what the value should be passed for TComponent parameter? As I am getting error 'Incompatible types TComponent and String'
    Sorry, missed one parameter. It's expecing three parameters (owner, label, ID). didn't pass owner.

    Code:
    EditForm := TForm2.CreateWithParameters(Application, <your LabelPub Value>, <Your IDPub Value>);
      EditForm.Show;
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    42
    Rep Power
    1
    Thanks for your great and detailed help ...finally sorted out things to some extent with the following code at Form1's edit button click

    Code:
       
          Form2 := TForm2.Create(self);
          try
            Form2.ShowModal;
            Form2 := TForm2.CreateWithParameters(labelpub,idpub);
          finally
            Form2.Free;
          end;
    I removed the TComponent parameter from CreateWithParameters function and done with above code it is opening & closing Form2 smoothly without any type of access violation errors. It is also passing the values from Form1 successfully (I checked with showmessage function)..

    Only problem is that how to display these values on the form. As I am getting problem to display these values on the Form at runtime. I used Form2.refresh function but it is not updating it with new values.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    Actually, as coded you are leaking memory

    Code:
       
          Form2 := TForm2.Create(self);
          try
            Form2.ShowModal;
            Form2 := TForm2.CreateWithParameters(labelpub,idpub);
          finally
            Form2.Free;
          end;
    the first line Form2 := TForm2.Create(self); creates the form. Then you show that form , then you overwrite that form with the next create Form2 := TForm2.CreateWithParameters(labelpub,idpub); losing the memory you allocated with the first call.

    Please change it like so
    Code:
       
          Form2 := TForm2.CreateWithParameters(Application,labelpub,idpub);
       //I assume you didn't recode the new constructor to not use the AOwner parameter...
          try
            Form2.ShowModal;
            //since you are doing a showmodal vs. a show make sure
            //Form2 returns a ModalResult.  
            Case From2.ModalResult of
               mrYes: //do something
               mrCancel:  //do something
               mrOK: //do something
               ....
          finally
            Form2.Free;
          end;
    To have your form return a modal result, place a button on form2, in the object inspector select a value (other than mrNone) for the property ModalResult.

    Also, since you are freeing the form in the code above (form2.Free), make sure you don't free it in Form2's OnClose Event. i.e. don't do "Action := caFree;" in Form2's FormClose method.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    42
    Rep Power
    1
    Code:
    Form2 := TForm2.CreateWithParameters(Application,labelpub,idpub);
       //I assume you didn't recode the new constructor to not use the AOwner parameter...
          try
            Form2.ShowModal;
            //since you are doing a showmodal vs. a show make sure
            //Form2 returns a ModalResult.  
            Case From2.ModalResult of
               mrYes: //do something
               mrCancel:  //do something
               mrOK: //do something
               ....
          finally
            Form2.Free;
          end;
    This gives error "cannot make a visible window modal."
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    How and where is Form2 declared? It should be declared in Form1 either as part of Form1's type declaration, or the better option is to have it declared locally in the procedure you use it in.
    Code:
    var
       Form2: TForm2;
    begin
       Form2 := TForm2.CreateWithParameters(Application,labelpub,idpub);
       try
          Form2.ShowModal;
         Case From2.ModalResult of
            mrYes: //do something
            mrCancel:  //do something
            mrOK: //do something
            ....
         end;
       finally
          Form2.Free;
       end;
    end;
    Also, in Form2 itself, do you still have the global declaration
    Code:
    ...
    var
       Form2: TForm2;
    
    implementation
    
    ...
    if so, delete it....

    Also, recheck and make sure that Form2 is not in the autocreate list.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    42
    Rep Power
    1
    It worked out after some tweaks. However I want to close the form2 (edit form) automatically after user enter value and clicks on SAVE button which would perform update first and then expected to close form.

    Is it possible ?
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    344
    Rep Power
    6
    Originally Posted by ninadgac
    It worked out after some tweaks. However I want to close the form2 (edit form) automatically after user enter value and clicks on SAVE button which would perform update first and then expected to close form.

    Is it possible ?
    Since you did a SHOWMODAL then what you want to do is set the save button's ModalResult property to some value such as mrOK, or mrYes. Put another button so your use can cancel, and set the modalresult to mrCancel.

    then where you show the form check the form's modal result
    Code:
    form2 := ....Create(....)
    try
      Form2.ShowModal;
      if Form2.ModalResult = mrOK then 
         //assumes you set save button's modalresult propert to mrOK
      begin
        //save your data here
      end;
    finally
       Form2.Free;
    end;
    You can define a PUBLIC method (function) that returns the value you need, so in Form2 add the new function

    Code:
    TForm2 = class(TForm)
     ...
    PUBLIC
      function GetNewValueEntered: Integer;//assumes new value is an integer;
    end;
    
    ....
    
    function TForm2.GetNewValueEntered: Integer;
    begin
      Result := <the new value>;
    end;
    where you see
    //save your data here
    in the code above, you will need to Get the new values using the new method in form2.

    Code:
    var
       NewValue: Integer;
    begin
      .....
      NewValue := Form2.GetNewValueEntered;
      //save your data here
    end;
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo