custom controls with sub components ownership - visual-studio

I have a Visual Studio designer enabled control which uses a collection editor to allow the user to create and edit sub components.
For example, I have a control which offers a readonly "List" property containing a collection of bindings (themselves components with a name property and an event). The user can modify this list of bindings at will and everything works pretty well. They can create and remove bindings without a glitch.
However, when they copy the control, the designer does not copy the binding, it still refers to the original binding components. Also, when they delete the control, it doesn't delete the bindings. It is as if the form has the ownership of these bindings, not the control.
I'm sure there is an attribute to use or some interface to implement, maybe even a custom editor trick to use, but googling for it has left me "feeling unlucky".
Thanks for any help you may have! :)

Related

Accessing Properties within the same project

I'm creating a ActiveX control. I've created a bunch of properties in a User Control and now I want to access those properties in the other modules, classes, forms, etc in the same project but I can't seem to find the correct syntax to do so?
Syntaxt I've tried:
connectString = user_control_name.DBConnectionString
connectString = Property("DBConnectionString")
connectString = Property(DBConnectionString)
connectString = Property Get ("DBConnectionString")
If you want to access a property on an instance of a UserControl on a specific form, you can do:
my_form_reference.my_usercontrol_name.my_property = <whatever>
If you want to access the instance of a UserControl that is in a UserControl, you will have to pass the reference to the UserControl via a property on the containing UserControl.
I generally recommend you don't pass UserControl references around. It is generally better to wrap up these controls, and not break abstraction.
If you want to access a property on an instance of a UserControl from inside the ActiveX Control project, I would recommend against it. The only way you can do this is by using module-level variables in a BAS module. This variable would potentially be accessible from every module in the project. You would have to set the variable in the UserControl_Initialize event of the control. However, I would strongly recommend you not doing this, because this creates an extra reference to your UserControl, which should really only have a reference from the containing Form or UserControl. This extra reference would mean that if the containing Form or UserControl was unloaded, the control would stick around in memory for the lifetime of your application, causing a memory leak. There are ways around this issue by using "weak references". But this is not supported in VB6, requires API hacks, and is prone to causing crashes.
I seriously would suggest you find some other way to do what you want. Perhaps you could explain exactly why you have this requirement.

Programatically setting control properties vs. using designer

Often, when I'm looking for information of how to set certain properties of controls (I'm using Visual Studio primarily, but this question does apply to any IDE), the examples I find normally involve programatically setting said properties.
Here's an example, using the DataGridView.DefaultCellStyle property on MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.defaultcellstyle(v=vs.110).aspx
So my question is: is it better for a programmer to modify these properties by code, or is it better to use the IDE itself to change them via the properties pane? I realize that doing it the latter way will result in auto-generated code anyway, but it will be "somewhere else" in relation to the main bulk of user-generated code.
It depends on your requirements. If the app you are developing is planned to be dynamic, then it is a good idea to do it the way it is described in the reference you provided. For example, you may create a class containing the method which will set the basic settings of the gridview, so on every page you may just call this method and pass to it the gridview as a parameter instead of repeating setting the grid properties on every page.

What ListBox like control is used in Collections Editor of Visual Studio

I need to create a from which uses the same ListBox as the one from Collection Editor of Visual Studio (The ListBox under the Members label). Please, explain exactly which WinForms control is this and which of its properties are set?
You can see the control I am asking about under the Members: label of every collection editor form in design time of Visual Studio.
Thank you.
Hopefully this can get you started. There's other (probably better...) samples out there, but this is a basic starter which can help you get the concept:
http://msdn.microsoft.com/en-us/library/9zky1t4k%28VS.90%29.aspx
Quote:
This example shows how to create a
control named ContactCollectionEditor
that implements a custom collection
editor. The example shows how to
specify the exact type of the object
that a page developer can add to the
control's collection property when
using a custom collection editor. You
associate a collection editor with a
collection property (or the type of
the property) by applying the
EditorAttribute to the collection
property of the control.

Best practices of building composite custom controls WPF

Can You tell me about subj?
For example I need to create new custom control that must be derived from datagrid and toolbar. And I want that new control to expose/propagate properties of base controls in order they to be accessed easily. The only way I know is deriving a class. Then descendant automatically gets all properties of ancestor. But multiple deriving in C# is prohibited, so I don't know how to expose properties and other behavior of second control... Styling and templating of such custom control is also needed.
Thanks!
In WPF inheritance is "out". ;O) Actually, it was complicated before WPF already, but with WPF you get various really powerful alternatives.
For the basic control I thing you'd better go with composition, create some usercontrol and make it contain a ToolBar and a DataGrid. You can then expose these as public properties, if you need to manipulate them from outside.
For special feature additions, attached properties are a very versatile mechanism.
Watching a window from windowsclient.net is a good place to start.
Make sure you understand dependency properties well.
Reading wpf blogs is my best guest for what to do then.

How can I add an editable ComboBox to Vista's Common Item Dialog?

Vista introduced the new Common Item Dialogs for opening and saving files that supersede the old Common File Dialogs. Custom controls can be added by utilizing the simple IFileDialogCustomize interface. One of them is a standard ComboBox which is non-editable. Is there any way to create an editable ComboBox or modify an existing one (by adding the CBS_DROPDOWN style)?
Unfortunately, you can't. There is no guarantee that the controls you add with IFileDialogCustomize are Win32 controls. (And in fact, I believe they aren't.) Since they aren't Win32 control, there's nothing to set the CBS_DROPDOWN style on. Sorry.

Resources