Extending UI Component Class in C#/VS2010 - c#

I've only just started using C#/VS this past week and wondered how to do something which, I think, should be quite simple:
What I want to do is extend the class used by a UI component and therefore implement my own methods in it - just for one instance of a UI component though. If I was using xcode/objective c I would normally just change the class name of the component in interface builder and it would become an instance of that class which would in turn extend the original UI class.
How do I do something comparable using C#/Visual Studio?

You can take any component class in Windows Forms and subclass it. Visual controls all derive from the Control class and you can do so as well.
If your component is a User Control (i.e., it derives from System.Windows.Forms.UserControl), it should automatically appear in the Toolbox after you build the project. For other components, you can add them to the Toolbox by right-clicking on the Toolbox and select Customize Toolbox, selecting the .NET Framework Components tab, clicking the Browse button, and selecting the DLL with the control.

Remember that all (or most) UI components are classes, so they can be "extended" just like any other class.
Some will have virtual members you can override to take special actions. In all cases, you can add properties, methods, and events to the components.
Once you've created and built them, you can use them from the Toolbox, just as though they were the "built-in" .NET components.

Related

What is the "Control.Site" Property?

In Windows Forms applications, controls in the System.Windows.Forms library have a property named Site.
What is this property’s job in Controls?
The Site property is inherited from Component, and is very much like the Parent property of a Control.
The main difference between Parent and Site is that the value of Parent can only be a Control, while Site can have a non-visual container assigned to it.
The Component base-class is used for those non-visual tools in the Winforms designer toolbox. For example the System.Windows.Forms.Timer which can be dragged onto a Form. The PropertyGrid can be used to set its properties and assign event-handlers, all from the designer without writing a line of code.
The idea behind the System.ComponentModel classes is to provide an API for Software libraries to take advantage of design-time capabilities of an IDE such as Visual Studio. It caters to RAD (Rapid Application Development) concepts where general-purpose or generic components would take advantage of the API. For example to expose extra information about a property in the bottom section of the property-grid, or even create complete custom editors.
If you want to dive deeper into the internals you could look at Programming with Components, or if you want a quick overview, I guess Class vs. Component vs. Control may be a good starting point.
Sites bind a Component to a Container and enable communication between them, as well as provide a way for the container to manage its components.
Sites can also serve as a repository for container-specific, per-component information, such as the component name. For more information about components, see Programming with Components.
Notes to Implementers
To be a site, a class must implement the ISite interface.
Reference:
https://msdn.microsoft.com/en-us/library/system.componentmodel.isite.aspx

bogus component being added to WinForm

Many times when I edit a WinForm in my (inherited*) project an instance of a particular component is added -- and I'm not intentionally doing anything to add it. What could trigger the form designer to always think that a component needs to be added?
inherited project: meaning that I didn't design/implement most of it. There may be components or tools in use that I am not aware of. It does use DotNetBar; but the component being auto-added is not from this library.

Inheriting Existing ActiveX Control

I am currently working with a 3rd party ActiveX Control (an editor for topographical data distributed by Cadcorp SIS). I have to do some fairly complex stuff with it in a VB.NET (framework 4) program and am finding that the API that comes with the control is quite limited.
As of now I have made a custom control which houses the control and acts as a wrapper for it to allow me to extend the API, which works fine, but What I would really like to do it give the control more events so that I can monitor what is going on with the data more closely.
I'm not sure how to go about doing this though...
I tried inheriting from the control and I can extend it just fine, but I can't figure out how to reuse it after that. Is there some way I can inherit the control and get it to appear in the toolbox so I can just drop it onto a form? Or do I have to load it programatically? If so, how can I do it?
Any pointers, examples, tutorials or alternative ideas as to how to do this kind of thing would be welcome.
If you are not seeing your control class in toolbox it doesn’t mean that you can’t use it just modify code from your form's designer to use your inherited class
Or
You can drag-and-drop your assembly (dll) from Windows Explorer to the
toolbox in VS.NET. Or you can right click on the toolbox in VS.NET and
choose Add/Remove items. Is this what you are looking for?
Also
<ToolBoxItem(True), ToolboxBitmap("MyNamespace.MyClass.bmp")> _
Public Class MyClass

