Home > OS >  Create a template in WinForms and use it as a control
Create a template in WinForms and use it as a control

Time:11-28

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 datas about itself and i have to output some informations about it in a tab and a chart. Here is the twist : the device can be a double, triple, or n-uple device wich 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.

CodePudding user response:

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.

CodePudding user response:

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