How can I make the code when run the code it go to example.com
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
Please correct it when run program it go to example.com
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Document != null)
{
IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (document != null)
{
IHTMLSelectionObject currentSelection = document.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
const String search = "ant";
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
}
Can you Navigate to example.com at Form.Load event? It's working fine in my machine.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
You need to handle the Load event of your form (Form1) if you want the WebBrowser control to automatically navigate to www.example.com whenever your form is shown on the screen.
As it's written now, you handle the Navigated event of the WebBrowser control and tell it to navigate somewhere else. However, the Navigated event is only raised when the browser has navigated to and begun loading a new page. Even if you get your code to work, it will be perpetually chasing its own tail.
Instead, try the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.example.com");
}
}
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("http://www.example.com");
}
This execute the navigate method after the app is initialized.
I'm not sure if I understand your question: The e variable in the webBrowser1_DocumentCompleted method contains the Url property that holds the current Uri object with the URL where the browser control has arrived:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
if (e.Url.Host.EndsWith("example.com"))
{
// do something
}
}
Related
I am building a simple web browser in C#. In order to have all of my buttons such as the go, forward, back, refresh button, along with textbox input in a single tab, I have decided to put a tool strip and web browser control in a single user control that I created. This will enable me to just drop 1 control into a tab. Unfortunately when I try to use my user control it does not work. I know that my code inside the user control is fine, because when I had it in my main form, it functioned properly. I think the main piece that I am missing is I do not understand how to properly call the user control from the main form. Can someone guide me in the right direction here?
The main form.
namespace WebBrowser.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("random text.");
}
}
}
And the User Control
namespace WebBrowser.UI
{
public partial class adkinsBrowser : UserControl
{
public adkinsBrowser()
{
InitializeComponent();
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
webBrowser1.Navigate(toolStripTextBox1.Text.ToString());
}
}
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(toolStripTextBox1.Text.ToString());
}
}
}
I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".
Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.
Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}
I have written some code to post messages to a site. It works just fine when I run it with button clicks, but if I try to run the whole thing with one single shot it throws an error. I know that the problem is that when I try to run it all in one shot the WebBrowser is not loading the page totally and thus it can't post the data. I know this is an easy fix, but I am stumped.
private void button1_Start_Click(object sender, EventArgs e)//this is just the Pseudocode
{
GetData();
SendData();//If I eliminate this and just fire the SendData method with a button click, program works fine
}
private void GetData()
{
webBrowser1.Navigate(inputURLID);
}
private void SendData()//if I replace this with button2_Post_Click it works fine
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
You cant run SendData() because your page don't load element you must wait until page load completely:try this:
private void button1_Start_Click(object sender, EventArgs e)//this is just the Pseudocode
{
GetData();
button1_Start.Enable = false;
}
private void GetData()
{
webBrowser1.Navigate("inputURLID");
}
private void SendData()
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
button1_Start.Enable = true;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
SendData();
}
I've been trying to template control panels in my site so I can take a panel and populate it fully. I'm good up until the point where my event handling needs to access functions on my page. My current test will take me to a login redirect page. So how can I get this event handler to perform a redirect?
public class DebugButton : Button
{
public string msg;
public DebugButton()
{
this.Click += new System.EventHandler(this.Button1_Click);
this.ID = "txtdbgButton";
this.Text = "Click me!";
msg = "not set";
}
protected void Button1_Click(object sender, EventArgs e)
{
msg = "Event handler clicked";
}
}
*on the Page*
protected void Page_Load(object sender, EventArgs e)
DebugButton btnDebug = new DebugButton();
PnlMain.Controls.Add(btnDebug);
Really appreciate the help. Thanks!
To do a redirect you can use:
Note:
Assuming that your login page is named login.aspx and it is located in root folder of your website.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/login.aspx");
}
or
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("login.aspx");
}
If you want the event to have access to the page, then the page needs to subscribe to the click event.
Aka:
on the Page
protected void Page_Load(object sender, EventArgs e)
{
DebugButton btnDebug = new DebugButton();
btnDebug.Click += new System.EventHandler(Button1_Click);
PnlMain.Controls.Add(btnDebug);
}
protected void Button1_Click(object sender, EventArgs e)
{
// access whatever you want on the page here
}
I just found out that that System.Web.HttpContext.Current will get me the current context of the page. So long as the custom class is part of the app(this one is in the apps folder of course) I'm good to go. Heres a sample of my quick TestTemplate that I used to make a custom button.
public class TestTemplate : Button
{
public TestTemplate()
{
this.Text = "Click Me";
this.ID = "btnClickMe";
this.Click += new System.EventHandler(this.EventHandler);
//
// TODO: Add constructor logic here
//
}
public void EventHandler(object sender, EventArgs e)
{
//System.Web.HttpContext.Current.Server.Transfer("Default.aspx");
System.Web.HttpContext.Current.Response.Write("This is a test!");
}
}
I have the following code that, weirdly, works for a couple of seconds and then stops working (my event handler method stops being called):
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate("google.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!webBrowser1.IsBusy && webBrowser1.Url == e.Url && webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HTMLWindowEvents_Event windowEvents = webBrowser1.Document.Window.DomWindow as HTMLWindowEvents_Event;
windowEvents.onscroll += new HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
}
}
private void windowEvents_onscroll()
{
HtmlDocument htmlDoc = webBrowser1.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
string text = scrollTop.ToString();
}
}
OK Found a solution:
protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
{
Follow();
if (!IsBusy && Url == e.Url && ReadyState == WebBrowserReadyState.Complete)
{
Document.Window.AttachEventHandler("onscroll", DocScroll);
}
}
If attached that way it works OK (so far...). Don't even need to use mshtml.