Microsoft Kinect SDK 1.6 missing KinectSensorChooser component?

I am currently following this tutorial
http://channel9.msdn.com/Series/KinectQuickstart/Setting-up-your-Development-Environment
But at around 9:50 he uses a component called KinectSensorChooser which is not available anymore in the latest SDK 1.6 version because i read Microsoft's SDK History log that states
"We’ve taken KinectSensorChooser, formerly part of the WpfViewers and split the logic and UI into two different classes: KinectSensorChooser and KinectSensorChooserUI in Microsoft.Kinect.Toolkit.dll.
KinectSensorChooser could be used in non-WPF scenarios as it is logic only, no UI."
Source: http://www.windows7download.com/win7-kinect-sdk/history-lxqvgakz.html
Since the Microsoft.Kinect does not include the KinectSensorChooser component i added the Microsoft.Kinect.Toolkit reusable component which does include the KinectSensorChooser but the component is not showing up in the toolbox, i tried adding it manually by right clicking on the toolbox and selecting choose items then WPF components then locating it but it imports as a UI (KinectSensorChooserUI) and if i drag it onto the form the component disappears from the toolbox, i am using Visual Studio 2012 Ultimate on Windows 8
I've never added the KinectSensorChooserUI control to the toolbox in Visual Studio. The need to do so really isn't there.
If you feel obligated to do so, I found a Adding Your WPF Control To The Toolbox blog post that might be of use. Haven't tried it myself, so I can not promise it will work.
I personally do not use the UI component of the KinectSensorChooser. Unless you really plan to be turning the Kinect on/off or switching between multiple Kinects manually, it doesn't really serve much of a purpose. It does provide some feedback, but that can done in other more aesthetically pleasing ways.
To use the KinectSensorChooser you simple need the following in your main class:
private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();
public MainViewModel()
{
// other initialization here
_sensorChooser.Start();
// more initialization here
}
You now have an active KinectSensorChooser, just minus the UI.
If you are dedicated to using the UI component, forgo trying to add it to the toolbox and just do the following:
Add the Toolkit project or a reference to the .dll.
Add the namespace to your Xaml so that you can reference the controls in your markup. xmlns:kt="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit"
Add the control to your visual tree
<kt:KinectSensorChooserUI x:Name="SensorChooserUI" />
Your code behind would declare the namespace, initialize the KinectSensorChooser and set up any events you want.
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();
// somewhere in your constructor, or other init function
this.SensorChooserUI.KinectSensorChooser = _sensorChooser;
_sensorChooser.Start();

List Box, c# Visual Studio 2010

One small question. I know the toolbox in Visual Studio has all the necessary components, but I was wondering if we could introduce our own "custom-made" tools. For example, the listbox tool. I've created a "SpeechListBox" class in which I've mentioned all the methods that I want my listbox to use, but when I'm in the "SpeechListBoxApplication" class (where I wanna use this listbox), the listbox with my methods doesn't show up, instead the general listbox tool that doesn't have my methods shows up.
What I want is to write something like private speechListBox1 SpeechListBox; and somehow introduce a visual form to the design class without confusing it with the toolbox's listbox. Making the program realize that I want this type of list box with all the extended methods, not the general toolbox type.
Is there any way to either make our own listbox or add methods to the original listbox tool?
Well, if you derive your SpeechListBox from a class that either is or derives from System.Windows.Forms.Control, when you compile your project it will show up in the Visual Studio control toolbox.
If you aren't sure which class to derive from, you'll have to make some decisions. If you want to hand-draw everything yourself, derive straight from Control itself. If you want to build a control is is a composite of other controls, consider deriving from UserControl. You don't explicitly list exactly what you're trying to do with your SpeechListBox, but you may want to consider just using a ListBox but supplying it with custom drawn list items. You could do this by making your class derive from ListBox or just configuring a ListBox to do what you want right in the form on which the listbox resides.
Is your code in separate project? Then you have to add that project to your SpeechListBoxApplication project's references in the solution explorer.
Otherwise your inherited control (public class SpeechListBox : ListBox) should show up, when in the GUI Designer in the toolbox in either the Common section or a section labeled after the project.

Categories

Resources