问题描述
如何将 +
按钮添加到 Windows 窗体应用程序中的 TabControl
.这是 到控件并指定选项卡宽度允许的最小尺寸:
[DllImport("user32.dll")]私有静态外部 IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);私有常量 int TCM_SETMINTABWIDTH = 0x1300 + 49;private void tabControl1_HandleCreated(object sender, EventArgs e){SendMessage(this.tabControl1.Handle, TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)16);}
注意
您可以简单地将逻辑封装在派生的
TabContol
中,并制作一个支持添加选项卡的自定义选项卡控件.关闭按钮:您也可以简单地使控件所有者绘制并处理选项卡的绘制以显示
+
图标和X选项卡上的图标.例如,您可以在这篇文章中看到一个实现:带有关闭和添加按钮的 TabControl.
从右到左 (RTL) 支持:您可以在使用自绘选项卡时添加对 RTL 的支持.这篇文章:右至 TabPages 的关闭按钮Left TabControl 是一个解决方案.
How can I add a
+
button to the TabControl
in a Windows Forms Application. Here is an answer for WPF. But I want it in WinForms application?
解决方案
You can add a new tab to the end of tabs of control and set it's text to
+
and then:
Check if the user clicked on the last tab, then insert a new tab before it.
You should prevent selection of the last tab.
You should adjust the width of tabs and let the last tab have smaller width.
Then you will have a tab control like below. To have larger tab buttons, I have applied a padding to the control.
Hanlde Click on Last Tab
You can handle
MouseDown
or MouseClick
event and check if the last tab rectangle contains the mouse clicked point, then insert a tab before the last tab:
private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
var lastIndex = this.tabControl1.TabCount - 1;
if (this.tabControl1.GetTabRect(lastIndex).Contains(e.Location))
{
this.tabControl1.TabPages.Insert(lastIndex, "New Tab");
this.tabControl1.SelectedIndex = lastIndex;
}
}
Prevent Selectin of Last Tab
To prevent selection of last tab, you can handle
Selecting
event of control and check if the selecting tab is the last tab, cancel the event:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPageIndex == this.tabControl1.TabCount - 1)
e.Cancel = true;
}
Adjust Width of Tabs
To adjust tab width and let the last tab have smaller width, you can hanlde
HandleCreated
event and send a TCM_SETMINTABWIDTH
to the control and specify the minimum size allowed for the tab width:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int TCM_SETMINTABWIDTH = 0x1300 + 49;
private void tabControl1_HandleCreated(object sender, EventArgs e)
{
SendMessage(this.tabControl1.Handle, TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)16);
}
Note
You can simply encapsulate the logic in a derived
TabContol
and make a custom tab control which supports adding tabs.Close button: Also you can simply make the control owner-draw and handle Painting of tabs to show a
+
icon andX
icon on tabs. As an example you can see an implementation in this post: TabControl with Close and Add Button.Right to Left (RTL) support: You can add support for RTL when using owner-draw tab. This post: Close button for TabPages of Right To Left TabControl is a solution.
这篇关于WinForms TabControl - 添加新标签按钮 (+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!