C# 如何将控制权传递给方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/799731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass control to a method
提问by
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.
我希望每次调用此方法时都能够更改“btnAcesScore”。我如何将它传递给方法。我尝试将它作为字符串传递,但这不起作用。
Here is what I tried:
这是我尝试过的:
public void ChangeProperties(string category)
{
category = true;
}
ChangeProperties("btnAcesScore.Enabled");
Susan
苏珊
回答by Canavar
Try this :
尝试这个 :
public void ChangeProperties(Control ctrl)
{
ctrl.Enabled = true;
}
and call it like that :
并这样称呼它:
ChangeProperties(btnAcesScore);
回答by Daniel A. White
How about also
还有怎么样
void ChangeProperty(ref bool output)
{
output = true;
}
ChangeProperty(ref btnAcesScore.Enabled);
回答by n8wrl
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);
回答by John Rudy
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.
这样做的目的究竟是什么?是否重用该方法来任意更改Enabled
任何给定控件的属性?如果是这样,则有一种更简单的方法来完成它,如Canavar 所述。
Or is the point of this method to togglethe 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
.
取决于您是只想点击一个控件,还是提供对多个控件的访问。无论如何,我个人认为在方法中封装单个属性访问没有多大意义,如果您坚持(并且此方法没有调整其他属性),我至少会将其重命名为ToggleEnabled
.
回答by Madeleine
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.
我会使用反射 - 在您发送到您的方法的对象上使用 GetType() 方法,然后使用 GetProperties 来匹配您发送的属性。然后您可以设置该点的值。
回答by Robert Kozak
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");
回答by tofi9
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.
该示例可能需要对控件和属性的存在进行一些检查。
回答by svik
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;
}