gpt4 book ai didi

c# - 在 C# WinForms 中设计自定义 TabPage?

转载 作者:太空狗 更新时间:2023-10-29 21:10:03 32 4
gpt4 key购买 nike

我有一个 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com