Create custom tab in C # WinForms?

I have a C # WinForm application that I need to create TabPages at runtime. Ideally, I would like to create these tabs using the VS designer, but it seems that I cannot do this directly, since I cannot drag it from the toolbar (is there any other way?).

I have two general tabs that I will use several times. One that contains a simple spreadsheet and a couple of text fields, and the other a graphic and a couple of text fields. These pages will eventually become more complex. I am looking for a better way to do this. Currently, I just create custom classes for each bookmark and set it as the base class of the TabPage. For instance:

public partial class SpreadsheetTabPage : TabPage{} 

I read that user controls offer some form of alternative, but I really don't understand the benefits of using this method against my method.

So, to be clear, I want to know what you think is the best approach to developing these customizable tablets and why. If necessary, provide a sample base code. My method does not actually cause too many problems, but I see that adding things to these pages will be difficult later, especially without using a designer.

Thanks for your help in advance!

+6
source share
3 answers

If you create a user control that contains your tabcontrol, you can see the constructor and design it as you wish, and you can add additional code and still drag it from the toolbar to use it.

Note. you cannot see the user control (nor your class with manual coding) if you rebuild your project

+3
source

You can create your own TabControl designer and implement the functionality you need during development. Here is the code for the simplest version of such a constructor:

 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); } } } 

It seems rather complicated, but once you know it, you will be impressed by the powerful features provided by Visual Studio. You can find a lot of information on MSDN. The most common features are described here: Improving development time support

+6
source

I ran into this problem with the same question. Thinking about the problem, I realized that the solution looked in my face. It is actually quite simple and elegant.

What I did was create a UserControl called TabPageLibrary. Accordingly, I dragged TabControl onto it and added the pages based on the setting I wanted. Then I set the TabPage modifier to internal. When I needed a special advanced TabPage - I would just call the TabPageLibrary class and grab the TabPage that I need for a particular application. This enabled me to reuse a specific pre-configured TabPage in a winform application

 private void PropertiesButton_Click(object sender, EventArgs e) { TabPageLibrary library = new TabPageLibrary(); TabPage propertyPage = library.PropertyPage; this.tabControl.TabPages.Add(propertyPage); } 

This certainly solved my problem - maybe this will work for your application.

+2
source

Source: https://habr.com/ru/post/909831/


All Articles