I have a bunch of buttons in the current application I am working on. The client doesn't like to use a mouse, so I was just asked to set the buttons to change colors upon Enter and Leave. I have a few options here and wanted an opinion as to the easiest way to quickly get this taken care of.
1 - Create a generic event handler for Enter and Leave and use AddHandler statements on each form for each button. This seems to be the option that would take the longest amount of time to implement.
2 - Create a User Control with the necessary code and then use a Find and Replace. This seems to be the quickest and easiest.
3 - I thought I could create a Component that inherits from Button that was more or less just a wrapper around the button control. At design-time, I could use the standard button control and then at run-time I could replace the standard buttons with my Component. However, I have been toying around with this one and haven't been able to get it to work. (If someone has some code for creating the component and implementing it, I would like to see it.)
Any assistance/opinions will be greatly appreciated.
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
Me.BackColor = Me._focusedBackColour
MyBase.OnEnter(e)
End Sub
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
Me.BackColor = Me._normalBackColour
MyBase.OnLeave(e)
End Sub
End Class
You can then add instances of this class to your form from the Toolbox, where it will be automatically placed for the solution in which it is declared, or else you can do a Find and Replace on existing Button's.
Just a note on terminology. If you inherit a class you are not creating a wrapper. Does a Dog wrap an Animal? No it doesn't. It just specialises the type to a greater degree. Creating a wrapper means defining a type that has an instance of another type inside. The two objects are distinct, with one enclosing, or wrapping, the other. Also, a "user control" is a type that inherits the UserControl class. If you inherit a class like Button, or ideed Control itself, you are creating a "custom control".
You can then add instances of this class to your form from the Toolbox, where it will be automatically placed for the solution in which it is declared, or else you can do a Find and Replace on existing Button's.
I am working with 2005 Express. I do not see it added to my Toolbox and do not see how to add it.
PS - Just to get things straight, you are just creating a class here and not a User Control, correct?
I would call it an extension of a user control using a class.
I would call it a class.
inheriting from the button class does nothing different than the standard button class, plus whatever functionality you add to it in the child class.
A user control follows a different inheritence chain, specifically inheriting from System.Windows.Forms.UserControl, which in the chain goes like this:
I will start at control, which is the place in the inheritence chain where a control and usercontrol split
Regular Winform Control (like inheriting from a button)
I am working with 2005 Express. I do not see it added to my Toolbox and do not see how to add it.
PS - Just to get things straight, you are just creating a class here and not a User Control, correct?
Like I said, it is added automatically for the solution in which it is declared. You have to compile the project that contains it first of course. If you want it available for other solutions then you'll have to add it to the Toolbox manually.
I thought that was the way it was supposed to work, but I am missing something.
Let's go over what you have going on. You have a Solution with two projects (Do they have to be in separate projects?). One project contains the Class the other project contains the form. Both projects are created as "Windows Application"?
I have tried all kinds of iterations and have not yet seen the "Project Name" Components section get added to my Toolbox.
I either have the project types incorrect or something like that.
If you want the types that a project contains to be usable by other projects then that project has to compile to a DLL. You aren't seeing your button class in the Toolbox because the project containing the form cannot see it. Change the output type of the project containing the button class to DLL and it will appear in the Toolbox when you're working on the project containing the form. Having said that, noone said anything about having to create a second project. You don't create new projects for other classes you define do you, so why this one? If you only intend to use this class in the one application then declare it in that application's project. If you will or may want to use a class in multiple projects then declare it in a library project, which you can then reference from as many other projects as you like.
Ah ha! I tested this out on my home PC and it worked fine. So, like any other inquisitive person, I started comparing the setting on the two machines. Under Tools -> Options -> Windows Forms Designer -> General there is a setting "AutoToolboxPopulate". I had set this to False on my laptop due to my understanding that it was supposed to make the UI faster. It should be set to True.
Well... When you count the hours I have spent trying to figure this whole thing out, I think that option just cost me far more time than it saved me.
Thanks to all who posted with assistance and were willing to stick with this!!