Re: property exists (OnPaint followup)
Peter Osucha <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
I'm annoyed (in many ways) that the Infragistics controls aren't more clearly thought out on things like this. I mean, almost all of their controls have an 'Appearance' object that is useful (it groups many 'neat' look features together for the control). However, the properties that the Appearance object holds that are essentially 'pass-throughs' for the underlying control properties (like BackColor, ForeColor, font, etc.) should still exist. I understand the thought behind what you are suggesting - however, a slight twist. As I mentioned, there are plenty of Infragistics controls that could be used as a canvas for what I'm doing (just as there are several VS controls). Every Infragistics control that could be used as a canvas will have an Appearance property. It would be much cleaner to try to discover not whether the canvas control can be cast to the many Infragistics controls but instead to discover whether the canvas control had an 'Appearance' property. Any good way to do this? Peter -----Original Message----- From: Discussion forum for developers using Windows Forms to build apps and controls [mailto:[email protected]] On Behalf Of Fabian Schmied Sent: Wednesday, May 23, 2007 10:46 AM To: [email protected] Subject: Re: [DOTNET-WINFORMS] property exists (OnPaint followup) > As a follow on to my painting question, I need to obtain the 'BackColor' > of the hosting 'canvas' control. For the VS controls, this is simple > since there is a BackColor property. For other controls that changed > the name of the 'BackColor' property or that have the BackColor property > another level deep in their object model, it isn't quite as simple. > > For example, the infragistics control I want to use as the canvas > exposes the BackColor property inside an 'Appearance' property. > > So for the System.Windows.Forms.Panel object, I get the BackColor from > > Color myColor = _canvas.BackColor; > > For the Infragistics.WinGroupBox object, I get the BackColor from... > > Color myColor = _canvas.Appearance.BackColor; > > If the 'Appearance' property exists for the object assigned to _canvas, > then I will use it. So what I am asking is, how do I check to see if > the object '_canvas' has a property called 'Appearance'? You'll notice when the C# compiler gives an error. In a programming language like C#, if you have a canvas variable of type "Control", you can only invoke methods directly supported by the Control class. I.e.: Control _canvas = ...; Color myColor1 = _canvas.BackColor; // compiles Color myColor2 = _canvas.Appearance.BackColor; // gives a compilation error In order to access the Appearance property, the "_canvas" variable must be of a type which supports that property. In C#, you need to cast the object to such a type. If you are not sure the cast will succeed, you can use the "is" or "as" operators to test it: Control _canvas = ...; ... Color myColor; // test whether _canvas implements the InfragisticsControl type InfragisticsControl _infragisticsControl = _canvas as InfragisticsControl; if (_infragisticsControl != null) { // yes, so use Appearance myColor = _infragisticsControl.Appearance.BackColor; } else { // no, use Color myColor = _canvas.BackColor; } (Note that I don't know Infragistics' controls, so the solution I'm proposing might not work exactly that way.) Regards, Fabian