Results 1 to 6 of 6

Thread: Control property dymicaly assignment

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Question 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
    Last edited by ishrar; Nov 5th, 2008 at 06:16 AM.
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. private void SetProperty(Control ctrl, string propName, object propValue){
    2.     System.Reflection.PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
    3.     if(propInfo != null){
    4.         propInfo.SetValue(ctrl, propValue, null);
    5.     }
    6. }
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    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.
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    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.
    Last edited by ishrar; Nov 5th, 2008 at 06:12 AM.
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    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.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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....

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width