Initialization control's property without repainting - c#

I'm creating a control, and adding a property (control's look depends on it). How can I set this property:
I can't set the property in form constructor immediatly after InitializeComponent() call. In this case user will see two frames of form initialization: the first — after InitializeComponent(), and the second — after property setting, that invoke control's redrawing. Bad.
Also, I can't mark my propperty with BrowsableAttribute, cause a type of the property, is my own class, that can't be configurated in «properties window». Аlso bad.
So, how could I inicialize the property between form1.SuspendLayout() and form1.ResumeLayout(false)?
Ideally, I would like to have possibility to write a code directly in a respective field of «properties window». For example I would write new MyClass(param1, param2), if type of the property is MyClass.

Don't force the controll to redraw on property change. It's not necessary when you initialize the control and when a user change the property it will be redrawn in the next paint event. If needed the user can call .Refresh() on your control after setting the property to force the redraw manually.

Related

Validating Attached Property in Property Change handler

I am developing a custom control for Windows Phone 8, which is derived from ItemsControl, will have many child objects(another custom class). It needs to have an Attached Property IsMinonAxis which should be set only once by one of the child, and not more than once. So the below code will be a problem I want to avoid.
<WPGraphControl:GraphControl>
<WPGraphControl:GraphLine GraphDataPoints="{Binding SpeedPoints}" WPGraphControl:GraphControl.IsMinonAxis="True" />
<WPGraphControl:GraphLine GraphDataPoints="{Binding AltitudePoints}" WPGraphControl:GraphControl.IsMinonAxis="True" />
</WPGraphControl:GraphControl>
The problem is attached properties are attached to child controls, and not to the parent.
In the PropertyCHangedCallback (registered as part of RegisterAttached as part of PropertyMetadata) I can get the child object for which the property is being set, but I can't access the actual control instance (this) to be able to validate the whole collection of child controls as its a static method common across all instances .
One option I am thinking is to have another attached property (internal) which will be attached when the child controls are added, and then use that property to get to parent inside the callback, and fire the validation logic. It sounds like a overly complicated logic to me.
Could you please suggest what is the best way to handle situations like this?
Might be easier to have a non-attached property on GraphControl that takes a Element to be used. Its been a while since I did WinPhone XAML, but in WPF this would look something like:
<wpgc:GraphControl MinorAxis="{Binding ElementName=Foo}">
<wpgc:GraphLine x:Name="Foo" />
<wpgc:GraphLine x:Name="Bar" />
</wpgc:GraphControl>
Since you can only assign a single value to the property, then this would ensure that only one is set.

Trigger the visibility of control based on count

This is just a generic question related to C# and WinForm..
Is there any way to automatically trigger visibility of a control based on the count..
For example, I have a Boolean count which could be true or false.. If the count is True I need to hide some control and if its false I need to show the control.
Is it possible that changing the value of Boolean control can trigger the visibility function automatically? So that there is no actual call of Show / Hide control.. When the count value changes it automatically triggers the function which checks for the count value and shows/hide the control?
At some point, there WILL be a call to Show() or Hide() or changing .Visible.
You can, in the designer, bind the control's .Visible property to the Count property (seriously reconsider the name for this) on your object. But that will really just pre-write the code for you.
When are you loading your object that has this boolean-count property? You could change the visiblilty of anything you like at that point.
Or, you could change the visiblity when your user edits the object you're presenting.

When a UIControl is Hidden does it lose its Functionality

If we hide a control with Control.Hide(); in C# (win-forms) does the Control lose its Functionality, in that way that Control's Stop's its Execution like if we Have an MP3 Player Control and if we Hide it does it Stop Sounding, or if we Populate Data in a DataGridView than if we Play with it's Visibility does GridView clear it's Data .
If the Control does lose ,what can i do to prevent it and if anyone knows why?
PS: Where is e Difference between Control.Visible = false; and Control.Hide();
The control will not lose its functionality. It is still an object in memory and if you call any method on it, it will execute. Hiding it simply means it does not display on a form anymore.
In your example, hiding a control will not automatically stop the sound, unless that is built into the hide function.
As for you PS - from MSDN, Control.Hide Method:
Hiding the control is equivalent to setting the Visible property to false. After the Hide method is called, the Visible property returns a value of false until the Show method is called.

Object synchronization with GUI Controls

Every time when I change some values in form controls, I need to set a property of an object or vice versa. Instead of writing some methods for each control, I want a general solution for GUI elements. I do not use WPF but only C# 3.0.
Any suggestions?
Thanks.
All controls expose events that signal that their value changed. You can subscribe to it and change another control's value.
You could make the object a field in your form. When the relevant event fires from a control, then call the appropriate operation on the object.
Alternatively, have a Presenter/Controller object that stores your form as a field. It could take it as a parameter in it's constructor. This presenter can then subscribe to your form's relevant events and act appropriately. You could go further with this and extract an interface from your form and program to that in the presenter instead which would help testing. Have a look at the MVP pattern.

Initialize properties at the load event or in the constructor?

I have a property that references a service layer object and I need it every time I use a form.
What is the best pratice: initialize a property in the constructor or in the form's load event?
If the validity of the state of the form is dependent upon the property being set, then set the property in the constructor. You always want your objects to be in a valid state after they're constructed.
Yeah, but be careful what you do in the constructor of a form, as the visual designer will run this when you open up the form to edit.
If you put anything here that relies on other stuff being set up at run time, it is likely to cause an error and you wont be able to edit the form layout.
I would say put it in the Form Load for this reason.
In the constructor. Very often you new up a form, and need to set some properties, or do other kind of set up before you actually display the form. In those cases, you'll want all your instance variables to be set up even before the Form actually loads up.
Constructor should be responsible for initialization, unless you have specific need or dependency to initialize your variable on Form Load, such as initializing it to something that is dependent on something else.
You should initialize properties in the constructor. The constructor is obviously called only once per form instance. The load event handler will be called every time a form is shown. Also, if you did initialization work, such as fill a combo box, in the load event handler, you'd have to write some pretty hacky code to preselect a value in that combo box before showing a form. That's just one example though. Hope that helps.

Categories

Resources