Is there any way to float TabPage of TabControl? - c#

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

Related

blank form in second TabPage in TabControl

hello I have a problem with TabControl. When I open the second Form it is always empty with no controls or anything.
my code. When I open the first tabpage everything is fine. Everything is in place
private void ShowFormInTabPage(TabPage tp , Form frm)
{
tabControl1.Controls.Add(tp);
frm.TopLevel = false;
tp.Text = frm.Text;
frm.Visible = true;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
tabControl1.TabPages[0].Controls.Add(frm);
}
I add form to tabcontrol through such a method. the methods are the same in both cases
private void guna2Button1_Click(object sender, EventArgs e)
{
_tpEmployees = new TabPage();
if (EmployeesForm.IsNull)
{
ShowFormInTabPage(_tpEmployees , EmployeesForm.Instance);
}
else
{
tabControl1.SelectedTab = _tpEmployees;
}

Not getting New Tab in WebBrowser Visual Studio 2012

I have two forms Form1 and Form2.
Form1 has a WebBrowser and Form2 contains tab code.
In Form1 on load I have kept the following code to get first tab:
public void Form1_Load(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
tabControl1.TabPages.Add(t);
}
This is working fine.
Now in Form2 I have a menu item which when clicked will open new tab. Its code is as follows:
public void addTabToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
Form1 b = new Form1();
b.tabControl1.TabPages.Add(t);
}
But I don't get a new tab. i.e. When I click the menu item nothing happens.
You are creating a new Form1 in your click Handler, it is not the Form1 that you used to create the instance of Form2 in. Your best bet would be to use the Show method that allows you to set the owning form and as long as your TabControl's Modifier is public it will work. This is IMHO really not the way to go. I would look into creating an event and subscribe to that.
public void Form1_Load(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show(this); //note the change here
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
tabControl1.TabPages.Add(t);
}
This will enable you to do this:
((Form1)this.Owner).tabControl1.TabPages.Add(t);

Load Image to picturebox control inside tabpage of tabcontrol

I have a winform on which I have tab control and I'm adding a picture box control to the tab page. I want to load an image to this picturebox control in the tab page. I'm adding the control to tab page is as follows:
// initializing the picture box control
m_Canvas = new PhotoCanvas();
m_Canvas.BackColor = Color.White;
m_Canvas.Width = 500;
m_Canvas.Height = 400;
m_Canvas.Left = 0;
m_Canvas.Top = 0;
//adding the picture box control to tabpage
this.MainTab.TabPages[0].Controls.Add(m_Canvas);
I have tried to load the image using an OpenFileDialog:
m_Canvas.Image = new Bitmap(m_OpenFileDialog.FileName);
but it's not showing the loaded image Can anyone help me please?
From comments:
I'm using a button to load the image and the code inside it's click event is
private void Openbutton_Click_1(object sender, EventArgs e) {
m_OpenFileDialog.Title = "Select Image";
if (m_OpenFileDialog.ShowDialog() == DialogResult.OK) {
m_Canvas.Image = new Bitmap(m_OpenFileDialog.FileName);
m_Canvas.Refresh();
}
}
Try:
string path = m_OpenFileDialog.FileName;
m_Canvas.Image = System.Drawing.Bitmap.FromFile(path);

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

C# Tooltip on TabPage

How can I show Tooltip text on my own TabPages extends from TabPages just added some extra variables and using DrawItem event.
I've generate them in method.
ISIMtabPage newTab = new ISIMtabPage();
// setting up newTab
string contactName = getContactName(object);
chatFormTabs.TabPages.Add(newTab);
toolTip1.SetToolTip((ISIMtabPage)chatFormTabs.TabPages[chatId], contactName);
But if I hover on the tab it doesn't do anything.
I've TabControl.ShowToolTips enabled.
What should I do?
Thanks in advance.
I would check toolTip1.SetToolTip
example for Tooltip use:
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private void MyTabs()
{
this.tabControl1 = new TabControl();
this.tabPage1 = new TabPage();
this.tabPage2 = new TabPage();
this.tabControl1.Controls.AddRange(new Control[] {
this.tabPage1,
this.tabPage2});
this.tabControl1.Location = new Point(35, 25);
this.tabControl1.Size = new Size(220, 220);
// Shows ToolTipText when the mouse passes over tabs.
this.tabControl1.ShowToolTips = true;
// Assigns string values to ToolTipText.
this.tabPage1.ToolTipText = "myTabPage1";
this.tabPage2.ToolTipText = "myTabPage2";
this.Size = new Size(300, 300);
this.Controls.AddRange(new Control[] {
this.tabControl1});
}
public Form1()
{
MyTabs();
}
static void Main()
{
Application.Run(new Form1());
}
}
Actually kinda tricky, so make sure that from the tabControl1 properties "ShowToolTips" is True.
the form1 has a tooltip1 and a TabControl named tabControl1 with multiple tabs for testing
tabControl1 has a collection of tabs
edit the collection and set the tab you want to have the tooltip "ToolTip on toolTip" and ToolTipText to "your message here"

Categories

Resources