c# webbrowser get its url from text box - c#

Ok i am trying to make my webbrowser(webbrowser1) get a url that is read from a textbox (textbox1 ) so the user can pick where the web browser connects from
i have tried:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Url = textBox1.Text();
}
And:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Url = textBox1.Text;
}
and some outer methods but i can not seem to get it to do it is it possible to do this for a web browser ?
(i mean a form web browser in the project not Firefox, chrome and so on ... )

You're handling the DocumentCompleted, which fires when the document has completed loading.
That's probably not what you want; you probably want that code to run when the user wants it.

You shouldn't override DocumentCompleted method. What you want to do is have a button, or in your custom textBox1 override lost focus or one of the action methods. With button override the click.
I guess you could override text changed in textBox1 and detect enter key pressed or some other trigger.
But regardless, you don't want to override DocumentCompleted
webBrowser1.Navigate(new Uri(textBox1.text));
would help you navigate to the URL string.
Here are some real simple examples:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.url.aspx

Related

Execute buttons with one click

I have a simple C # application that contains buttons and a web browser, each button execute a request and displays the result of the request on the web browser. And I use the results of a request in the next button.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.test.com");
}
private void button2_Click(object sender, EventArgs e)
{
if (webBrowser1.Document.GetElementById("tesxtbox1") != null)
{
HtmlElement txt1 = webBrowser1.Document.GetElementById("tesxtbox1");
txt1.SetAttribute("value", "test");
webBrowser1.Document.Forms[0].InvokeMember("submit");
}
}
my question is to find method or way to perform the two buttons with a single click, but the second button, do not execute until the web browser is loaded.
in my first solution, I added in the first button :
webBrowser1.DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (Button2_Click);
but the second button excuted several times, so I added in the last line of the button 2:
webBrowser1.DocumentCompleted - = new WebBrowserDocumentCompletedEventHandler (Button2_Click);
it works, but in the console I find that Button 2 is execute twice
Thanks in advance!
You're approaching this issue the wrong way. First you are not looking for a way to click two buttons. You are looking for a way to click one button and execute an operation if a condition is met.
Basically you just need to call button2.PerformClick() in your WebBrowser DocumentCompleted method. If however you want to ensure that button1 was clicked, you can set a bool in button1 Click method and reset it in the button2 Click method.
If you need to perform more than one button click in your document complete method after the first button click, you can add them to your DocumentCompleted method like this:
First subscribe normally.
webBrowser1.DocumentCompleted += Document_Completed;
Normally you can generate the method stub by pressing TAB after subscribing to the event. The method could be as follows:
private void Document_Completed(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//here you add the other buttons that must be clicked when document complete
button1.PerformClick(); button2.PerformClick();//etc
//you could even iterate all over the buttons on the form and click each of them if it meets a certain condtition.
}
I hope that answers your question :)
DocumentCompleted is executed each time your WebBrowser control finishes downloading a document.
You only need to subscribe once.

How to get a button to open a web page?

I've looked everywhere, including the MSDN forums, but no one has even mentioned this or ways to do it. The basic idea is that once a Button is dragged from the toolkit, how do you then link that button to a web page, ie I have a 'Facebook' button, how do I then make it so that when the button is clicked, Facebook opens in a new browser window?
Once you've dragged the button onto the designer, you can double-click on it to open up the Button's Click event handler. This is the code that will get run when the user clicks. You can then add the required logic, ie:
private void button1_Click(object sender, EventArgs e)
{
// Launch browser to facebook...
System.Diagnostics.Process.Start("http://www.facebook.com");
}
You can use Process.Start with the desired URL to open the default browser and navigate to the page:
using system.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
Process.Start("http://www.YouTube.com");
}
Since you said open in a new browser window, I was thinking the context was an web application you were developing, so this would apply in that case:
Add an HTML button with the window.open JavaScript method. You don't need or want code-behind here because it's all happening on the client. Here's a snippet, and there are a few other options you can pass to window.open to control behavior.
<input id="Button2" type="button" value="Facebook" onclick="window.open('http://facebook.com')"/></p>
You have to use Process class under System.dll which is be default added to the solution. But you should refer the namespace at the top of your class this way:
using System;
namespace MyApplication
{
public class MyProgram
{
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.facebook.com");
}
}
}

