Passing a variable within a procedure

4

I have this procedure in my project

procedure DocumentComplete(ASender: TObject; const pDisp: IDispatch; const URL: OleVariant);

Now I need to use it in a loop by passing the count variable. I tried the following:

procedure DocumentComplete(ASender: TObject; const pDisp: IDispatch; const URL: OleVariant; var contagem : Integer);

But it happens that when I pass the variable, the procedure also requests the other parameters Sender: TObject; const pDisp: IDispatch; const URL: OleVariant , which between us, I have no idea what they are.

I program by hobby and I'm doing a little project for a friend, please help me. I can not know what to do, I'm sure it's something simple but I searched all day and did not find the solution. What happens in place of these parameters?.

Edit: This is the part where I use the loop to call the procedure, every time it is called it is a different browser used.

procedure TForm1.Button1Click(Sender: TObject);
  begin
  for contagem := 1 to StrtoInt(Edit1.Text) do
  begin
browsers[contagem].OnDocumentComplete := nil;

NavigationOK := true;

browsers[contagem].OnDocumentComplete := DocCompleteA;
browsers[contagem].Navigate
  ('http://****/index.php?id=entrar');
end;
end;

Creating the browsers:

procedure TForm1.Button2Click(Sender: TObject);
begin
  for contagem := 1 to StrToInt(Edit1.Text) do
  begin
    campos[contagem] := TEdit.Create(self);
    campos[contagem].Parent := Form1;
    campos[contagem].Show;
    campos[contagem].Left := 150;
    campos[contagem].Top := 350;
    campos[contagem].Width := 600;
    browsers[contagem] := TWebBrowser.Create(Form1);
    TWinControl(browsers[contagem]).Parent := Form1;
    browsers[contagem].Align := alTop;
    browsers[contagem].Height := 300;
    browsers[contagem].Show;
    browsers[contagem].Silent := true;
  end;
end;
    
asked by anonymous 08.11.2016 / 22:16

2 answers

0

Try this way

procedure TForm1.Button1Click(Sender: TObject);
var
  iCont,iContagem: Integer;      
begin
  iCont := StrToInt(Edit1.Text);
  for iContagem := 0 to iCont-1 do
  begin
    browsers[iContagem].OnDocumentComplete := nil;
    NavigationOK := true;
    browsers[iContagem].OnDocumentComplete := DocCompleteA;
    browsers[iContagem].Navigate
    ('http://****/index.php?id=entrar');
  end;
end;
    
08.11.2016 / 23:29
0

Dear, The Sender is the reference of the object that is executing the request to the method, in this case it is the TWebBrowser itself.

procedure TForm1.OnDocumentComplete(
  Sender: TObject;
  const pDisp: IDispatch;
  var URL: OleVariant);
var
  currentBrowser: IWebBrowser;
  topBrowser: IWebBrowser;
  document: OleVariant;
  windowName: string;
begin
  currentBrowser := pDisp as IWebBrowser;
  topBrowser := (Sender as TWebBrowser).DefaultInterface;
  if currentBrowser = topBrowser then
  begin
     ShowMessage('Browser que completou:' tag:'+IntToStr(TWebBrowser(Sender).tag));
  end
  else
  begin
    document := currentBrowser.Document;
    windowName := document.ParentWindow.Name;
    //ShowMessage(Format('Frame "%s" was loaded', [windowName]));
  end;
end;

Notice the TAG property when creating webs browsers:

procedure TForm1.BotaoCriaBrowsersClick(Sender: TObject);
begin
  for contagem := 0 to StrToInt(Edit1.Text)-1 do
  begin
    browsers[contagem] := TWebBrowser.Create(Form1);
    TWinControl(browsers[contagem]).Parent := Form1;
    browsers[contagem].Align := alTop;
    browsers[contagem].Height := 100;
    browsers[contagem].Tag := contagem;
    browsers[contagem].OnDocumentComplete:= Form1.OnDocumentComplete;
  end;
end;

Main lines for understanding the example:

browsers[contagem].Tag := contagem;
browsers[contagem].OnDocumentComplete:= Form1.OnDocumentComplete;

In this way I used the tag property to store the browser position in array .

Suggestion to see more about TWebBrowser:

link

    
12.11.2016 / 17:42