Category Archives: C#

Custom User Controls and Events in C#

One of the interesting features of C# is the ability to add custom user controls to your forms. If you have some specific functionality centered around a group of form controls that you use repeatedly, you can save it as a custom control in a class library and then add it to other applications. This is a great example of code reuse and saves a lot of time.

I’m starting to get the hang of creating demo videos with Camtasia and other tools and my latest videos show how to create user controls and implement them within Visual Studio.

Part 1:

Very often, you’ll want your custom controls to communicate with the host application, passing data back and forth and one way to do this is to have your control raise custom events at specific times.  The host application listens for these events and responds as it needs to.  This is a very powerful ability that enables your classes to interact in more dynamic ways.  Once you understand how to raise and catch custom events, you’re no longer dependent on the C# event model and your applications can signal and respond to any condition you want them to.

The second video builds on the first part to show how to add custom events to your classes.

I encourage you to follow along with the videos and try creating the controls yourself. Also, experiment with your own ideas so that you’ll better understand the concepts.

If you would like to read more about creating custom events in C#, you can find more detail on the Microsoft website at the following links:

How to Raise and Consume Events
https://docs.microsoft.com/en-us/dotnet/standard/events/how-to-raise-and-consume-events

Handling and Raising Events
https://docs.microsoft.com/en-us/dotnet/standard/events/index

 

How to Convert Between Roman and Arabic Numerals in C#

The Challenge

My latest challenge to my database programming students was to create a C# class that would perform various number conversions, starting with Arabic numbers (0 to 3999) to Roman numerals and vice versa.

I required that all public functions in the class needed to be declared as static, so that they could be called from outside the class without having to create a new instance. Also, the class was to be completely portable;  it must not use any functions such as message boxes that would prevent them from copying the entire class into any other type of project and referencing it.  Of course, all code must also have error handling and commenting.

My students came up with various solutions and I think some had more help from StackOverflow than others.  I know that one of them learned the hard way not to search for “XXX” on Google.

One popular strategy was to use the C# Dictionary class to hold the Roman characters and their equivalents.  I don’t always get around to writing up my own solutions to these challenges before I assign them, so I happily swiped that strategy for my own code which was otherwise written from scratch. I ended up using the SortedDictionary class instead.

Continue reading