Reputation:
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
Upvotes: 5
Views: 16582
Reputation: 222
Main()
{
ChangeProperties(ref category,True); //Where Category is the ID of the Textbox control i.e <asp:textbox ID="Category "></textbox>
}
public void ChangeProperties(ref TextBox category,bool val)
{
category.Enabled = val;
}
Upvotes: 0
Reputation: 5853
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.
Upvotes: 0
Reputation: 2033
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");
Upvotes: 1
Reputation: 2182
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.
Upvotes: 0
Reputation: 37850
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
.
Upvotes: 4
Reputation: 19765
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);
Upvotes: 0
Reputation: 190942
How about also
void ChangeProperty(ref bool output)
{
output = true;
}
ChangeProperty(ref btnAcesScore.Enabled);
Upvotes: 0
Reputation: 48088
Try this :
public void ChangeProperties(Control ctrl)
{
ctrl.Enabled = true;
}
and call it like that :
ChangeProperties(btnAcesScore);
Upvotes: 13