Adding close button on tab header in c# winforms [duplicate] - c#

My scenario is the following:
I am working on a winforms application in C# that has a button inside the main page of a tabcontrol that will generate another tabpage each time that it is clicked. Each new tabpage will contain a layout defined by a user control.
My Questions are:
How can I allow the user to then close one of the tabs that were created dynamically at runtime?
How might I go about modifying the tabcontrol itself so that it has a small 'X' in each tab that the user may click on in order to close that particular tab? (Like Firefox has)
How can I expose the SelectedIndex property of the tabcontrol to the user control if I want to close the tab with a button inside the user control instead?

I found this code and was very helpful to me:
private void tabControl_MouseUp(object sender, MouseEventArgs e)
{
// check if the right mouse button was pressed
if(e.Button == MouseButtons.Right)
{
// iterate through all the tab pages
for(int i = 0; i < tabControl1.TabCount; i++)
{
// get their rectangle area and check if it contains the mouse cursor
Rectangle r = tabControl1.GetTabRect(i);
if (r.Contains(e.Location))
{
// show the context menu here
System.Diagnostics.Debug.WriteLine("TabPressed: " + i);
}
}
}
}
TabControl: How To Capture Mouse Right-Click On Tab

I created a derived tab control about one year ago. I am not going to post the source here, because it's about 700 lines long and coded quite messy. Maybe I will find some time to clean the code up and then release it here. For now I will briefly outline the way it is build.
Each tab page has a 'X' icon to the left of the title and the tab pages support reordering by drag and drop and moving them between multiple tab control.
I choose the easy way to get the icon on the tab pages. The tab control has the TabControl.ImageList property and a tab page has a TabPage.ImageIndex property. So I just added three icons to a image list - normal, hover, pressed - and process the mouse events.
With TabControl.GetTabRect() you can test if the mouse is over a specific tab pages and with some math you find if it is over the icon. Then you just need to change the icon depending on the mouse button state and eventually remove the tab page under the mouse if the button was pressed.
The main problem with this solution is, that calculating if the mouse is over the icon requires to know where the icon is painted relative to the tab page and this might change with a new windows version. And the icon is to the left of the title, but that does not look too bad.

I did the following:
on the create (add) TabPage stage, I added a toolStrip
ToolStrip ts = new ToolStrip();
ts.Dock = DockStyle.Top;
ts.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
Then, create the X button and add it to toolstrip
ToolStripButton ToolStripButton = new ToolStripButton("X");
ts.Items.Add(ToolStripButton);
create an event on clicking the X button
ToolStripButton.Click += new EventHandler(ToolStripButton_Click);
add toolstrip to the tabpage
tabControl1.TabPages[curenttabpage].Controls.Add(ts);
now for the ToolStripButton_Click is as follows:
void ToolStripButton_Click(object sender, EventArgs e)
{
ToolStripButton t = (ToolStripButton)(sender);
ToolStrip ts = t.Owner;
TabPage tb = (TabPage)
(ts.Parent);tabControl1.TabPages.Remove(tb);
}
Maybe it is not as you want, but it will work well.

I created a setup that is similar.
Each control that is added to the tab page at runtime is derived from a special base control I created. This base control has a close button (along with some other features such as safe to close flag).
Close tab code I'm using on my base control:
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(parent);

I know this is an old thread but I did find this link that will allow you to "hide" tabs in an array and then you can just re-load the tabs you want at run time. I added this more for a place I can easily find it again.

This code might help throgh closing the tab controls with middle mouse click :
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Middle)
return;
for (int i = 0; i < MainTabControl.TabPages.Count; i++)
{
if (this.MainTabControl.GetTabRect(i).Contains(e.Location))
{
this.MainTabControl.TabPages.RemoveAt(i);
return;
}
}
}

It´s works!
TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(tabpage);

Related

How can I resize a TreeView during runtime in a Control

I am trying to resize my TreeView window during runtime and I cant do it.
in my program I can press on a button and the TreeView opens as a popup window.
The TreeView is inside a Control:
private Control parent;
mytree= new TreeView();
parent.Controls.Add(mytree);
I have already tried to search for any resize properties but no luck, I can't find a way that the tree will be resizable during runtime.
The only way I can see it is to delete the control and to put it in a Form and then I can make it look like the same but I still want to know if there is another way to solve this, please if someone knows !
thank you
Assign anchor property to tree view so it resizes with form (or maybe control he is child of)
Here is example of anchoring button:
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
// Create a button and add it to the form.
Button button1 = new Button();
// Anchor the button to the bottom right corner of the form
button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
// Assign a background image.
button1.BackgroundImage = imageList1.Images[0];
// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;
// Make the button the same size as the image.
button1.Size = button1.BackgroundImage.Size;
// Set the button's TabIndex and TabStop properties.
button1.TabIndex = 1;
button1.TabStop = true;
// Add a delegate to handle the Click event.
button1.Click += new System.EventHandler(this.button1_Click);
// Add the button to the form.
this.Controls.Add(button1);
}
Important part is button1.Anchor where in example button is anchored to bottom right of window, so it will just follow window as you resize, but you can anchor to all sides of window and it will resize with it.
Try different things and you will figure it out.
SOURCE: MSDN

dropdown menu using a label

