Detect the bottom of a WebBrowser Page in Windows Phone 8 - c#

I have a xaml page in which I have used a webbrowser control.I need to scroll down to the web page and once the bottom is reached,I have to enable a button.
Thanks in advance!

There's two part to this. The first is to detect, in Javascript, the moment when the browser reaches the bottom of the page. The second is to forward the event to the C# code.
Say you ask your WebBrowser control to navigate to a given page. First, in the C# code, subscribe to the Navigated event to inject the proper Javascript code:
private void WebBrowser_Navigated(object sender, NavigationEventArgs e)
{
const string Script = #"window.onscroll = function() {
if ((window.innerHeight + document.documentElement.scrollTop) >= document.body.offsetHeight) {
window.external.notify('bottom');
}
};";
this.WebBrowser.InvokeScript("eval", Script);
}
This Javascript code uses the window.external.notify method to notify the C# code. To receive the notification, you need to subscribe to the ScriptNotify event of the WebBrowser:
private void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == "bottom")
{
// Reached the bottom of the page
}
}

Related

Waiting for webBrowser control to load before filling textbox and clicking

I'm using Visual Studio Community in C# (.Net 4.5).
I have a simple form, with one button and one webBrowser control.
I want to check if "tremblay jean" has a trademark registered in his name in Canada (I know he has two).
So when I click my button I load the trademarks search page in my webBrowser control, I wait for it to be complete, then I insert his name in their textbox and click their button.
If I pause the program using a MessageBox.Show after loading the page, it works, there's two documents found.
But if I don't pause the program using a MessageBox it doesn't work. It gives me 500 results, unrelated to "tremblay jean".
So the line of code waiting for the ReadyState to be Complete doen't seem to work.
Does anyone know why?
private void button1_Click(object sender, EventArgs e)
{
string website = "http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/home?lang=eng";
webBrowser1.Navigate(website);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
MessageBox.Show(webBrowser1.ReadyState.ToString()); // to pause the program
webBrowser1.Document.GetElementById("search-crit-1").SetAttribute("value", "tremblay jean");
HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement el in elc)
{
if (el.GetAttribute("type").Equals("submit"))
{
if (el.InnerText == " Search ")
{
el.InvokeMember("Click"); //comment this line to see if textbox is filled
break;
}
}
}
}
The first thing to do, when you're using a WebBrowser control, is to initialize it with this html string:
<meta http-equiv='x-ua-compatible' content='IE=edge,chrome=1'>
This allows to set the compatibility mode of the control's underlying activex (Internet Explorer) to the most recent locally available version.
With webBrowser1.ScriptErrorsSuppressed = true;, the scripting error popup is disabled.It's a just in case measure.
Then, subscribe the DocumentCompleted event, that will raise when the page has been loaded. As already noted in the comments, this event might be raised more than once, because of the interaction of Scripting and IFrame.
The WebBrowser.ReadyState is used to verify that page is indeed completed.
It's true that, sometimes, the inner scripting can cause some trouble
here, but since this is not the case, I'll leave it as a side
note.
One other thing you'll notice is that the DocumentCompleted event is unsubscribed after the WebForm button is clicked. This is done to avoid further notifications from the WebBrowser, as the needed action is already been performed and no further action is required on other pages.
So, the event will be active only when you'll be requesting new results to the server (e.g. clicking your UI Search button).
Here, the private string SearchName; is a placeholder for a procedure the defines the new search criteria.
private string SearchName;
private void button1_Click(object sender, EventArgs e)
{
SearchName = "tremblay jean";
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("");
webBrowser1.Document.Write("<!DOCTYPE html><head><meta http-equiv='x-ua-compatible' content='IE=edge,chrome=1'></head>");
webBrowser1.Navigate("http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/home?lang=eng");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.WBDocCompleted);
}
protected void WBDocCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = ((WebBrowser)sender);
if (browser.ReadyState == WebBrowserReadyState.Complete)
{
if (browser.Document != null)
{
browser.Document.GetElementById("search-crit-1").SetAttribute("value", this.SearchName);
foreach (HtmlElement button in browser.Document.GetElementsByTagName("button"))
{
if (button.GetAttribute("type") == "submit" && button.Name == "")
{
browser.DocumentCompleted -= this.WBDocCompleted;
button.InvokeMember("click");
break;
}
}
}
}
}

Mouse event with RadPageView

I have a problem with RadPageView on catch mouse event when I'm building an app using Telerik UI for WinForms.
I just want to catch mouseover event of some page in a page view. In this case, I want to show the content while a page header is under pointer.
For example, I have a page view named "Page" and it has 2 pages are "A" and "B". I want to show these page's content when the pointer point to them.
Please give me a solutions for this case.
Thanks,
Sorry for my bad English.
Here you are:
private void RadPageView1_MouseMove(object sender, MouseEventArgs e)
{
RadPageViewItem hoveredItem = radPageView1.ElementTree.GetElementAtPoint(e.Location) as RadPageViewItem;
if (hoveredItem != null)
{
radPageView1.SelectedPage = hoveredItem.Page;
}
}
For me, e.location was not behaving properly in some corner cases (multiple pages were getting hovered if we moved cursor in a certain way).
I used below logic (use of isMouseOver property of each item) and it works perfectly:
private void RadPageView1_MouseMove(object sender, MouseEventArgs e) {
foreach (RadPageViewPage pageView in radPageView1.Pages) {
if (!pageView.Item.IsMouseOver) {
//your logic for hovered pages
} else {
//your logic (if any) for non-hovered pages
}
}
}

