简体   繁体   中英

Create a template in WinForms and use it as a control

I'm pretty new to WinForms and I need something for a project: I'm writing a test program for a device. The device sends some data about itself and I have to output some information about it in a tab and on a chart.

Here is the twist: the device can be a double, triple, or n-uple device which means I'll have to output n tabs and n charts. For this I would like to have a "template" with a tab and a chart and put one for every sub-device, one under another in my winforms window. How do you think is this possible? Do you have a better idea?

I don't know if I'm very clear so I added a picture to explain what I want.( https://i.stack.imgur.com/Nd00g.png ) Thank you

I tried to search the web for templates in winforms but found nothing that fits what I want.

You can create your own custom class that inherits TabPage and adds whatever child controls you want. Here's an example I wrote some time ago for a tabebed web browser app:

Public Class WebBrowserTabPage
    Inherits System.Windows.Forms.TabPage
 
    Private _browser As New WebBrowser
 
    Public ReadOnly Property Browser() As WebBrowser
        Get
            Return Me._browser
        End Get
    End Property
 
    Public Sub New()
        Me._browser.Dock = DockStyle.Fill
        Me.Controls.Add(Me._browser)
    End Sub
 
End Class

That code is in VB but hopefully the principle is clear. I can provide a C# translation of required. You can then add an instance of that class to a TabControl instead of a regular TabPage .

If you only need one child control - it sounds like you only need a Chart - then you're good to go. If you need multiple child controls then you should probably create a user control with all the children first, then add just an instance of that user control to the custom TabPage .

You could also add more functionality by creating a custom TabControl if you wanted. I won't go into specifics unless you need them but you can check out my thread on the tabbed web browser here .

Here's a C# translation of the code above, updated a bit too. The original was written almost 15 years ago.

public class WebBrowserTabPage : TabPage
{
    public WebBrowser Browser { get; } = new WebBrowser { Dock = DockStyle.Fill };

    public WebBrowserTabPage()
    {
        Controls.Add(Browser);
    }
}

Well, unfortunately windows Forms are based on inheritance.

So you cannot compose new behaviour in, but if you don't mind inheritence, simply create a child Form And then use a Panel that will work as your base template, where you can add your chart, and your data.

Then you can either create as many children as you want, one pr type, OR you can compose different behaviour onto the panel, as you like.

I prefer the composing solution myself, as it at least attempts to be modern, but the problem is Forms are not really build for composition, so it would strictly speaking be better to continue in the inheritance paradigm, so everything is uniform.

However, Testing is much harder on inheritance. So I would like composition for that actually. Yes. Composition, is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM