how to denied open new window in geckofx? - c#

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);
}

Related

MouseClick event handler doesn't work in ChromiumWebBrowser

On other objects like the Forms itself MouseClick event works but when it comes to ChromiumWebBrowser the event handler simply doesn't listen any mouse clicks.
The browser is in a tab page. I tried to listen mouse clicks from there but that also didn't work.
private void ChromeBrowser_MouseClick(object sender, MouseEventArgs e)
{
Log("Click");
}
Designer.cs:
//
// chromeBrowser
//
this.chromeBrowser.ActivateBrowserOnCreation = false;
this.chromeBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.chromeBrowser.Location = new System.Drawing.Point(3, 3);
this.chromeBrowser.Name = "chromeBrowser";
this.chromeBrowser.Size = new System.Drawing.Size(1003, 539);
this.chromeBrowser.TabIndex = 0;
this.chromeBrowser.MouseClick += new System.Windows.Forms.MouseEventHandler(this.ChromeBrowser_MouseClick);
//

how can I catch event when navigate the url

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

How to add button to a previous, manual-created form?

What I'm trying to do is: create a new form with a click (DONE and working, see code below), and then add some buttons to that new form. In this case, it's just one button because I need to make this work before adding more buttons. Should be pretty simple, but after following some Stackoverflow answers/YouTube tutorials/Internet tutorials I'm still not able to do it.
Actually, [the application] It's meant to be like a personal schedule, where I could track every activity or work to do, dispersed over several days (from monday to friday) and in each day you should find the different times of the day (morning/midday/evening).
My code is shown below (this code belongs to the button1_Click method of the first form, as you may notice).
private void button1_Click(object sender, EventArgs e)
{
// día lunes
Form SubLunes = new Form(); // new form
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
SubLunes.ShowDialog();
// botones
Button Mañana = new Button(); // new button
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana); // should add button to SubLunes
}
private void Mañana_Click(object sender, EventArgs e)
{
MessageBox.Show("hello, i'm new button"); // displayed when clicking new button
}
This is how currently looks:
Main form ///////
After clicking Lunes button (a new Button called 'Mañana' should be in there)
THANKS YOU in advance. See you later.
You're showing the form before creating the button :
Form SubLunes = new Form();
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
SubLunes.ShowDialog();
Button Mañana = new Button();
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana);
You should create the button before showing the form, like this :
Form SubLunes = new Form();
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
Button Mañana = new Button(); // new button
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana);
SubLunes.ShowDialog();

Save window state when application in tray

I have an app that minimizes to the system tray by clicking on "close" button and I want to save it's state (position, all elements (comboboxes, textboxes) with their values, etc).
Now I wrote this code, but it creates a new window from tray (instead of recovering the old one, with its parameters):
# app.xaml.cs:
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
// create a system tray icon
var ni = new System.Windows.Forms.NotifyIcon();
ni.Visible = true;
ni.Icon = QuickTranslator.Properties.Resources.MainIcon;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
var wnd = new MainWindow();
wnd.Visibility = Visibility.Visible;
};
// set the context menu
ni.ContextMenu = new System.Windows.Forms.ContextMenu(new[]
{
new System.Windows.Forms.MenuItem("About", delegate
{
var uri = new Uri("AboutWindow.xaml", UriKind.Relative);
var wnd = Application.LoadComponent(uri) as Window;
wnd.Visibility = Visibility.Visible;
}),
new System.Windows.Forms.MenuItem("Exit", delegate
{
ni.Visible = false;
this.Shutdown();
})
});
How I can modify this code for my problem?
When you hold a reference to your `MainWindow´ then you can simple call Show() again after closing it. Closing the Window will simply hide it and calling Show again will restore it.
private Window m_MainWindow;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
if(m_MainWindow == null)
m_MainWindow = new MainWindow();
m_MainWindow.Show();
};
If you´re sure that the MainWidnow is your Applications primary Window then you can also use this:
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
Application.MainWindow.Show();
};
I would prefer the first variant since it´s explicit.

Create a web browser with tab capability

I want to recreate a simple browser with tab capability. A new tab has to be created every time the user clicks on "button_addTab" or when the selected web site tries to open a new window.
Here is my code:
Add a new tab by pressing the dedicated button:
private void button_addTab_Click(object sender, EventArgs e)
{
TabPage addedTabPage = new TabPage("tab title"); //create the new tab
tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage, //add the new webBrowser to the new tab
Dock = DockStyle.Fill
}
addedWebBrowser.Navigate("www.google.com");
Prevent a site from opening new windows (if you know every webBrowsers name) :
private void specificWebBrowser_NewWindow(object sender, CancelEventArgs e)
{
WebBrowser thisWebBrowser = (WebBrowser)sender;
e.Cancel = true;
TabPage addedTabPage = new TabPage("tab title");
tabControl_webBrowsers.TabPages.Add(addedTabPage);
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage,
Dock = DockStyle.Fill
}
addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString());
}
Now my question is:
How can I modify the second branch of code in order to match all the web browsers created with no names when the first code is called (at one time)?
Try to modify the first function to attach an event handler to newly created browser:
private void button_addTab_Click(object sender, EventArgs e)
{
TabPage addedTabPage = new TabPage("tab title"); //create the new tab
tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage, //add the new webBrowser to the new tab
Dock = DockStyle.Fill
};
addedWebBrowser.NewWindow += specificWebBrowser_NewWindow;
addedWebBrowser.Navigate("www.google.com");
}

Categories

Resources