C# 从变量参数更新实例属性

发布于 2024-08-26 19:13:15 字数 783 浏览 5 评论 0原文

假设我有这个类:

class block999
{
 string parm1;
 string parm2;
 string parm3;
 string parm4;
 string parm5;

 block999(){}

 setValue(object sender, RoutedEventArgs e)
 {
 }
}

以及带有以下代码的表单:

block999 B999 = new block999();    

TextBox parm1 = new TextBox();
TextBox parm2 = new TextBox();
TextBox parm3 = new TextBox();
TextBox parm4 = new TextBox();
TextBox parm5 = new TextBox();

parm1.LostFocus += new RoutedEventHandler(B999.setValue);
parm2.LostFocus += new RoutedEventHandler(B999.setValue);
parm3.LostFocus += new RoutedEventHandler(B999.setValue);
parm4.LostFocus += new RoutedEventHandler(B999.setValue);
parm5.LostFocus += new RoutedEventHandler(B999.setValue);

假设实例属性名称 == 文本框的名称,如何在 setValue 方法中设置正确的属性?

Say I have this class:

class block999
{
 string parm1;
 string parm2;
 string parm3;
 string parm4;
 string parm5;

 block999(){}

 setValue(object sender, RoutedEventArgs e)
 {
 }
}

And a form with this code:

block999 B999 = new block999();    

TextBox parm1 = new TextBox();
TextBox parm2 = new TextBox();
TextBox parm3 = new TextBox();
TextBox parm4 = new TextBox();
TextBox parm5 = new TextBox();

parm1.LostFocus += new RoutedEventHandler(B999.setValue);
parm2.LostFocus += new RoutedEventHandler(B999.setValue);
parm3.LostFocus += new RoutedEventHandler(B999.setValue);
parm4.LostFocus += new RoutedEventHandler(B999.setValue);
parm5.LostFocus += new RoutedEventHandler(B999.setValue);

How can I set the correct property in the setValue method assuming that the instance property name == the textboxes' name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-09-02 19:13:15

由于您使用的是 RoutedEventArgs,我假设您使用的是 WPF(或者可能是 SL)。

我认为最好的选择是在您班级的文本框和字段。如果您不想这样做,我建议只要任何文本框发生更改就更新所有字段。

无法从代码中访问局部变量的名称。所以您可能无法获取文本框名称。如果它们是类中的字段或属性,理论上您可以使用反射来执行此操作,但我不建议将其作为解决方案。

Since you are using RoutedEventArgs I assume you are using WPF (or maybe SL).

I think the best option would be to set up data bindings between the text boxes and fields of your class. If you don't want to do that, I would recommend just updating all of the fields whenever any of the text boxes changes.

There's no way to access the name of a local variable from code. So you probably can't get the text box names. If they are fields or properties on your class, you could theoretically do so using reflection, but I wouldn't recommend that as a solution.

有深☉意 2024-09-02 19:13:15

定义“文本框名称”的含义。 (暂时)分配给它的变量的名称是没有意义的。您可以使用控件的 Name 属性,但在您发布的示例中,您从不分配它。如果你是,那就只是:

setValue(object sender, RoutedEventArgs e) 
 { 
       TextBox tb = sender as TextBox;
       switch(tb.Name)
       {
          case "parm1":  
           this.parm1 = .... 
          // etc

       }
 } 

Define what you mean by "the textboxes' name". The name of the variable it's (temporarily) assigned to is meaningless. You could use the Name property of the control, except in the sample you post, you never assign it. If you were, it would be just:

setValue(object sender, RoutedEventArgs e) 
 { 
       TextBox tb = sender as TextBox;
       switch(tb.Name)
       {
          case "parm1":  
           this.parm1 = .... 
          // etc

       }
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文