Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am developing a game project in c#. I want to create 3 to 4 usercontrols to be used in my project. I want to create usercontrols in the winformscontrol library and then use it in another windows forms project by adding the reference of winformscontrol library.

My usercontrol1 has a label1 on it. I want to create an eventhandler that could perform same action whether I click on label1 or I click on usercontrol1.

I do not understand how to do it. Anybody please, help me.

I have no idea of how to get usercontrol in toolbox while creating a new windowsforms project.

What I have tried:

C#
using System.ComponentModel;

namespace WinFormsControlLibrary1
{
    public partial class singlepattern : UserControl
    {
        
        [Browsable(true)]
        public event EventHandler UserControlClicked;

        public singlepattern()
        {
            InitializeComponent();
            //after intialize compoment add same handler for all three controls
            this.Click += ControlClicked;
           
            this.label1.Click += ControlClicked;
        }
        [Browsable(true)]
        public Color label1BackColor
        {
            get => label1.BackColor;
            set => label1.BackColor = value;
        }
        [Browsable(true)]
        public Size label1Size
        {
            get => label1.Size;
            set => label1.Size = value;
        }

        private void label1_Click(object sender1, EventArgs e)
        {
            
        }
        
        //this method will "catch" all clicks
        public void ControlClicked(object sender, EventArgs e)
        {
            //raise event
            UserControlClicked?.Invoke(this, e);
        }
        public void UserControl_Click(object sender, EventArgs e)
        {

        }
    }
}


C#

Posted
Updated 29-Dec-23 22:17pm
v2

1 solution

Assuming that you are using Visual Studio :

Step 1: Create a Custom Control
From within your Windows Forms game project, right-click on your project in the Solution Explorer, select "Add" and then choose "User Control" or "Custom Control"

User Control: A composite control that is created by combining existing controls
Custom Control: A control that you create from scratch

Design your custom control by adding and arranging controls on the form.

Step 2: Compile the Project
Ensure that your project builds successfully. This is necessary for the control to appear in the toolbox.

Step 3: Add the Control to the Toolbox
Right-click on the toolbox in Visual Studio.
Choose "Choose Items..."
In the "Choose Toolbox Items" dialog, go to the ".NET Framework Components" tab.
Click "Browse..." and locate the compiled DLL or EXE of your project (typically found in the "bin\Debug" or "bin\Release" folder of your project).
Select the DLL or EXE file, and click "Open"

Step 4: Verify the Control in the Toolbox
Once you've added the control to the toolbox, it should appear in the toolbox window. You can now drag and drop your custom control onto your forms as you would with any other standard control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900