Passing a variable within a precedent

Asked

Viewed 645 times

5

I have this 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 it as follows:

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

But it turns out that when I pass the variable, Procedure also asks for the other parameters Sender: TObject; const pDisp: IDispatch; const URL: OleVariant that, between you and me, I have no idea what they are.

I program for a hobby and I’m doing a little project for a friend, please help me.
I can’t figure out what to do, I’m sure it’s something simple but I’ve been researching all day and I haven’t found a solution. What step in place of these parameters?.

Edit: This is the part where I use the loop to call the Procedure, each time it is called 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;

Creation of 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;
  • But is this a single-component event... if it’s a post here which one you’re using?.

  • @Yes, Ondocumentcomplete event of a Webbrowser, but this Webbrowser is created in Runtime, so I need the loop. The process will be executed once for each browser created

  • As you are handling this loop, you can post the source code in the question, so it would help the forum staff to better understand and help you. Hugs

  • @Jeffersonrudolf changed the question with the part where I call the loop with the file inside. If it’s not enough I can post the whole code. Sorry for the mess in formatting but I always mess around in this part rsrs

  • for each browser created, you treat a different way in the Doccompletaa function?... or it is the same for all browser created. If so, it is not necessary to pass a counting variable as parameter, for each loop it executes the event, got it... anything but talk

  • @Jeffersonrudolf Same task for each browser, the problem is that without passing the variable occurs an error of those complicated to know (rsrs), "Violation 0x00000" <- similar to this. Manually placing the number (browsers[1]...) makes the error stop, which made me assume that the error is being in the variable that is not being passed

  • There is a way to do exactly what you are asking, but I think there would be a less radical solution to your problem. To do what you are asking, simply create a class by inheriting from Twebbrowser, and overwrite the Property that receives the Ondocumentcomplete method by adding its new parameter. If you can’t solve the problem, we can try this solution.

Show 2 more comments

2 answers

1

Dear, Sender is the reference of the object making the request to the method, in this case it is 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;

Observe the property TAG in the creation of 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;

That way I used the property tag to store the browser’s position in array.

Suggestion to see more about Twebbrowser:

http://www.cryer.co.uk/brian/delphi/twebbrowser/twebbrowser_oleobject.htm

1

Try it like this

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;
  • Access Violation at address 005E9DFF in module... if I pass the value manually it works, which leads me to believe that the problem is to pass even the variable

  • As such creating the browsers in Runtime?

  • I have a button for this. "Client" wants to choose the both of Simultaneos browsers. The button simply creates the browsers.

  • You can post on the question, such as creating it in execution mode.

  • 1

    It’s already there, buddy

  • I’ll have to put together something similar to what you’re talking about, to get an idea.

  • I can put all the code in Pastebin if you want

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.