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!

Dynamically created TWebBRowser Event Handler 1

Status
Not open for further replies.

richardlev

Programmer
Aug 15, 2009
8
US
Hi Guys,

I am writing a multitab web browser using BCB TWbBrowser,

So far I can create TWebBrowser objects dinamically for each tabsheet ( see snippet below):

//===========
pTabSheet = new TLMDTabSheet(MainWindow->PageControl);
pTabSheet->PageControl = MainWindow->PageControl;

TabWebBrowser = new TWebBrowser(pTabSheet);
pTabSheet->InsertControl(TabWebBrowser);
TWinControl(TabWebBrowser).Parent = pTabSheet;
TabWebBrowser->Align = alClient;
TabWebBrowser->Visible = true;
TabWebBrowser->Navigate("
//===========

As you can see I am using a TPageControl and TTabSheet to host the TWebBrowser controls created at runtime.

My problem is How I set OnDocumentComplete event handlers for the TWebBrowser's created dynamically.

Thanks in advanced.

Richard
 
I think I should first create a new component derived from TWebBrowser, where the events are overwritten and than make instances of the new created component.
 
Or maybe something like

In the header file:

private: // User declarations
void __fastcall DoThis(TObject *Sender);

In the cpp file

//===========
pTabSheet = new TLMDTabSheet(MainWindow->PageControl);
pTabSheet->PageControl = MainWindow->PageControl;

TabWebBrowser = new TWebBrowser(pTabSheet);
pTabSheet->InsertControl(TabWebBrowser);
TWinControl(TabWebBrowser).Parent = pTabSheet;
TabWebBrowser->Align = alClient;
TabWebBrowser->Visible = true;
TabWebBrowser->Navigate("//add this line
TabWebBrowser->OnDownloadComplete = DoThis;
//===========

void __fastcall TForm1::DoThis(TObject *Sender)
{
//your code, but it will run for each opened Browser
....
}
 
Thanks luckieluc,

I think I will apply the second method...

Your help is appreciated.

Regards

Richard
 
I am having another problem .....maybe you can help me luckieluc.

After I created dinamically TWebBrowser and assign them to the tabsheets.
When I click on each tabsheet I can see each page have the respective webpage which I create with....But How I can get access to a specific TWebbroser to make some change like for example navigate to another page?

There are no indexes....How I can make some changes on a specific TWebBrowser control?

Foe example : TabWebBrowser->Navigate(someURL); for the second TWebbrowser I created?


Best Regards

Richard


 
suppose a button ButtonGo
and an Edit Textbox EditURL

void __fastcall TForm1::ButtonGoClick(TObject *Sender)
{
TWebBrowser *web;
int iControlIndex = 0;

while ((iControlIndex < MainWindow->PageControl->ActivePage->ControlCount)||(!web)) {
web = dynamic_cast<TWebBrowser *> (PageControl1->ActivePage->Controls[iControlIndex++]);
}
if(web){
web->Navigate(WideString(EditURL->Text));
}
}

My Pleasure

Luckieluc

 
If you are sure of the index of TWebBrowser control, you do not need a Dynamic_cast.

Suppose the index of the WebBrowser control would on each TabSheet 1.

You can write:

TWebBrowser *browser;

browser = MainWindow->PageControl->ActivePage->Controls[1];
browser->Navigate(WideString("...");
 
Thanks for all your responses luckieluc.

Now I am facing another problem:

As you know in the app i create dinamically tabsheet and embed a TWebBrowser for each od these tabsheet.
When OnDocumentComplete events fire...(see code snippet):

void __fastcall TMainWindow::pTabWebBrowserDocumentComplete(TObject *Sender, const _di_IDispatch pDisp,OleVariant &URL)
{
// here I want to identify which of my TWebBrowser control fired the event...
// I will use this to update EditURL and some other stuff
}

How I can do this?


I found online that some programmer use the Tag property of each TWebbrowser and they put an ID number like : 0,1,2,3,and so on.

then they do something like :

void __fastcall TMainWindow::pTabWebBrowserDocumentComplete(TObject *Sender, const _di_IDispatch pDisp,OleVariant &URL)
{
TWebBrowser *TempBrowser = dynamic_cast <TWebBrowser*>(Sender);
int ID=TempBrowser->Tag; //Getting ID number from the TWebBrowser
// who fired this event.
EditURL->Text = TempBrowser->LocationURL;

}

I think approach is fine and work for a simple(not multitab) browser......
I rather not to use this approach...because if the user keeps deleting tabs while browsing.....you have to update the TWebbrowser tag......that leave room for some bugs.

Is there another better way to use identify the control to fired the events ?

Thanks in advance

Richard

 
I'm sorry for the mistake I wrote.
You still need a Dynamic_cast without the while( ) loop.

In doThis(TObject *Sender) Sender is a pointer to TWebBrowser. TObject is a Base class of all controls. You may cast an Base class to the class of a child, if the Base class is an instance of that child class. (I hope I did not wrote some errors) but it means that in doThis(TObject *Sender) you can cast Sender to TWebBrowser.
So, you can use doThis(TObject *Sender) to fill in TEditURL with the URL property of the pointer to TWebBrowser as in:

void __fastcall TForm2::DoThis(TObject *ASender)
{
TWebBrowser* web;
web = dynamic_cast<TWebBrowser*>(ASender);
EditURL->Text = web->LocationURL;
}

Personally I find the idea of using the tag to identify pWebBrowser a good idea. But I should use the property TabIndex of the TabSheet as ID that contains the TWebBrowser. Be aware of the difference between the properties TTabIndex and TPageIndex....

If you have an pointer to TWebBrowser you always know the parent component grace the property as in:

tabsheet *TTabSheet;
tabsheet = dynamic_cast<TTabSheet *>(web->GetParentComponent());

And when you have a pointer to TTabSheet you always know the pointer to a TWebBrowser.

So I believe it must be possible to get the right pointer to TWebBrowser and do what you want to do.

 
After Navigate(..) or Navigate2(...) use (web is a pointer to TWebBrowse or TCppWebBrowse):

while (web->Busy) Application->ProcessMessage();

A webpage can possess multiple documents (for instance a page with frames)...thus otherwise multiple onDocumentComplete events will occur.
 
I will try to apply your suggestions tomorrow.

I did not have a clue that dealing with the TWebBrowser is such as pain in the butt.

I still have to deal with the javascript error popup messages,the ok key that sometime doesnt work on some webpages,etc etc.

Well one problem at the time.

Also I have been thinking in using a vector of objects to deal with my dynamic array of Twebbrowser......i will explore this later.

I will tell you my progress.

thanks luckieluc

Richard
 
Luckieluc when you say :

"And when you have a pointer to TTabSheet you always know the pointer to a TWebBrowser."

Can you explain that in some code example please?

Richard


 
It is my understanding that vector can not be used to store controls like TWebBrowser.
TWebBrowser is a control and need the new keyword, however I could be wrong. I should explore to be sure. But it is not necessary, because the pagecontrol with their tab is as the same and much easier. Use pagecontrol with his indexed tabsheet as an array that keep track (tabsheet,tabindex and webbrowser).

The code is already given:
suppose you have a pointer to TWebBrowser, the pointer to the corresponding tahsheet will be:

p_tabsheet *TTabSheet;
p_tabsheet = dynamic_cast<TTabSheet *>(p_web->GetParentComponent());

and suppose you have a pointer to tabsheet, the pointer to the corresponding TWebBrowser will be:

p_browser *TWebBrowser;
p_browser = dynamic_cast<TWebBrowser *>(p_tabsheet->Controls); // I is the control index of TWebBrowser in tabsheet.

Because it is inserted by an function together with tabsheet this number will probably always the same, but I cann't be sure without the code, otherwise you will need the while loop.

I must confess I start to doubt.

 
I tried this code:

TWebBrowser *p_browser;

p_browser = dynamic_cast<TWebBrowser *>(p_tabsheet->Controls); // I is the control index of TWebBrowser in tabsheet.

But I could not make it work.

I still cant find the pointer to the TWebBrowser.

Richard


 
I think the best tip I can give you is exercise some drag and drop tutorials. Because the same technique are used.
 
Finally I got it work.....I am able to find the right TWebBrowser control using your code on your 5th post.

Thanks Luckieluc.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top