All control's properties disabled in Visual Studio - winforms

I have a parent form and a child form that inherits from the former. In design mode all the inherited controls' properties are disabled. How can I change these properties in the child form?
Here is a screenshot of the designer. Note how I have selected a text box and all its properties are disabled (greyed out).

By default, the Windows Forms designer creates components with the private access modifier. This means that an inheriting form will render the controls, but cannot modify them.
To fix this, open the base form in the designer. Select the control(s) that you want to be able to modify and change the Modifiers property to Protected.
Important: After this change, you must rebuild the base form's project for changes to show up in the inheriting form's designer view.

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.

Winforms Databinding and multiple Forms

i have a main window that contains multiple UserControls, arranged as tab pages and tab groups (much like Visual Studio allows to have two or more editors visible at the same time).
I also have the possibility to open such an UserControl into a seperate floating window.
One of these UserControls contains simple form fields (e.g. text boxes). These text boxes are bounded with common databinding to an object / property. The binding mode is OnValidation (not on OnPropertyChanged).
When I switch the focus from this User Control inside the main window into another UserControl in the Main windows, the validation is automatically performed and the databinding is finised / the changed text will be set on the model object / property that is bounded to that text field.
But if I switch the focus to an UserControl which resides in another (floating) window, the databinding is not finished since no validation is performed.
I know that I can handle this manually by triggering ValidateChildren etc, but this seems to my the wrong way / is ugly.
Is there a "correct" / clean way to solve this issue? I want that the validation is performed as soon as the UserControl loses its focus or the window gets deactivated.
One information: On of my UserControls contains a TreeControl. If I edit a tree node label, and when I switch the focus to another (foating) window, the label edit is finished automatically. I want the corresponding behaviour for usual form fields regarding binding...
Thanks for help!
There is no automatic way to do this. From the point of view of the control, it still has the focus (if you click the title bar or Alt-tab back to the main window, you will notice that the focus remains in the same control). Its just that the form the control is on is not active. If you want it to save changes when your form is deactivated, you must manually trigger it. The best way to do that is probably to override the OnDeactivate method of the form.
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.ValidateChildren();
}

Custom Control Appearance

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

Pass a value from child to parent in Silverlight

I am working on Silverlight project.
I added a custom user control (Say, control1) which has a text box and button to a xaml page (Say, Page1).
Now what I want to do is when users clicks on the button, i want to pass the value in the textbox to Page1 and do something.
So basically, I am looking for a way to pass back a value from child to parent page in Silverlight.
Thank you.
You should look into the Model View ViewModel (MVVM) pattern. It works very well with WPF and Silverlight.
http://en.wikipedia.org/wiki/Model_View_ViewModel
http://karlshifflett.wordpress.com/mvvm/ (lots of good information and demos)
You can do this through binding. Bind the Text value of the TextBox to a string property in your ViewModel and use that property throughout the code.
All the controls within your user control are accessible within your main page. If possible, write the click event of the button within the main page and you'll be able to access any control's property. Hope that work for you.

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