Create a windows form control from another class

swodniW 141 Reputation points
2021-07-09T10:19:48.677+00:00

hello everybody.
How to create a control from another class
class form1 : form
{
}
class it's_a_class
{
//create a control in form1
}
thanks in advance

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2021-07-09T12:54:42.68+00:00

    Hello,

    One would think this a simple task while there are things to consider like

    • Position of a control or multiple controls
    • Events for the control as simply place a control on a form generally we want to interact with it except perhaps a label and maybe a image.

    I put together two code samples that are pre-done.

    First project/code samples creates a single button with events for click, create multiple buttons with interaction with a ListBox and gets captions for buttons from database which could also come from say a text file etc.

    Second code sample is rather complex which works with a custom control - grouped-radio buttons.

    Find these code samples in the following GitHub repository. Note I used Visual Studio 2019 to write these code samples, one with classic framework and the other with .NET Core Framework.

    113373-createbuttons.png

    113339-radio.png

    0 comments No comments

  2. Sam of Simple Samples 5,571 Reputation points
    2021-07-09T18:10:55.55+00:00

    Perhaps you just want to use a UserControl. If you can do what you need to do using a UserControl then that would be quite easy.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.