- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 c# WinForm 应用程序,我需要在运行时生成 TabPage。理想情况下,我想使用 VS 设计器设计这些标签页,但似乎我不能直接这样做,因为我不能从工具箱中拖放它(还有另一种方法吗?)。
我有两个我将多次使用的通用标签页。一个包含一个简单的电子表格和几个文本框,另一个包含一个图表和几个文本框。这些页面最终会变得更加复杂。我正在寻找执行此操作的最佳方法。我目前只是为每个标签页创建自定义类并将其设置为 TabPage 基类。例如:
public partial class SpreadsheetTabPage : TabPage{}
我读到用户控件提供了某种形式的替代方法,但我并不真正理解使用它与我的方法相比的优势。
明确地说,我想知道您认为什么是开发这些自定义标签页的最佳方法以及原因。如果相关,请提供基本代码示例。我的方法并没有真正造成太多问题,但我发现以后向这些页面添加内容会很困难,尤其是在不使用设计器的情况下。
提前感谢您的帮助!
最佳答案
您可以创建自己的 TabControl 设计器,并在设计时实现所需的功能。下面是此类设计器最简单版本的代码:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using CustomTabControlExample.TabControl;
namespace CustomTabControlExample.Design {
public class CustomTabControlDesigner :ParentControlDesigner {
DesignerVerbCollection fVerbs;
public override DesignerVerbCollection Verbs {
get {
if (fVerbs == null)
fVerbs = new DesignerVerbCollection(new DesignerVerb[] {
new DesignerVerb("Add Tab", OnAdd)
});
return fVerbs;
}
}
void OnAdd(object sender, EventArgs e) {
TabPage newPage = (TabPage)((IDesignerHost)GetService(typeof(IDesignerHost))).CreateComponent(
typeof(CustomTabPage));
newPage.Text = newPage.Name;
((System.Windows.Forms.TabControl)Component).TabPages.Add(newPage);
}
public override void InitializeNewComponent(IDictionary defaultValues) {
base.InitializeNewComponent(defaultValues);
for (int i = 0; i < 2; i++)
OnAdd(this, EventArgs.Empty);
}
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
// Selection of tabs via mouse
if (m.Msg == 0x201/*WM_LBUTTONDOWN*/) {
System.Windows.Forms.TabControl control = (System.Windows.Forms.TabControl)Component;
int lParam = m.LParam.ToInt32();
Point hitPoint = new Point(lParam & 0xffff, lParam >> 0x10);
if (Control.FromHandle(m.HWnd) == null) // Navigation
if (hitPoint.X < 18 && control.SelectedIndex > 0) // Left
control.SelectedIndex--;
else control.SelectedIndex++; // Right
else // Header click
for (int i = 0; i < control.TabCount; i++)
if (control.GetTabRect(i).Contains(hitPoint)) {
control.SelectedIndex = i;
return;
}
}
}
protected override void OnDragDrop(DragEventArgs de) {
((IDropTarget)((System.Windows.Forms.TabControl)Component).SelectedTab).OnDragDrop(de);
}
protected override void OnDragEnter(DragEventArgs de) {
((IDropTarget)((System.Windows.Forms.TabControl)Component).SelectedTab).OnDragEnter(de);
}
protected override void OnDragLeave(EventArgs e) {
((IDropTarget)((System.Windows.Forms.TabControl)Component).SelectedTab).OnDragLeave(e);
}
protected override void OnDragOver(DragEventArgs de) {
((IDropTarget)((System.Windows.Forms.TabControl)Component).SelectedTab).OnDragOver(de);
}
}
}
这似乎是一个相当复杂的方法,但是一旦你学会了它,你就会被 Visual Studio 提供的强大功能所折服。您可以在 MSDN 中找到很多信息。这里描述了最常见的功能:Enhancing Design-Time Support
关于c# - 在 C# WinForms 中设计自定义 TabPage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9535204/
要在 winform 应用程序中创建一个新选项卡,我在 msdn 中找到了这个: string title = "TabPage " + (tabControl1.TabCount + 1).ToSt
我创建了一个自定义 TabControl 和一个自定义 TabPage,如下所示: 自定义 TabControl 代码: public class MyCustomTabControl : TabCo
vim中是否有任何命令可以用来跳转到一个已经打开的选项卡,其中包含在选项卡中打开的文件。 在所有打开的缓冲区中也有完成的可能性。 例如,您在不同的选项卡中打开了以下文件。 readme.txt pom
我有一个由多个 TabPage 组成的 TabControl。这些 TabPage 之一包含 TreeView,另一个包含 DataGridView。我希望这些控件(和选项卡页)能够在不滚动的情况下将
所有,我想向 WinForm TabPage 添加自定义 RichTextBox。我尝试了下面代码所示的各种方法 TabPage tmpTabPage = new TabPage("Test"); t
在 Visual Studio 2010 的设计模式下工作... 我正在尝试在 tabPage 上获得水平滚动条。根据我在这里和那里阅读的理解,将 tabPage 的 AutoScroll 属性设置为
我想创建 tabpage 作为 userControl。 有什么办法可以解决这个问题吗? 最佳答案 添加一个 TabPage,并向其添加一个 UserControl... TabControl tab
我希望在设计时有一个带有控件的面板,但我想在运行时将此面板添加到我的 TabControl 的所需 tabPage。 我写了这样的东西,没有用:面板没有显示在标签页中。请帮助我。 panel2.Par
我正在尝试根据在选项卡控件上单击的选项卡页自动触发事件。 在我的窗体的设计模式中,当我单击选项卡时,属性框显示 Tabs System.Windows.Forms.TabControl 无论哪个选项卡
这个问题在这里已经有了答案: How can I disable a tab inside a TabControl? (26 个答案) 关闭 6 年前。 如何使 TabControl 中的 Tab
我想让用户通过拖放来重新排列 TabPages 顺序。此外,让用户能够将 TabPages 从一个 TabControl 拖到另一个 TabControl 会很酷。就像在 Firefox 和 Tota
我有一个应用程序在客户帐户上运行测试,以判断他们的服务是否正常工作。在运行测试的过程中,应用程序读取每个测试并检查它是否通过/失败/等等......它在tabPage本身上播放绿色复选标记/红色x作为
我想在我的应用程序上实现一个 MiddleClick-To-Delete 功能,就像大多数选项卡式环境一样: 它只是一个带有 TabControl 的单一表单,它有 2x TabPage:Active
我希望能够在 TabPage 上中键单击选项卡并将其从 TabControl 中删除,但即使启用了 HotTracking,我也不知道该怎么做捕获我中间单击的选项卡。 有办法吗? 最佳答案 您可以在
我有一个带有多个选项卡的窗口窗体。我想要一个“TabPage”类型的数组来保存每个选项卡,我想在声明时初始化它。我已经试过了 TabPage[] tabs = {Tab1, Tab2}; TabPag
TabPage 一直在创建,即使标签页已经存在于我的标签控件中。请考虑下面我的代码: void button1_Click(object sender, EventArgs e) { TabP
我有一个包含 TabControl 的简单表单,其中 TabPages 在运行时添加。我已经让所有这些都按我想要的方式工作几乎,但是当我将表单设置为自动调整大小时,它会忽略计算中的运行时 TabPag
类似于浏览器关闭选项卡的方式,我想知道您是否可以在 TabPage 的标题中添加一个按钮。 它会使用自定义 TabPage 渲染器吗 我可以在选项卡本身上添加一个按钮吗 最佳答案 您可以在此处找到示例
我制作了一个 TabControl 并想使用一些控件(标签、文本框、按钮和组合框下拉菜单)。问题是当我选择一个选项卡时,默认背景颜色与控件曾经所在的窗体的默认背景颜色不同。选项卡和控件的颜色太相似,看
我读了这个话题(Adding buttons to a TabControl Tab in C#)但我不明白为什么我下面的代码只向标签页添加一个按钮。我显然已经调试过 foreach 是否正常工作。
我是一名优秀的程序员,十分优秀!