views:

170

answers:

2

Hello,

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?

+2  A: 

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.

Daniel Plaisted
+1  A: 

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

       }
 } 
James Curran