ImG*_*reg 6 c# user-controls tabs winforms
我有一个ac#WinForm应用程序,我需要在运行时生成TabPages.理想情况下,我想使用VS设计器设计这些标签页,但似乎我不能直接这样做,因为我无法从工具箱中拖放它(有另一种方式吗?).
我有两个通用的tabpages,我将多次使用.一个包含一个简单的电子表格和一对文本框,另一个包含图形和几个文本框.这些页面最终会变得更复杂.我正在寻找最好的方法.我目前只是为每个标签页创建自定义类并将其设置为TabPage基类.例如:
public partial class SpreadsheetTabPage : TabPage{}
Run Code Online (Sandbox Code Playgroud)
我读到用户控件提供某种形式的替代方案,但我并不真正理解使用它与我的方法相比的优势.
所以要清楚,我想知道您认为开发这些自定义tabpages的最佳方法及其原因.如果相关,请提供基本代码示例.我的方法并没有真正造成太多问题,但我发现稍后在这些页面上添加内容会很困难,特别是在没有使用设计器的情况下.
感谢您的帮助!
小智 6
您可以创建自己的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一种非常复杂的方式,但是一旦你学会了它,你会被Visual Studio提供的强大功能所打动.您可以在MSDN中找到大量信息.以下是最常见的功能:增强设计时支持
小智 6
我带着同样的问题遇到了这个帖子。在思考这个问题之后,我意识到解决方案就在我面前。它实际上非常基本且优雅。
我所做的是创建一个名为 TabPageLibrary 的 UserControl。因此,我在其上拖动了一个 TabControl,并根据我想要的设置添加了页面。然后我将 TabPage 修饰符设置为内部。当我需要特定的扩展 TabPage 时,我只需调用 TabPageLibrary 类并获取特定应用程序所需的 TabPage。这使我能够在整个 winform 应用程序中重用特定的预配置 TabPage
private void PropertiesButton_Click(object sender, EventArgs e)
{
TabPageLibrary library = new TabPageLibrary();
TabPage propertyPage = library.PropertyPage;
this.tabControl.TabPages.Add(propertyPage);
}
Run Code Online (Sandbox Code Playgroud)
这确实解决了我的问题——也许它适用于您的应用程序。
如果您制作一个包含选项卡控件的用户控件,您可以看到设计器并根据需要进行设计,并且您可以添加一些附加代码,并且仍然可以将其从工具箱中拖放以使用它。
注意:在重建项目之前,您看不到用户控件(也看不到手动编码的类)
| 归档时间: |
|
| 查看次数: |
12527 次 |
| 最近记录: |