Is there some way to go on the previous page get the pressed button text?

I'm working on Visual Studio, in C# language
I would like to ask if there's someway to go back to get the ButtonX.Text I pressed on the previous page, some sort of Login without a password.
Basically I need a worker to specify which person he is by clicking on their name (button) then it goes foward to the next page I have a label on the MasterPage but it resets everytime it goes on a next page what I would like to do is keep the info there
If you need some code tell me.
Thanks
You could use session variables?
On the button click handler on the first page...
protected void button_Click(object sender, EventArgs e)
{
Session["Worker"] = button.Text;
}
Then on the second page...
Label.Text = Session["Worker"];
Based-off your reply to Zollistic's answer, you could do this...
Apply this event to all your all your worker buttons...
protected void button_Click(object sender, EventArgs e)
{
if (Session["Worker"] == null) Session["Worker"] = "";
Session["Worker"] += button.Text + ",";
}
Now Session["Worker"] has a character-delimited list of all the clicked buttons. The character in this example is a comma; but you can change it to whatever you want (i.e. a pipe "|").

Windows Phone Show Progress Indicator Each Link Click in Webbrowser Component

I'm on small Windows phone project.
In this project, I use a WebBrowser component (like WebView in Android) to show the mobile website of my company as an application.
I need to show a progress bar/dialog/indicator whatever you say, after every link click.
How can I handle that? For example, I will click the news link and something will be shown to the user like loading etc.
UPDATE :
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Browser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Browser_Navigated);
Browser.Navigating += new EventHandler<NavigatingEventArgs>(Browser_Navigating);
Browser.ScriptNotify += new EventHandler<NotifyEventArgs>(Browser_ScriptNotify);
}
void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
Browser.Navigate(new Uri(e.Value, UriKind.Absolute));
}
void Browser_Navigating(object sender, NavigatingEventArgs e)
{
ProgBar.Visibility = Visibility.Visible;
}
void Browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
ProgBar.Visibility = Visibility.Collapsed;
}
}
i wrote this code but it doesnt show anything about progress.
You should use intercept the Navigating event of the WebBrowser control to start your progress bar.
You can just change the Visibility property of a loading overlay, say consisting of a semi-transparent Rectangle and a ProgressBar.
Then you should intercept the LoadComplete or NavigationFailed events and use these to remove your overlay.
Beware that some URLs (such as tel: links) don't fire the Navigated event, so you may need special handling for these.

How to detect document changes when using the MSHTML WebBrowser in edit mode using C# in Windows Forms?

I'm using the .NET WebBrowser control in a WinForms application to implement a very basic e-mail template editor.
I've set it in edit mode by this code:
wbEmailText.Navigate( "about:blank" );
( (HTMLDocument)wbEmailText.Document.DomDocument ).designMode = "On";
So the user can modify the WebBrowser content.
Now I need to detect when the user modifies the content 'cause I have to validate it.
I've tried to use some WebBrowser's events like DocumentCompleted, Navigated, etc. but no-one of these worked.
Could someone give me advice, please?
Thanks in advance!
I did have some working, really world code but that project is about 5 years old and I've since left the company. I've trawled my back-ups but can't find it so I am trying to work from memory and give you some pointers.
There are lots of events you can catch and then hook into to find out whether a change has been made. A list of the events can be found here: http://msdn.microsoft.com/en-us/library/ms535862(v=vs.85).aspx
What we did was catch key events (they're typing) and click events (they've moved focus or dragged / dropped etc) and handled that.
Some example code, note a few bits are pseudo code because I couldn't remember off the top of my head the actual code.
// Pseudo code
private string _content = string.empty;
private void frmMain_Load(object sender, EventArgs e)
{
// This tells the browser that any javascript requests that call "window.external..." to use this form, useful if you want to hook up events so the browser can notify us of things via JavaScript
webBrowser1.ObjectForScripting = this;
webBrowser1.Url = new Uri("yourUrlHere");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Store original content
_content = webBrowser1.Content; // Pseudo code
webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
webBrowser1.Document.PreviewKeyDown +=new PreviewKeyDownEventHandler(Document_PreviewKeyDown);
}
protected void Document_Click(object sender, EventArgs e)
{
DocumentChanged();
}
protected void Document_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
DocumentChanged();
}
private void DocumentChanged()
{
// Compare old content with new content
if (_content == webBrowser1.Content) // Pseudo code
{
// No changes made...
return;
}
// Add code to handle the change
// ...
// Store current content so can compare on next event etc
_content = webBrowser1.Content; // Pseudo code
}

Categories