开发者

Programatically setting design-time properties in Windows.Forms control

Is there an easy way to programatically setting a property value on a control such that it will be persisted in the designer-generated code?

I imagine a piece of code in the control constructor or load event which is executed when i open the control in d开发者_运维技巧esign mode, but sets a property such that it will be persisted the same way as if I changed the value manually through the properties grid.

Edit: Yes, this would be the same as editing the designer code manually, but I want to do it programatically.


Presuming I understand the question

You can databind that property to a setting, using the Visual studio Gui. Check the properties for that control, under the Data section for (Application Settings), (Property Bindings).


It depends on what kind of functionality you want. If you only need the properties to be set when you add the control to a form, then setting the properties in the control's constructor works perfectly. However, changes you make using the Properties panel will take precedence, and setting properties in the control's constructor won't necessarily affect existing instances of the control.

If you want to be able to change the properties for instances of the control in one place, assigning bindings in (application settings), (property bindings) works. Then you can modify all the bindings from the Settings.settings file. This still requires you to assign property bindings for each instance of the control, though.

Now for the finale. If you want to set properties in the control's class that affect all instances of the control, whether the instances are yet to be created or already exist, you have to get a little creative. I found a solution, but it may not be the best. My solution goes like this:

In the control's constructor, for each property you want to set, you:

  1. Store the desired value in a private variable.
  2. Assign the variable's value to the property.
  3. Assign an event handler that assigns the variable's value to the property whenever the property is changed.

A downside is the amount of coding for each property. Also, you wouldn't be able to change the properties from the Properties pane.


Do you think about something like:

if (this.DesignMode)
{
    // do somthing
}

If you put this into the constructor remember to call InitializeComponent() before.


What about:

Private Function GetPropertyByName(ByVal propName As String) _
As PropertyDescriptor
    Dim prop As PropertyDescriptor
    prop = TypeDescriptor.GetProperties(l_dWindow)(propName)
    If prop Is Nothing Then
        Throw New ArgumentException( _
        "Matching ColorLabel property not found!", propName)
    Else
        Return prop
    End If
End Function

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    GetPropertyByName("AnyPublicProperty").SetValue(AnyControl, "AnyStringVALUE")
    Me.DialogResult = DialogResult.OK
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