tags:

views:

219

answers:

7

I want to create a method that changes enabled property. How do I pass the contorl name and property to a method.

If the following were my original method:

public void ChangeProperties()
{ 
     btnAcesScore.Enabled = true;
}

I want to be able to change the "btnAcesScore" each time I call this method. How do I pass this to the method. I tried passing it as a string but that doesn't work.

Here is what I tried:

public void ChangeProperties(string category)
{ 
     category = true;
}

ChangeProperties("btnAcesScore.Enabled");

Susan

+4  A: 

Try this :

public void ChangeProperties(Control ctrl)
{ 
     ctrl.Enabled = true;
}

and call it like that :

ChangeProperties(btnAcesScore);
Canavar
I like yours better!
n8wrl
agreed.. this is more OOP and answers the question more directly.
madcolor
Am I wrong ? isn't she asking for this ? or is she asking for changing the control's property ?
Canavar
Why on earth would you write a method that sets a control to be enabled, when you can just set it to be enabled without passing it around all and sundry. OOP for the sake of it, it isn't wrong so no downers from me, but I never got something signed off because it wasn't wrong :-)
Mark Dickinson
First of all, don't think it that just enables/disables control, I use this kind of methods to set many properties to many types of controls. Once I get a downvote for getting many upvotes to a very easy question.. so never mind the downvote, if you know it's true.
Canavar
Is Canavar not "passing a control to a method"??? Seems like the answer to me.
madcolor
A: 

How about also

void ChangeProperty(ref bool output)
{
    output = true;
}
ChangeProperty(ref btnAcesScore.Enabled);
Daniel A. White
A: 

Not sure I totally understand your intent, but you could pass a delegate to some code that changed your property...

public void ChangeProperties(Action<bool> setprop)
{
    ...
    setprop(true);
}

then call it:

ChangeProperties(b => btnAcesScore.Enabled = b);
n8wrl
+3  A: 

What exactly is the purpose of this? Is it to reuse the method to arbitrarily change the Enabled property of any given control? If so, there is an easier way to accomplish it, as outlined by Canavar.

Or is the point of this method to toggle the setting? In which case, your method would look either like:

public void ChangeProperties()
{ 
     btnAcesScore.Enabled = !btnAcesScore.Enabled;
}

or

public void ChangeProperties(Control ctrl)
{ 
     ctrl.Enabled = !ctrl.Enabled;
}

depending on whether you wanted to hit just the one control, or provide access to many. In any event, I personally don't see much point to encapsulating a single property access within a method, and if you were insistent (and this method didn't adjust other properties), I'd at least rename it to something like ToggleEnabled.

John Rudy
Who knows why people downvote when they don't say why. I would have changed the answer. Oh well, thanks for your comment.
Mark Dickinson
A: 

I'd use reflection - use the GetType() Method on the object you send through to your method and then use the GetProperties to match against the property you send through. You can then set the values at that point.

Vixen
+1  A: 

Since the original question had a reflection tag I think she wanted a reflection answer (whether or not that is good design) so here is a Reflection answer.

the form has a controls collection and with this you can search for it and use reflection to set the property:

public void ChangeProperties(Form form, string category)
{
   string[] parts = category.Split(".");
   int index = form.Controls.IndexOfKey(parts[0]);

   Control control = null;
   if (index >= 0)
   {
     control = form.Controls[index].;
   }

   if (control != null)
   {
     PropertyInfo propertyInfo = control.GetType().GetProperty(parts[1]);
     if (propertyInfo != null)
     {
       propertyInfo.SetValue(control, true);
     }
   }
}

if you call it from the form the control lives on

ChangeProperties(this, "btnAcesScore.Enabled");
Robert Kozak
A: 

Try this:

    public void ChangeProperties(string category, object value)
    {      
        var categoryConcat = category.Split('.');
        var control = this.Controls.Cast<Control>()
            .Where(x => x.Name == categoryConcat[0]).First();
        control.GetType().GetProperty(categoryConcat[1])
            .SetValue(control, value);        
    }

The example probably needs some checks on the existance of the control and the property.

taoufik