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");
}
Related
I am using CefSharp to create a browser. It is working, I can navigate to various websites by using new tab. But when I click on previous tabs, all the tab shows same URL in the address bar and all of them has exactly same title. Here is my code:
private void FormBrowser_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
browser.Parent = tabControl.SelectedTab;
browser.Dock = DockStyle.Fill;
browser.AddressChanged += Browser_AddressChanged;
browser.TitleChanged += Browser_TitleChanged;
}
// new tab function
public void addNewTab()
{
TabPage tpage = new TabPage();
tpage.Text = "New Tab";
tabControl.Controls.Add(tpage);
tabControl.SelectTab(tabControl.TabCount - 1);
toolStripTextBoxAddress.Text = "";
ChromiumWebBrowser browser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text);
browser.Parent = tpage;
browser.Dock = DockStyle.Fill;
browser.AddressChanged += Browser_AddressChanged;
browser.TitleChanged += Browser_TitleChanged;
}
private void Browser_TitleChanged(object sender, TitleChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
tabControl.SelectedTab.Text = e.Title;
}));
}
private void Browser_AddressChanged(object sender, AddressChangedEventArgs e)
{
this.Invoke(new MethodInvoker(() =>
{
toolStripTextBoxAddress.Text = e.Address;
}));
}
// navigate method
private void toolStripTextBoxAddress_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (!string.IsNullOrEmpty(toolStripTextBoxAddress.Text))
{
if (!toolStripTextBoxAddress.Text.Contains("."))
{
getCurrentBrowser().Load("http://www.google.com/search?q=" + toolStripTextBoxAddress.Text);
}
else
{
getCurrentBrowser().Load(toolStripTextBoxAddress.Text);
}
}
}
}
// get current browser
private ChromiumWebBrowser getCurrentBrowser()
{
return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
}
// new tab button
private void toolStripButtonNewTab_Click(object sender, EventArgs e)
{
addNewTab();
}
Here is what I have tried:
private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
{
ChromiumWebBrowser currentBrowser = getCurrentBrowser();
toolStripTextBoxAddress.Text = currentBrowser.Address;
}
When i try to open a new tab it is giving me an error in this line return (ChromiumWebBrowser)tabControl.SelectedTab.Controls[0];
How can I solve this problem? Thanks in advance.
I wrote my multi-tab cefsharp code in a very similar fashion to yours, and encountered the same error.
It was cause by the default number of tab pages. (When you drag tabcontrol to your form, it by default comes with 2 tabpages to start with). From the property panel, I removed those two tabpages, so that the browser starts wit zero tabpage. Any tabpage is only added when you start browsing, by inputting url or clicking favorites.
If you do not set the initial number of tabpages to zero, those two "empty" tabpages have no browser attached to them. Therefore the getcurrentbrowser() function fails to find any browser on those empty tabpages and errors occur.
I am having TabControl in which there are multiple tabs. I need to have a functionality in which if I 'drag a tab' or 'right click on tab -> float' then that tab comes out from tabs list and becomes free float window(or a winform) which can be moved anywhere on the screen. And vice Versa.
Here is the code for the TabControl-
TabControl tabControl = new TabControl();
TabPage tabPage1 = new TabPage();
tabPage1.Text = "Tab Page 1";
TabPage tabPage2 = new TabPage();
tabPage2.Text = "Tab Page 2";
tabControl.Controls(tabPage1);
tabControl.Controls(tabPage2);
Please use below code for swaping Tab-Page controls to Form and vise-versa. Please note that instead of adding controls to Tab-Page, you need to first add controls in a panel and then that panel need to be added to the Tab_Page:
private void Button1_Click(object sender, EventArgs e)
{
TabPage tabPage1 = (TabPage) sender;
Form frm = new Form();
frm.Text = tabPage1.Text;
Panel panel = (Panel) tabPage1.Controls[0];
tabPage1.Controls.RemoveAt(0);
tabControl.TabPages.Remove(tabPage1);
frm.Controls.Add(panel);
frm.Show();
}
private void Form1_Click(object sender, EventArgs e)
{
Form frm = (Form) sender;
TabPage tabPage1 = new TabPage();
tabPage1.Text = frm.Text;
Panel panel = (Panel)frm.Controls[0];
frm.Controls.RemoveAt(0);
tabControl.TabPages.Add(tabPage1);
frm.Controls.RemoveAt(0);
frm.Hide();
}
I'm creating a Windows Universal App which contains a ListView filled with User-Controls. User-Controls are added to ListView dynamically during runtime, based on the elements from the database.
public void ShowFavorites()
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), (Application.Current as App).DBPath))
{
var Favorites = conn.Table<Favorites>();
lvFavorites.Items.Clear();
foreach (var fav in Favorites)
{
FavoriteItem favItem = new FavoriteItem();
favItem.Favorite = fav;
lvFavorites.Items.Add(favItem);
}
}
}
So how can i create an event that that triggers when the user-control is pressed?
When you create the control, all you need to do is simply link the control to a new event:
// Dynamically set the properties of the control
btn.Location = new Point((lbl.Width + cmb.Width + 17), 5);
btn.Size = new System.Drawing.Size(90, 23);
btn.Text = "Add to Table";
// Create the control
this.Controls.Add(btn);
// Link it to an Event
btn.Click += new EventHandler(btn_Click);
Then, when you (in this case) click on the newly added button, it will call your btn_Click method:
private void btn_Click(object sender, EventArgs e)
{
//do stuff...
}
I got it working even for items with diffrent text etc.
I gonna explain it based on a button beeing added to a panel.
You need to have a List<> in which you store the items, in this case buttons.
List<Button> BtList = new List<Button>();
and I also have a Panel in this case.
Panel PanelForButtons = new Panel();
Here is my code I hope it helps you:
void AddItemToPanel()
{
//Creating a new temporary item.
Button TempBt = new Button();
TempBt.Text = "Hello world!";
//Adding the button to our itemlist.
BtList.Add(TempBt);
//Adding the event to our button.
//Because the added item is always the last we use:
PanelForButtons.Controls.Add(BtList.Last());
BtList.Last().Click += MyButtonClicked;
}
And here is the event:
void MyButtonClicked(object sender, EventArgs e)
{
//First we need to convert our object to a button.
Button ClickedButton = sender as Button;
//And there we have our item.
//We can change the text for example:
ClickedButton.Text = "The world says: \"Hello!\"";
}
I have created two buttons close and new tab and a web browser which loads google. When I click new tab it opens the new tab but doesn't open a web browser.
Here's the code I have so far which is from the Microsoft help site. I'm using Visual Studio 2012 with c#.
private void newTab_Click(object sender, EventArgs e)
{
string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
}
private void closeTab_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
At the moment I have this code but when I type in the search box it only works in tabpage 1
and not in any of the new tabs that I open so I need to add some code to the searchbox?
my code is:
private void newTab_Click(object sender, EventArgs e)
{
string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
var browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
browser.Url = new Uri(#"http://www.google.com");
myTabPage.Controls.Add(browser);
tabControl1.TabPages.Add(myTabPage);
}
You are missing webbrowser control in your code:
private void newTab_Click(object sender, EventArgs e)
{
string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
WebBrowser wb = new WebBrowser();
myTabPage.Controls.Add(wb);
wb.Navigate("google.com");
}
private void closeTab_Click(object sender, EventArgs e)
{
if(tabControl1.TabPages.Count == 1)
return;
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
You also have to add a new instance of the WebBrowser control to your tab:
private void newTab_Click(object sender, EventArgs e)
{
string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
// Create new WebBrowser instance
var browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
browser.Url = new Uri(#"http://www.google.de");
// Add the WebBrowser control to the TabPage.
myTabPage.Controls.Add(browser);
tabControl1.TabPages.Add(myTabPage);
}
Each TabPage is initially empty, just like it is if you would have added it using the designer. It's content depends on what you want to add. Sharing a WebBrowser-Control does not make sense, since you want to open different websites on different tabs, I guess.
In case you do not want the user to close the first tab you can simply check if the tab the user requests to close is the first one created:
private void closeTab_Click(object sender, EventArgs e)
{
// Is the Tab Page the first one?
if (tabControl1.SelectedTab == tabControl1.TabPages[0])
return;
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
Or in case you want at least one tab page to stay "open" you can use the TabCount-Property:
private void closeTab_Click(object sender, EventArgs e)
{
// Is the Tab Page the last one opened?
if (tabControl1.TabCount == 1)
return;
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
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);
}