how can I catch event when navigate the url - c#

this is the console that call browser and navigate url, how can I check the
event when navigated url to check conflict event.
private static void ThreadStart()
{
WebBrowser browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
browser.Name = "webBrowser";
browser.ScrollBarsEnabled = false;
browser.TabIndex = 0;
browser.Url = new Uri("http://www.microsoft.com");
Form form = new Form();
form.WindowState = FormWindowState.Maximized;
form.Controls.Add(browser);
form.Name = "Browser";
Application.Run(form);
}

You can use the DocumentCompleted event on WebBrowser :
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx

Related

WebBrowser cant open Instagram c#

when i put Instagram's address in my WebBrowser object, on loading of program, it just shows a blank white page, and nothing else, where is problem?!
first i think it should be cause WebBrowser using IE, but IE(ver.11) loads Instagram successfully .
UPDATE:
this is the code which i use:
this.webB.Location = new System.Drawing.Point(93, 17);
this.webB.MinimumSize = new System.Drawing.Size(20, 20);
this.webB.Name = "webB";
this.webB.Size = new System.Drawing.Size(642, 324);
this.webB.TabIndex = 1;
this.webB.Url = new System.Uri("https://www.instagram.com/", System.UriKind.Absolute);
this.webB.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webB_DocumentCompleted);
this.webB.ScriptErrorsSuppressed = true;
Try this one :). This code is useful for loading urls(ex:Instagram) and download the source code of the appropriate page.
protected void LoadUrl(object sender, EventArgs e)
{
string url = txtUrl.Text.Trim();
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 768;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
string sourcecode=browser.documentText;
}

C# navigating webbrowser from created combobox

I created visual web browser.
private void dodajKartę()
{
web = new WebBrowser();
web.DocumentCompleted += web_DocumentCompleted1;
web.Dock = DockStyle.Fill;
web.Visible = true;
web.ScriptErrorsSuppressed = true;
web.IsWebBrowserContextMenuEnabled = false;
pasek = new ToolStrip();
przyciskWstecz = new ToolStripButton();
przyciskDalej = new ToolStripButton();
pasekAdresu = new ToolStripComboBox();
przyciskZamknijKartę = new ToolStripButton();
przyciskMenu = new ToolStripDropDownButton();
wszystkiePrzyciskiMenu(); // this metod define menu buttons
WszystkoOPrzyciskach(); //this method define ToolStrip buttons
tabControl1.TabPages.Add("Nowa karta");
tabControl1.SelectTab(i);
tabControl1.SelectedTab.Controls.Add(web);
tabControl1.SelectedTab.Controls.Add(pasek);
i += 1;
}
I would like to navigate my browser from created ToolStripComboBox
This code not working:``
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate((ToolStripComboBox)tabControl1.SelectedTab.Controls[10]).Text;
How can I change this code?

How to control form load from another form

I want to control my form load event from another form.
my problem I create some winform control in form1 runtime, but the creation will controlled by form2.
I will read some data from user in form2 and when user enter specific text I will create winform control in form1.
I make some code to do that using from1 to create winform control in runtime.
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new System.Drawing.Size(550, 550);
this.Text = "Test Create form in runtime ";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Add";
this.btnAdd.Location = new System.Drawing.Point(90, 25);
this.btnAdd.Size = new System.Drawing.Size(50, 25);
this.txtBox.Text = "Text";
this.txtBox.Location = new System.Drawing.Point(10, 25);
this.txtBox.Size = new System.Drawing.Size(70, 20);
this.lstBox.Items.Add("One");
this.lstBox.Sorted = true;
this.lstBox.Location = new System.Drawing.Point(10, 55);
this.lstBox.Size = new System.Drawing.Size(130, 95);
this.chkBox.Text = "Disable";
this.chkBox.Location = new System.Drawing.Point(15, 190);
this.chkBox.Size = new System.Drawing.Size(110, 30);
this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
this.lblCount.Location = new System.Drawing.Point(55, 160);
this.lblCount.Size = new System.Drawing.Size(65, 15);
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
}
How make the same thing from form2 ?
I don't know which kind of 'Control' you need. However in multiple forms environment, communication between forms is trivial. There are many ways to do communicate, like one can be as
Create public properties of type Form in your Parent form,
public Form propForm1 {get;set;}
When on menu item click you open form1, store it's object to that public property.
var form1 = New yourchildformname();
form1.MdiParent = this;
propForm1 = form1; // Add this line.
form1.Show();
Now every time when you will click an other button to open the form2, you will have propForm1 object, which you can use to set some data on that form.
EDIT:
On form2, you can access controls of form1 as
private void button1_Click(object sender, EventArgs e)
{
this.parent.propForm1.txtUserName = "Yokohama";
}
Remember the above code is on form2. Also set 'access modifier' property of txtUserName from private to public.

Show WinFrm from C# console application and wait to close