Ok I have been redoing an app I created 4 years ago. I was wandering if there was a way to create a drop down menus using labels.
Currently I have a few buttons on a borderless form and the menustrip is just not looking right.
So I figured if I do a binding on a label I can achieve what I am looking for. I have a label that opens and hides my tab control.
Is there a way to extend that when on the mouse hover event it will produce a drop down menu?
Example
Customize (label)
Commands (tab)
User Input (tab)
Web Commands (tab)
Is it possible to do a drop down menu to list each tab in my tab control individually? My main label Customize opens and hides the tab control.
I really don't want to use menuStrips or buttons or anything because it just does not look right. I have two buttons one is a log out button and the other will be an update commands button once I get the tab Control situated.
If it can't be done then I will just leave it the way it is, and just click the tab I want. The picture below is my main form. I click customize and it brings up a tabControl. I am wanting the form to be border-less. I dont want a white strip going across the top of the form. I am trying to make the form smooth. So when i hover my mouse over customize it will display the tabs in the TabControl. Like a menu. See both pictures below.
Update : This is what i am trying to do, using a Label only. I do not want to use any of these:
toolStrips,
toolStripContainers,
menuStrips,
ContextMenuStrip,
buttons
comboBoxes.
I want to use solely a label. And only a label.
It was painful trying to figure out what you were asking help for, but as near as I can tell, this is probably what you were looking to do:
Size originalSize;
String originalText;
void CustomizeLabel_MouseEnter(object sender, EventArgs e) {
originalSize = CustomizeLabel.Size;
originalText = CustomizeLabel.Text;
StringBuilder sb = new StringBuilder();
sb.AppendLine(originalText);
foreach (TabPage tp in tabControl1.TabPages) {
sb.AppendLine(" " + tp.Text);
}
CustomizeLabel.Text = sb.ToString();
CustomizeLabel.Height = TextRenderer.MeasureText(CustomizeLabel.Text,
CustomizeLabel.Font).Height;
}
void CustomizeLabel_MouseLeave(object sender, EventArgs e) {
CustomizeLabel.Text = originalText;
CustomizeLabel.Size = originalSize;
}

Hiding TabPage from TabControl in Winform application

I have a TabControl in Winform s application,
I have to disable the second tab, clicking it would be enabled only after some action on my first page.
I have achieved this by disabling tab by code
tabControl1.TabPages[1].Enabled = false;
But I want that tab to be hidden or clicking the tab itself should be disabled.
Try This. It will hide and show the TabPages without a Control lost.
Hide TabPage and Remove the Header:
this.tabPage1.Hide();
this.tabPage3.Hide();
this.tabPage5.Hide();
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage5);
Show TabPage and Visible the Header:
tabControl1.TabPages.Insert(0,tabPage1);
tabControl1.TabPages.Insert(2, tabPage3);
tabControl1.TabPages.Insert(4, tabPage5);
this.tabPage1.Show();
this.tabPage3.Show();
this.tabPage5.Show();
tabControl1.SelectedTab = tabPage1;
You have asked two questions:
How to hide a TabPage
How to make it non-selectable
You can't really hide a TabPage; the closest and simplest solution is to remove it from the orginal Tab control and add it to a hidden helper Tab control:
tabPage3.Parent = helperTab;
To make it non-selectable, you code the Selecting event of the Tab control. You need to set a flag, maybe in the Tag of the page, and then you can prevent a page where the flag is set from being selected:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage.Tag == "X") e.Cancel = true;
}

Usercontrol removal

My problem is the following. I have a form which when it loads up invokes a user control onto it. On the user control I have buttons. With the buttons I would like to make the user control disappear with all the buttons and picture boxes on it and make a new user control appear. The appear part is all right but I can't make the user control disappear with all the objects on it.
Can you help me with this one?
So the dispose method closes the usercontrol well, but then I can't make the new user control appear. Code is like this:
akusztikusUserControl auc = new akusztikusUserControl();
public menuUserControl()
{
InitializeComponent();
}
private void akusztikus_btn_Click(object sender, EventArgs e)
{
this.Dispose();
this.Controls.Add(auc);
}
}
Have you considered using Panel or a TableLayoutPanel control ? In both cases you can remove controls through the .controls.remove option
You can then just add the user control to the panel / tablelayoutpanel whenever you need to use it again.

How can I add a button to the empty area of a TabControl in Winforms?

Grabbed the images from a similar question in WPF. Basically I want the extra buttons to do different things, not create or delete tabs. Actions related to the current tab which are gonna be the same for all the tabs in a single TabControl.
When I try to place my buttons on that area, the visual studio designer throws my control away, back to their original position. It doesn't allow me to put anything there.
Can this be done?
alt text http://www.freeimagehosting.net/uploads/92ca1b0a8c.png
alt text http://www.freeimagehosting.net/uploads/ff0d08e0ed.png
For specifically that visual experience I'd just create a separate tab with header "+" and add some logic on switching to it - so it will perform some other action instead of switching. The same can be done with multiple tabs I guess.
UPDATE
Actual code for this:
private void TabControl1_Selecting(Object sender, TabControlCancelEventArgs e)
{
if (e.TabPage... /* Do check whether some of your special TabPages is being selected */)
{
e.Cancel = true;
// All other TabPage-specific actions here
...
}
}
So it looks like they are using a tab as a button here. I would imagine they have the tab controls index change event fire a new tab into the tab control and force focus to it.

Categories

Resources