c# web browser and statustext to show hyperlink - c#

So I have a custom windows form with a tabbed browser. I want to be able to see the hoovered hyperlink in the status bar. I have searched everywhere but nothing yet.
I checked the MSDN under the web browser class which led me to the StatusTextChanged.
So in my code I have
wb.StatusTextChanged += MainWindow_StatusChange;
then
private void MainWindow_StatusChange(Object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
this.toolStripStatusLabel2.Text = wb.StatusText;
}
Well, the tool.StripStatusLabel2.Text is empty.
Any help?

You have subscribed the wrong event.
Do this...
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
wb.Navigate("http://www.google.de");
}
void wb_StatusTextChanged(object sender, EventArgs e)
{
this.toolStripStatusLabel2.Text = ((WebBrowser) sender).StatusText;
}

Related

How to Fire WebBrowserDocumentCompleted event from default Web Browser

I tried two method:
wb.Navigate(Url,true); When i use this webbrowser appear but WebBrowserDocumentCompleted event doesn't fire after page loaded.
wb.Navigate(Url); When i use this WebBrowserDocumentCompleted event is firing but browser doesn't appear on screen. How can i display external browser and fire WebBrowserDocumentCompleted event same time?
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.DocumentCompleted += WebBrowserDocumentCompleted;
wb.Visible = true;
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate("http://www.google.com",true);
}
private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if ((sender as WebBrowser).ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete)
{
// Do what ever you want to do here when page is completely loaded.
}
}

How to implement new tab in CefSharp with correct address and title change?

I am using CefSharp to create a browser. It is working, I can navigate to various websites by using new tab. But when I click on previous tabs, all the tab shows same URL in the address bar and all of them has exactly same title. Here is my code:
private void FormBrowser_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
browser.Parent = tabControl.SelectedTab;
browser.Dock = DockStyle.Fill;
browser.AddressChanged += Browser_AddressChanged;
browser.TitleChanged += Browser_TitleChanged;
}
// new tab function
public void addNewTab()
{
TabPage tpage = new TabPage();
tpage.Text = "New Tab";
tabControl.Controls.Add(tpage);
tabControl.SelectTab(tabControl.TabCount - 1);
toolStripTextBoxAddress.Text = "";
ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
browser.Parent = tpage;
browser.Dock = DockStyle.Fill;
browser.AddressChanged += Browser_AddressChanged;
browser.TitleChanged += Browser_TitleChanged;
}
private void Browser_TitleChanged(object sender, TitleChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
tabControl.SelectedTab.Text = e.Title;
}));
}
private void Browser_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
toolStripTextBoxAddress.Text = e.Address;
}));
}
// navigate method
private void toolStripTextBoxAddress_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (!string.IsNullOrEmpty(toolStripTextBoxAddress.Text))
{
if (!toolStripTextBoxAddress.Text.Contains("."))
{
getCurrentBrowser().Load("http://www.google.com/search?q=" + toolStripTextBoxAddress.Text);
}
else
{
getCurrentBrowser().Load(toolStripTextBoxAddress.Text);
}
}
}
}
// get current browser
private ChromiumWebBrowser getCurrentBrowser()
{
return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
}
// new tab button
private void toolStripButtonNewTab_Click(object sender, EventArgs e)
{
addNewTab();
}
Here is what I have tried:
private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
{
ChromiumWebBrowser currentBrowser = getCurrentBrowser();
toolStripTextBoxAddress.Text = currentBrowser.Address;
}
When i try to open a new tab it is giving me an error in this line return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
How can I solve this problem? Thanks in advance.
I wrote my multi-tab cefsharp code in a very similar fashion to yours, and encountered the same error.
It was cause by the default number of tab pages. (When you drag tabcontrol to your form, it by default comes with 2 tabpages to start with). From the property panel, I removed those two tabpages, so that the browser starts wit zero tabpage. Any tabpage is only added when you start browsing, by inputting url or clicking favorites.
If you do not set the initial number of tabpages to zero, those two "empty" tabpages have no browser attached to them. Therefore the getcurrentbrowser() function fails to find any browser on those empty tabpages and errors occur.

Invoking google search button

My application has one button one text box and a webbrowser control which is going to navigate at google.com at form load event.
If I am setting the text attributes to the google textbox in webBrowser1_DocumentCompleted method after clicking the button I am able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "facebook");
}
private void btnLogin_Click(object sender, EventArgs e)
{
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
If I am setting the text attributes to the google textbox in btnLogin_Click method after clicking the button i am not able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
}
private void btnLogin_Click(object sender, EventArgs e)
{
textElement.SetAttribute("value", "facebook");
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
Where I am going wrong please suggest me.

Why the external IE is openings from .NET webbrowser

I have this form which has a System.Windows.Forms.WebBrowser controller in it. One of the html pages has some href in it like this
<a href="https://twitter.com/xxx" target="_blank">
<h3 style="margin-top:15%; text-align:center">xxx</h3>
</a>
I am catching the clicks in the
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
and opening the page as so
webBrowser1.Navigate(page);
Now this works and the page is shown inside the webBrowser1, but this also opens the external IE with the same page. Actually, the external IE opens first before the webBrowser1 shows the page.
So my question is: Why does the external IE opens and how can I stop this.
cheers,
es
Instead of opening a new page try to create custom onClick event
private bool bCancel = false;
private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
int i;
for (i = 0; i < webBrowser.Document.Links.Count; i++)
{
webBrowser.Document.Links[i].Click += new
HtmlElementEventHandler(this.LinkClick);
}
}
private void LinkClick(object sender, System.EventArgs e)
{
bCancel = true;
MessageBox.Show("Link Was Clicked Navigation was Cancelled");
}
private void webBrowser_Navingating(object sender,
WebBrowserNavigatingEventArgs e )
{
if (bCancel == true)
{
e.Cancel=true;
bCancel = false;
}
}

The name "webBrowser" does not exist in the current context

I have this code:
public void button1_Click(object sender, EventArgs e)
{
for(int i = 0; i < numericUpDown1.Value; i++)
{
WebBrowser webBrowser = new WebBrowser();
webBrowser.Visible = false;
webBrowser.ScriptErrorsSuppressed = true;
webBrowser.Navigate("want to keep it private.");
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
}
}
public void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement botNick = webBrowser.Document.GetElementById("nick");
botNick.SetAttribute("value", textBox1.Text);
HtmlElement botPlay = webBrowser.Document.GetElementsByTagName("button")[2];
botPlay.InvokeMember("click");
}
Why am i getting "The name "webBrowser" does not exist in the current context"?
Firstly, "The name "webBrowser" does not appear in the current context" error means that you propably have not defined such a variable/field/property/etc. in the current context.
Secondly, since you have not clarified the line of the error, I will assume that it appeared on these 2 lines:
HtmlElement botNick = webBrowser.Document.GetElementById("nick");
HtmlElement botPlay = webBrowser.Document.GetElementsByTagName("button")[2];
This happens because, while you have declared webBrowser here:
WebBrowser webBrowser = new WebBrowser();
which exists in function void button1_Click(object sender, EventArgs e)
and therefore is invisible to the function void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e).
To rectify this situation you can either pass the webBrowser as an argument, or define a new var with the name of webBrowser in the latter function.

Categories

Resources