Control property dymicaly assignment
Hii all,
Something I want, which seems like short cut way.
I have created a Method which will accept the type of control and 4 defined property of control.
This method will create a control on the form and assign these 4 common properties on the control and it will put the created control on the form.
Now I want one more method which will allow user to assign the property which user want to assign
i.e.
Code:
SetProperty("TextBox1","ReadOnly","True");
This method should assign readonly property to textbox1 created on the form.
I think there is some method which allows to assign property dynamically.
Thanks in advance:down: :down:
Re: Control property dymicaly assignment
Im not at a computer with .NET right now, but that can easily be done with the use of reflection. However why not just give the user access to the control reference so he can access the properties direclty, the "proper" way? That would be alot more efficient and a better design.
However if you still wish to do this, you'd need to do something like this:
(this might or might not be 100% correct, as it is off the top of my head)
CSharp Code:
private void SetProperty(Control ctrl, string propName, object propValue){
System.Reflection.PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
if(propInfo != null){
propInfo.SetValue(ctrl, propValue, null);
}
}
Re: Control property dymicaly assignment
Hmmm nice.....
I was not aware of it...
You have given me an exact function which I desired....
Thanks Man..
Thanks A lot.
Re: [RESOLVED] Control property dymicaly assignment
@Atheist Thank you man. Again I have problem with this thing. I can assign all properties of control.But I don't know how I can assign subproperty using this method
i.e.
If I want Column.Count of DataGridView then what should I do?
Code:
System.Reflection.PropertyInfo propInfo = Button1.GetType().GetProperty("Fonts.Bold");
propInfo.GetValue(Button1,null);
Please suggest me some way.
Thank you.
Re: Control property dymicaly assignment
You'd have to alter your function so that Ctrl was an object, then you'd call it something like this.
Code:
SetProperty(MyGridView.Columns, "Count", 5
I think that should do the trick.
P.S. You didn't need to duplicate question as a new thread. Lots of people check these forums and as long as the thread isn't marked as resolved they'll assume you need more help that has been given so far.
Re: Control property dymicaly assignment
you do realise that Reflection is expensive? you can pretty much probably achieve the same thing by having a propertygrid control... and let the user change properties via that based on what properties you want to expose on the control selected....