Custom Controls for Desktop Projects
How to make some custom controls for your desktop apps. Start by creating a simple desktop project.
And I’m going to add a class to the project. And this is going to be a subclass of the PopupMenu. We’ll call it MonthPopup and it will just have its values pre-populated with the months of the year, with the current month selected. So I’ve got my class here, with the super set to PopupMenu. I can now add an event.
I’m gonna add the Open event where I can set up the values. This code is going to add the rows for the month and then it’s gonna get the current date and use that to find the current month so it can select the current month in the list. And that’s it. Now I can drag that control onto a layout and use it instead of a regular PopupMenu. And you’ll see it becomes pre-populated with the months and the current month is the one that is selected.
Now I’m gonna do another custom control. This one is going to be a subclass of Label. It will let you click on it and jump to a URL so we’ll call it LinkLabel. And I need to add a property for the URL that you should go to so we’ll just create a property of a String on there. And there’s a few event handlers that I’ll want.
MouseDown, MouseEnter and MouseExit. So I’ll select those MouseDown is called when you click on the label and when that happens we’re going to show the URL which will open the URL in the user’s default web browser. And for the enter and exit. I will simply change the color of the text as the mouse hovers over or outside of the label itself. So if you hover over the label, we’ll change the text color to blue.
And when the mouse cursor leaves the label set the color back to black. The last thing I’m going to do here is I’m going to make it so that the URL property we added actually shows in the Inspector. So I’ll click Inspector Behavior here and you can see all the available properties for the Label. And the property we added, URL, is at the bottom so I can check it. And that means it will now show up on the Inspector when you drag a LinkLabel onto a layout.
Now I’ll do that I can still give the label any text I want. And then looking over in the Inspector you can see the new URL property where you can enter the URL you want to go to when the label is clicked. I run this and you can see when I hover over the color changes to blue and back to black. And when I click it the default web browser opens at the location.
And that’s how you can subclass controls for your desktop projects.