I am trying to show a WinForm (inputbox) from a console application in C# and wait until the user closes the form. It is important for me to have the inputbox ontop and active when it opens. ShowDialog() is not working in my case as in some cases it does not appears as an active form. So I'd like to change my code and use Show(). This way I can manually make find out if the form is active or not and if not activate it myself. With ShowDialog(). my code stops and I can not do anything until the from is closed.
Below is my code. It does show the inputbox, however it is frozen. What am I doing wrong please? As clear the while-loop after "inputBox.Show();" is not doing anything, but if I can manage to run the loop and the inputbox does not freeze, I will sort out the rest myself. I appreciate your help.
public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse)
{
string strResponse = null;
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
inputBox.ClientSize = new Size(500, 85);
inputBox.Text = strTitle;
inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width / 2) - (inputBox.ClientSize.Width / 2);
inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (inputBox.ClientSize.Height / 2);
Label lblPrompt = new Label();
lblPrompt.Text = strPrompt;
inputBox.Controls.Add(lblPrompt);
TextBox textBox = new TextBox();
textBox.Text = strDefaultResponse;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.Text = "&OK";
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.Text = "&Cancel";
inputBox.Controls.Add(cancelButton);
okButton.Click += (sender, e) =>
{
strResponse = textBox.Text;
inputBox.Close();
};
cancelButton.Click += (sender, e) =>
{
inputBox.Close();
};
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
SetWindowPos(inputBox.Handle, HWND_TOPMOST, inputBox.Left, inputBox.Top, inputBox.Width, inputBox.Height, NOACTIVATE);
inputBox.Show();
while {true}
Thread.Sleep(100);
Application.DoEvents();
return strResponse;
}
I'm not sure why you are taking this route, I'm sure there are better ways to do it (explain one at the end). however to make your code run you should add Application.DoEvents() inside your loop
the code should be something like this:
var formActive = true;
inputBox.FormClosed += (s, e) => formActive = false;
inputBox.Show();
inputBox.TopMost = true;
inputBox.Activate();
while (formActive)
{
Thread.Sleep(10);
Application.DoEvents();
}
and the whole method will be:
public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse)
{
string strResponse = null;
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
inputBox.ClientSize = new Size(500, 85);
inputBox.Text = strTitle;
inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (inputBox.ClientSize.Width/2);
inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (inputBox.ClientSize.Height/2);
Label lblPrompt = new Label();
lblPrompt.Text = strPrompt;
inputBox.Controls.Add(lblPrompt);
TextBox textBox = new TextBox();
textBox.Text = strDefaultResponse;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.Text = "&OK";
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.Text = "&Cancel";
inputBox.Controls.Add(cancelButton);
okButton.Click += (sender, e) =>
{
strResponse = textBox.Text;
inputBox.Close();
};
cancelButton.Click += (sender, e) =>
{
inputBox.Close();
};
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
var formActive = true;
inputBox.FormClosed += (s, e) => formActive = false;
inputBox.Show();
inputBox.TopMost = true;
inputBox.Activate();
while (formActive)
{
Thread.Sleep(10);
Application.DoEvents();
}
return strResponse;
}
but I think it would be a better Idea to Derive from Form and create a InputBox and set TopMost and call Activate OnLoad to create what you need, then simply call ShowDialog, something like:
class Inputbox : Form
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
TopMost = true;
Activate();
}
}
and better to put UI code in InputBox class as the whole code and usage would be like:
class Inputbox : Form
{
public string Response { get; set; }
public Inputbox(string strTitle, string strPrompt, string strDefaultResponse)
{
//add UI and Controls here
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
ClientSize = new Size(500, 85);
Text = strTitle;
StartPosition = System.Windows.Forms.FormStartPosition.Manual;
Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (ClientSize.Width/2);
Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (ClientSize.Height/2);
Label lblPrompt = new Label();
lblPrompt.Text = strPrompt;
Controls.Add(lblPrompt);
TextBox textBox = new TextBox();
textBox.Text = strDefaultResponse;
Controls.Add(textBox);
Button okButton = new Button();
okButton.Text = "&OK";
Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.Text = "&Cancel";
Controls.Add(cancelButton);
okButton.Click += (sender, e) =>
{
Response = textBox.Text;
Close();
};
cancelButton.Click += (sender, e) =>
{
Close();
};
AcceptButton = okButton;
CancelButton = cancelButton;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
TopMost = true;
Activate();
}
}
public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse)
{
string strResponse = null;
Inputbox inputBox = new Inputbox(strPrompt,strTitle,strDefaultResponse);
inputBox.ShowDialog();
return inputBox.Response;
}
You need to run a message loop:
Application.Run(inputBox);

how to denied open new window in geckofx?

i can't find way to denied open new window when clicking links on webpage. All preferences about popups is not working.
I want to open any clicked links in current window. How can i do that?
You can use the event CreateWindow to handle a new popup window:
GeckoWebBrowser wb1 = new GeckoWebBrowser();
wb1.CreateWindow += new EventHandler<GeckoCreateWindowEventArgs>(wb1_CreateWindow);
Here event CreateWindow:
void wb1_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
{
//Keep popup new window here!
e.Cancel = true;
//e.WebBrowser.Navigate(e.Uri);
// OR
//GeckoWebBrowser wb1 = new GeckoWebBrowser();
//wb1.Navigating += new EventHandler<GeckoNavigatingEventArgs>(wb1_Navigating);
//wb1.Dock = DockStyle.Fill;
//wb1.CreateControl();
//TabPage tab1 = new TabPage("New WebBrowser");
//tabBrowser.TabPages.Add(tab1);
//tab1.Controls.Add(wb1);
//wb1.Navigate(e.Uri);
}

Categories

Resources