How do I go to a webpage, load, then do some code?

I've tried using Sleep but that just froze the program. Is it messing up because it's in the timer? I can get it to go to the site and not do code, but once I put other code it doesn't load properly.
I also tried putting code in:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) but then I had even more problems.
private void timer1_Tick(object sender, EventArgs e)
{
//code here
if (click == null)
{
webBrowser1.Url = new System.Uri("http://url.com", System.UriKind.Absolute);
timer1.Stop();
//load or wait x seconds
timer1.Start();
}
EDIT:
I'm trying to go from one page to the next and doing code once the page is loaded. If I put code in webBrowser1_DocumentCompleted it messes up the code before it. How do I add DocumentCompleted to only this one instance?
First, it seems like ur approach is wrong,
Open the url in the web browser control.
Wait for DocumentCompletedEvent. It will come once page is loaded fully.
Write ur code in document completed event based on your criteria/condition.

C# Webbrowser document , invokemember("click"), is it possible to wait until that click action is resolved?

What I have going on is a Invokemember("Click"), the problem is I want to be able to grab the resulting innerhtml. The problem is i'm unsure of how/if it's possible to wait until the resulting action of the invokemember("click") is resolved. Meaning, in a javascript when you perform this click it will take you ot the next 20 items listed. However, i'm unsure of how to tell when that javascript will be fully loaded. Below is what I'm using.
private void button1_Click(object sender, EventArgs e)
{
HtmlElement button = webBrowser1.Document.GetElementById("ctl08_ctl00_InventoryListDisplayFieldRepeater2_ctl00_BlockViewPaging_Next");
button.InvokeMember("click");
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
One possible solution is to modify your "click" event handler in javascript so it changes the value of some hidden input field right before exiting the method (after all work is done). You can attach to the event of changing field from C# code and act when it's fired.
// Your init method - you can call it after `InitializeComponent`
// in the constructor of your form
Init() {
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webBrowser1.Document.GetElementsByTagName("statusField")[0].AttachEventHandler("onchange", WorkDone);
}
void WorkDone(object sender, EventArgs e) {
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
That's the raw solution, I haven't yet checked whether "onchange" is a correct DOM event.
Also, you can't attach to DOM events before the document is completely loaded, that's why I put the attaching logic in the handler of DocumentCompleted event.

Problems with IsPostBack

I have an .aspx page which sends a letter to a customer if a button on that page is clicked. Onclick the page calls itself, so the mail send class is in the same file. However I do not want the mail sent when the page is simply loaded. I want it send the letter when the button is clicked, so, I'm trying with the following code:
void page_Load(Object sender, System.EventArgs e)
{
if (IsPostBack)
{
SendMail();
}
}
But it doesn't work. What am I doing wrong?
Why not use the Button's Click Event then?
What you are talking about is you want to send an email only when a specific button is clicked. Then why not register to it's click event instead of bloating your page_load with extra code?
Button's click event is raised only when that button's click causes a postback. So, that's your best option.
Make an event handler for the button's click event (Just double click the button in Visual Studio's Designer).
Using Page_Load will result in emails being sent out when the user posts back in any circumstance, not just your button click.
Looks like the page does not find the correct event handler for the Page_Load, check the case and correct it to Page_Load
See if this works (replace your page_Load method with the following code):
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
if (IsPostBack)
{
SendMail();
}
}
You are currently relying on AutoEventWireup functionality to hook up your page events. This is slow and problematic and may be the cause of your issue. The method I gave you overrides Page.OnLoad and should correct the problem as well.
The Page_Load event is raised every time the page is posted, well by means of postback or callback, if you want to use server side events you should call you SendMail method in the button's click event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !IsCallback)
{
/*occurs the first time the page is loaded*/
}
if (IsPostBack)
{
/*occurs every time a postback is raised (e.g. by form submission) */
}
if (IsCallback)
{
/*occurs every time a callback is raised, e.g. by generating callbacks by means of AJAX/
}
}
protected void SendMail_Click(object sender, EventArgs e) { SendMail(); }
Read more at MSDN: ASP.NET Page Life Cycle.

Categories