C# Custom User Controls and Events - Part 1
Vložit
- čas přidán 26. 07. 2024
- For more videos and courses, check out my Teachable online school at:
comeau-software.teachable.com/
A demonstration of how to create a custom user control in C# and implement it within a host application. Check out Part 2 ( • C# User Controls and E... ) for an explanation of how to create custom events to enable the user control to communicate with the host application.
Would you like to support videos like this?
Buy me a coffee - bmc.link/acomeau - Věda a technologie
I'm a beginner in C#. And you got my respect.
couple of comments from an old timer.
I have been doing UI control stuff since before designer modes existed so take this for whatever it is worth.
The Project type should be "Windows Forms Control Library" That way you can develop your control without concern of clobbering your program.
In the"Windows Forms Control Library" leave the default UserControl1 and add your new CustomUserControl, build then drop your CustomUserControl in design mode onto UserControl1 and you can see it work in realtime when you run it. Add listeners/events to the UserControl1 constructor so you can check them firing as expected. When everything is working you can delete the default UserControl1. Build it and add your control project or dll to project you want to use them in.
PRO Tips:
keep 2 copies
1. (development) this one that has the UserControl1 and have all my events, interactions wired up for testing and
2. (production) a copy of development with the testing stuff removed.
3. Copy your_control.dll into the project and reference that... don't include the controls project... when you include the control project you potentially can create application breaking changes in the control. When you make changes
.
4. I would add the control programmatically instead of dropping it in design mode for the safest possible compatibility. If your a newbie drop it from the toolbox set the properties so it looks right. Then programmatically instantiate it setting the properties then remove the the one you dropped from the toolbox. You can do this very quickly if you copy the settings from the Designer.cs in the InitializeComponent() method [name of your control in the designer].
5. The moment visual studio starts acting abnormally in design/run time mode check your control.
6. Do a new Windows Forms Control Library for each new control.
7. Version control your controls to death.
8. If you have dropped your new control from the toolbox then later change to a newer dll without removing that control first be prepared for bad things to happen. In this case bad things happen often. So backup before you make the change just in case.
9. you can never backup enough. Incremental development strategy is a must for controls.
Wow, I did not know that I can use a single event for multiple controls. Good stuff.
Thank you
Ah so that's how to do it. Very helpful thanks...
Awesome. Great tutorial.
❤️🌹❤️
thank you very much
thank you so much
great thanks
Nice video. Clear and concise. I wish you had gone one step further, however. I am working on a gauge user control that I would like to use in multiple programs. I am struggling with permanently installing the gauge in the toolbox like the button is. Otherwise, I would like to do an "Add Existing" and get the control to show up the toolbox which is also a problem. If you have any ideas I would appreciate a video or response. Thanks.
I don't know if it's possible to permanently add it. Try this - docs.microsoft.com/en-us/dotnet/framework/winforms/controls/walkthrough-automatically-populating-the-toolbox-with-custom-components
The custom control was not appearing in the toolbox for me.
The solution was going to Tools > Options > Windows Forms Designer > General and changing Toolbox > AutoToolboxPopulate to True
This is only c# or WPF user control? Thanks
It's not WPF but this example is specifically C#. You should be able to do the same with other .NET languages.
I've been having problems with interacting with the controls after I've created my customusercontrol,but now I noticed that u wrote the code for it whilst being in the user control n not after you dragged and dropped...I don't know if I'm still not getting it please help me out,I've got a project and I don't want to use multiple forms
The main reason I did that was because I was pointing the three controls to the same event handler. It shouldn't really make a difference in working with your controls. Try checking all the settings in your form for anything that might mark it as read- only or affect the accessibility. You could also try following along with my example to see if it works for you.
@@ComeauSoftwareSolutions Alrighty ,thank you very much 😁,I really appreciate it Mr. Andrew