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);
}
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.
How do I make so when I click on a textbox, make form2 open?
I understand is something like
Form2 f2 = new form2();
f2.show();
but its not a button, it's a textbox.
Im currently expressing some values in the textbox, and I want to click the textbox and form 2 to appear, which haves 4 option to change the value into.
listBox2.Items.Add(" 1 " + seatArray[0].PrintInfo);
listBox2.Items.Add(" 2 " + seatArray[1].PrintInfo);
listBox2.Items.Add(" 3 " + seatArray[2].PrintInfo);
listBox2.Items.Add(" 4 " + seatArray[3].PrintInfo);
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string test = listBox2.SelectedItem.ToString().Substring(0,2);
int num = int.Parse(test);
textBox1.Text = test;//Row
textBox2.Text = seatArray[num-1].A.ToString();
textBox7.Text = seatArray[num-1].B.ToString();
textBox5.Text = seatArray[num-1].C.ToString();
textBox3.Text = seatArray[num-1].D.ToString();
textBox6.Text = seatArray[num-1].E.ToString();
textBox4.Text = seatArray[num-1].F.ToString();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
when I click and item on my listbox, it summons the form 2, but I need for it to open when I click the textbox itself.
You need an event handler for the click-event of the textbox:
private void NameOfYourTextBox_Click(oject sender, EventArgs e)
{
var secondForm = new form2();
secondForm.Show();
}
Normally you would use
secondForm.ShowDialog();
to provide a modal form and wrap everything inside an using:
private void NameOfYourTextBox_Click(oject sender, EventArgs e)
{
using (var secondForm = new form2())
{
var result = secondForm.ShowDialog();
// result contains informations on what user did with the form
}
}
because this disposes the form-instance after usage.
So where in this chain is your gap?
place in textbox click event
private void textBox1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
I have a form with a tabControl and inside of each tab is a flowLayoutPanel where I can drag and drop files and a button is created for each dropped file. Afterwards when I click on a button, the file that i dropped should open. I have managed to do this for one file only.. My problem is how can I tell which button was clicked and to open the file/app stored in the path for each button.. How can I differentiate in the button_click event the clicked button and the path of the app to open?
Code for this part so far:
Process myProcess = new Process();
string path_app;
public Form1()
{
InitializeComponent();
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
}
}
private void button_Click(object sender, System.EventArgs e)
{
myProcess.StartInfo.FileName =path_app;
myProcess.Start();
}
Also my tabControl has the possibility to add new tabs but how can I get the selected tab and the inside flowLayoutPanel to know where to create the button?
And by the way, is there a problem of how I open the files? I understood that i have to take into consideration the working directory..
Thank you for your help!
You can utilize Tag property of the Button:
void Form1_DragDrop(object sender, DragEventArgs e)
{
foreach (String s e.Data.GetData(DataFormats.FileDrop))
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
// Add to Tag any data you want to pin to the button
button.Tag = path_app;
}
}
private void button_Click(object sender, System.EventArgs e)
{
// Obtain via Tag
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
You could use button.Tag = "theFancyPath" and in the EventHandler cast the object sender as Button to access the Tag property.
If you need more then you could inherit from Button:
public class ButtonWithPathProperty : Button
{
public FileInfo PathToOpen { get; private set; }
public ButtonWithPathProperty(FileInfo path)
{
PathToOpen = path;
this.Click += new EventHandler(this.button_Click);
}
private void button_Click(object sender, System.EventArgs e)
{
var yourPath = this.PathToOpen;
}
}
This is not tested btw :)
I have making the Tab Document Interface like that Firefox. I want to make a some portion of the page refresh Time to Time. I think have you seen already Cricket Page Refresh page after some time. I have used panel for the refresh for page but panel not show. I have try but can't do this.I am working at windows form. plz any body solve this problem and thanks in advance. My coding part here:-
private void Form1_Load(object sender, EventArgs e)
{
AddNewTab();
toolStripTextBox1.Text = "Go to website";
toolStripTextBox2.Text = "Google Search text here";
toolStripComboBox1.Text = "Google.co.in";
toolStripComboBox1.Items.Add("Google.co.in");
timer1.Enabled = true;
timer1.Start();
panel1.Show();
label1.Text = DateTime.Now.ToString();
}
TabPage tp;
WebBrowser wb;
private void AddNewTab()
{
tp=new TabPage();
wb=new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Focus();
wb.Url = new Uri("http://google.co.in");
wb.Dock=DockStyle.Fill;
tp.Controls.Add(wb);
tabControl1.TabPages.Insert(tabControl1.TabPages.Count - 1, tp);
tabControl1.SelectedTab = tp;
}
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");
}