Jaume Posted July 1, 2013 Share Posted July 1, 2013 Hi, in VCL I usually had this kind of function in units that allow me to had one block of code for opening showmodal forms that are used for select one row in a grid and return an ID. This block of code can be used for a lot of forms, so it's not repeted. For example: function choose_city() : integer; var f_Cities: TCities; begin result:=0; f_Cities := TCities.Create(nil); if f_Cities.init() then begin if f_Cities.ShowModal = mrok then begin result:= f_Cities.return_city_id; end; end; f_Cities.free; end; In WEB mode this can't be done, because showmodal is not blocking. So do i have to repete this code in every form for every callback? Is there any better sugestion to help to reproduce the same behaviour in WEB mode with common code? Thank you very much for your help. Quote Link to comment Share on other sites More sharing options...
Jancarlos Martins Posted July 1, 2013 Share Posted July 1, 2013 Hi, Go Unigui Demos, Form Anonymous Callback, Form Callback, etc, is very good samples. PerjanBR Quote Link to comment Share on other sites More sharing options...
Jaume Posted July 1, 2013 Author Share Posted July 1, 2013 Thank you. I've seen the demos before of course, but that is not what i asked for. If I translate this: function choose_city() : integer; var f_Cities: TCities; begin result:=0; f_Cities := TCities.Create(nil); if f_Cities.init() then begin if f_Cities.ShowModal = mrok then begin result:= f_Cities.return_city_id; end; end; f_Cities.free; end; for function choose_city() : integer; var f_Cities: TCities; begin result:=0; f_Cities := TCities.Create(nil); if f_Cities.init() then begin if f_Cities.ShowModal(procedure(Result:Integer) begin if Result = mrok then result:= f_Cities.return_city_id; end); end; end; this does not compile because in anonymus callbacks can't use the result of the funcion. So this makes the code not useful for a common purpose. So every time that i need to call choose_city i must re-code it again, but with the specific anonymous method. Also, if i do it without anonymous method, and passing the callback as a parameter to choose_city(), i can't use the f_Cities variable from the callback procedure. So i asked if there is any idea or stardard method to code this type of VCL style functions with WEB mode, as i'm not used to this. Thank you very much. Quote Link to comment Share on other sites More sharing options...
Jancarlos Martins Posted July 1, 2013 Share Posted July 1, 2013 Jaume, In form form TCities create callback, example: procedure TCities.returnKeyCities(Sender: TObject); begin ModalResult:=<your key return>; end; function Form.choose_city() : integer; var f_Cities: TCities; returnKey:integer;begin f_Cities := TCities.Create(nil); f_Cities.init(); f_Cities.ShowModal( procedure(result: Integer) begin returnKey:=result; //result already contains the value f_Cities.return_city_id; end); PerjanBR Quote Link to comment Share on other sites More sharing options...
Jaume Posted July 1, 2013 Author Share Posted July 1, 2013 Thanks for the answer. Anyway, as i see I will have to re-code the funcion to every form. My aim was to code this: function Form.choose_city() : integer; var f_Cities: TCities; returnKey:integer;begin f_Cities := TCities.Create(nil); f_Cities.init(); f_Cities.ShowModal( procedure(result: Integer) begin returnKey:=result; //result already contains the value f_Cities.return_city_id; end); into a Unit and use it from multiple forms. But as I imagine, i can't do it with callbacks, so i will have to re-code the same pice of code in every form. If anyone has more ideas, please let me know. Thank you. Quote Link to comment Share on other sites More sharing options...
Jancarlos Martins Posted July 1, 2013 Share Posted July 1, 2013 Hi, Also was my problem, I used inheritance. One form for all searches Quote Link to comment Share on other sites More sharing options...
Oliver Morsch Posted July 1, 2013 Share Posted July 1, 2013 If anyone has more ideas, please let me know. Instead of using a (global) function "choose_city(): integer" which returns an integer, I would use a (global) procedure "choose_city(DoAfterChooseCity: TDoAfterChooseCityProc)" which gets an anonymous procedure that contains the code to do after the city was choosen. And then call: choose_city(procedure(CityCode: Integer) begin //do this after the user has choosen a city... end); 1 Quote Link to comment Share on other sites More sharing options...
Jaume Posted July 1, 2013 Author Share Posted July 1, 2013 Thank you. That's a nice idea. I think this should work better for me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.