Custom Control Appearance - winforms

I created my own user controls that inherit from the standard .net controls (for example MyTextBox : TextBox). MyTextBox has within it some custom logic, and also sets some style properties (eg colour). I build the project that contains these controls, and they get added to my toolbox. I then drag them onto the windows form designer. The problem is that when I drag them, the windows form designer automatically includes the style definition for the control that i dragged. For example
this.myTextBox1.BackColor = System.Drawing.Color.Gray;
Now, if later on during development I decide to change the colour for all instance of MyTextBox in the solution from Gray to White, I cannot simply go to the MyTextBox control code, change it there and rebuild. The change will not be applied to existing text boxes, since this property will be overridden in the forms designer! What is the best approach to handle such cases?

you can control the designer code generation with an attribute ... http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx

Related

About custom control usage

I want to understand its logic and to modify. This library is refactored based on the open source library. I'm not very familiar with customization. Of course, I'll study hard.
How is it created automatically?
How is it grouped with controls, and where are properties controlled?
The Library: https://github.com/kelicto/KeLi.TreeListViewKit
How to Test: create a new form and add a TreeListView control.
Not sure i fully understand the question but:
WindowsForms comes with a UI design via Visual Studio. Users drag controls from the designer onto the target form. Each component on the form is clickable, and there is an associated Properties window; this is where values for the properties can be altered. The code in InitializeComponent, whether it be a form, or a custom control is automatically generated by the forms designer. e.g.: when you first start up there is a blank small form with no components.
Drag a textbox control from the left hand side and place it on the form. The form now contains a textbox. Click on the textbox and you can alter the properties of the textbox (name, value, width, even event handlers). Each change will alter how InitializeComponent works.
So if you wanted this custom component on your form you will need to compile the assembly it belongs and add the assembly so it can be referenced by the forms designer. Once this is done your TreeView component will be available to be dragged & dropped onto the form. Do this and you will also be able to set its properties.
Even custom components come with a designer piece; so if you were designing a component from scratch you could still drag / drop components onto that custom control and same as a form the implementation of InitializeComponent will change according to the components dropped, and the properties you set (and their location, anchoring etc which can be done on the main forms designer). I wouldn't recommend building a WindowsForms app without the designer, not that it cannot be done; ultimately it's code at the end of the day. But it's a lot more awkward to do without the visual designer component.

Quickest way to inherit a standard control in WPF?

I just want to wrap a standard control with some more additional properties (look stay the same, I don't want to do theming in first stage).
Can I just inherit from this standard control instead of UserControl or Control ? In fact I read it is obligatoryb to use Custom Control Project Template and not UserControl ontrol Project Template. Why ?
Update: I try with a Custom Control Project and inherit from the standard slider but I have nothing show up visually ! Now what should I do to have the same visual slider as the standard one ?
I know the difference between a user control and a custom control but in practice how do you do when you just want ONE single standard control ? How will a slider for example resize AUTOMATICALLY if I encapsulate it inside a User Control instead of a Custom Control ?
A custom control is a single control and can derive from another control, this would support styling. A UserControl is a composite control out of many different controls, and as a whole, doesn't support styling (the parts do however).
If you want to add features of any kind to an existing control, derive from it. If you want to pack several controls together to make it easier to handle them (you could still add DP's to it), use a UserControl.
A custom control alone won't do anything related to resizing etc, that is dependent on the settings you supply to it from the outside (ie. HorizontalAlignment, VerticalAlignment and others) when you used it in a container. The custom control should inherit the default template from the base class unless you override it.

UserControl VisualStates do not appear in Blend

The States pane in Blend 4 does not appear to show the VisualStates declared on a UserControl using the TemplateVisualState attribute. Is this to be expected and if so why? Creating the States manually in Blend seems to work correctly.
User controls aren't typically designed to be modified or re-templated the same way that regular controls are.
Most of the time where you want a control to be editable and offer alternative animations (through the states panel for instance) you're going to want to build something that derives from a proper custom control type.

Templates in extended Silverlight controls

i am looking to extend a third party control, it is a ComboBox (so it isn't the standard Silverlight one, but that shouldn't matter for this question). To do this, i add a new template control to my controls project, then i change the new control to inherit from ComboBox instead of Control. A style has been created for me in the generic.xaml file, so i delete the default border stuff that was inserted, and then add a property setter for the PopupTemplate.
My problem is that when doing it this way, the combobox doesn't (visibly) render in the silverlight application anymore. However, in the constructor of my extension if i comment out the line
this.DefaultStyleKey = typeof(MyComboBox);
and put the PopupTemplate xaml bit in the main silverlight page, it renders correctly. I want the popup template to be declared within the control library, but does this mean that i also have to define the regular Template property? Am i wrong in thinking that anything i don't explicitly specify should just be inherited from the base control?
A control can only have one default style. You need to copy the entire default style of the base control into the Generic.Xaml for you new MyComboBox then adjust it accordingly.

Does Visual Inheritance work with User controls in VS2008

I have a base User Control. I place Ok and Cancel buttons on the bottom right of the control and anchor them Bottom and Right.
I then create another user control that inherits from the base user control. I resize the inherited control (e.g. increase height or width). Throw the inherited control onto the form. Run. The inherited control does not honor the anchor properties of the Ok and Cancel buttons.
Here are the exact steps to repro.
1 - Create a new winforms project
2 - Create a base control (BaseControl1) with a Ok and Cancel buttons located at bottom/right. Anchor them there at Bottom,Right. Compile the app.
3 - Create a new User Control (UserControl1) that inherits from the base control (BaseControl1), created in step 1.
4 - Increase (in the designer) UserControl1's height or width.
5 - Throw UserControl1 onto Form1. Run. You'll see that Ok and Cancel buttons are not where they are supposed to be.
Am I doing something wrong, or does VS2008 simply not honor the anchor properties of the controls on the base user control?
Change the Modifiers property on your buttons to Protected. Then, after you complete step 4, you'll notice the designer code for UserControl1 now contains a line of code to set the buttons' locations. That wasn't happening when your buttons were scoped as Friend.
I've always wondered why controls dropped from the toolbox weren't scoped to Private by default.
I think your problem is that the default values for the Anchor property is not to be anchored. When you change the property and compile, that doesn't mean that's the default setting for classes that inherit your control.
If you are using the property selector, Visual Studio automatically puts some code in your application to change those values (i.e the designer code). Find the InitializeComponent() method and I bet you'll see something to the effect of:
this.myOKButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
this.myCancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
You'll need to set this property somewhere in your class, like the constructor, or override that property and specify the way you want it anchored.

Resources