locked
Can I use a 'variable' to substitute a control name? RRS feed

  • Question

  • Greetings All,

     

    I want to know whether it is possible to pass in a variable in the place of a hard-coded control name e.g....

     

    ______________________________

    public string MyVariable;

    ...

    MyVariable = "Slider1.Value";
           
    MyVariable = 50;

    ______________________________

     

    Any thoughts?

    Many Thanks,

    -RJ-

    Wednesday, July 7, 2010 7:06 PM

Answers

  • I believe this code should do what you are after, and not require quite so much duplication:

    double RotateEverythingByX = 45;
    
    			for (int Variable1 = 0; Variable1 <= 355; Variable1 = Variable1 + 5)
    			{
    				string controlName = "TextBlock" + Variable1 + "deg";
    
    				var control = this.FindName(controlName) as UIElement;
    				if (control != null)
    				{
    					if (Variable1 + RotateEverythingByX > 90 && Variable1 + RotateEverythingByX < 270)
    					{
    						control.RenderTransform = new RotateTransform() { Angle = Variable1 + 180 };
    					}
    					else
    					{
    						control.RenderTransform = new RotateTransform() { Angle = Variable1 };
    					}
    				}
    			}

    The same thing but with a method:

    			double RotateEverythingByX = 45;
    
    			for (int Variable1 = 0; Variable1 <= 355; Variable1 = Variable1 + 5)
    			{
    				MyMethod(Variable1, RotateEverythingByX);
    			}
    
    private void MyMethod(int Variable1, double RotateEverythingByX)
    		{
    			string controlName = "TextBlock" + Variable1 + "deg";
    
    			var control = this.FindName(controlName) as UIElement;
    			if (control != null)
    			{
    				if (Variable1 + RotateEverythingByX > 90 && Variable1 + RotateEverythingByX < 270)
    				{
    					control.RenderTransform = new RotateTransform() { Angle = Variable1 + 180 };
    				}
    				else
    				{
    					control.RenderTransform = new RotateTransform() { Angle = Variable1 };
    				}
    			}
    		}

    • Proposed as answer by Chuck HaysModerator Thursday, July 8, 2010 2:24 PM
    • Marked as answer by dt01rrj Thursday, July 8, 2010 4:37 PM
    Thursday, July 8, 2010 2:24 PM
    Moderator

All replies

  • or alternatively ...

    ______________________________

    public string MyVariable;

    ...

    MyVariable = "Slider1";
           
    MyVariable.Value = 50;

    ______________________________

    Wednesday, July 7, 2010 7:30 PM
  • What are you trying to accomplish? If you change your variable type to object instead of string the first example will compile.
    Wednesday, July 7, 2010 8:11 PM
    Moderator
  • Greetings Chuck,

     

    Many thanks for the reply; below I have added some example code - Essentially I want to call a common Method/Subroutine for +70 different TextBlocks.  These TextBlocks run tangent along the circumference of a circle (from 0 to 360 in increments of 5 ... like numbers around a compass, text facing inwards) I want to ensure these textblocks are legible (never upside-down on screen) so I need them to rotate (flip) by 180 depending if they appear in the top half of the circle (>90 and <270) and not whilst in the bottom half ... I need also to rotate the whole 'graphic' around so the TextBlocks are not fixed (like rotating a compass on a table).

    ___________________________________________________________________________________________________

     

    public double Variable1; // Angle which makes TextBlock tangent to circle edge

    public double RotateEverythingByX;

    public object TextBlockToRotate;

     

           
    public void Flip_15deg_BEV_Markers (){

    Variable1 = 0; TextBlockToRotate = "TextBlock0deg.RenderTransform"; MyMethod ();  // textblock 0

    Variable1 = 5; TextBlockToRotate = "TextBlock5deg.RenderTransform"; MyMethod ();  //textblock 5

    //... and so on in integers of 5 until ...

    Variable1 = 355; TextBlockToRotate = "TextBlock355deg.RenderTransform"; MyMethod ();  //textblock 355

    }

     

    public void MyMethod ()

    {if  (Variable1 + RotateEverythingByX > 90) && (Variable1 + RotateEverythingByX < 270)

    {this.TextBlockToRotate = new RotateTransform (Variable1+180);}  

    else  {this.TextBlockToRotate = new RotateTransform (Variable1);}}

     

    ___________________________________________________________________________________________________

     

    Your previous suggestion did allow me to compile the sample code I initially submitted, but, it didn't actually affect/influence the Slider control's value ...? I may have misunderstood ... :(

     

    Many Thanks Again,

    -RJ-

    (Expression Blend 4, C#, WPF)

    Thursday, July 8, 2010 9:01 AM
  • I believe this code should do what you are after, and not require quite so much duplication:

    double RotateEverythingByX = 45;
    
    			for (int Variable1 = 0; Variable1 <= 355; Variable1 = Variable1 + 5)
    			{
    				string controlName = "TextBlock" + Variable1 + "deg";
    
    				var control = this.FindName(controlName) as UIElement;
    				if (control != null)
    				{
    					if (Variable1 + RotateEverythingByX > 90 && Variable1 + RotateEverythingByX < 270)
    					{
    						control.RenderTransform = new RotateTransform() { Angle = Variable1 + 180 };
    					}
    					else
    					{
    						control.RenderTransform = new RotateTransform() { Angle = Variable1 };
    					}
    				}
    			}

    The same thing but with a method:

    			double RotateEverythingByX = 45;
    
    			for (int Variable1 = 0; Variable1 <= 355; Variable1 = Variable1 + 5)
    			{
    				MyMethod(Variable1, RotateEverythingByX);
    			}
    
    private void MyMethod(int Variable1, double RotateEverythingByX)
    		{
    			string controlName = "TextBlock" + Variable1 + "deg";
    
    			var control = this.FindName(controlName) as UIElement;
    			if (control != null)
    			{
    				if (Variable1 + RotateEverythingByX > 90 && Variable1 + RotateEverythingByX < 270)
    				{
    					control.RenderTransform = new RotateTransform() { Angle = Variable1 + 180 };
    				}
    				else
    				{
    					control.RenderTransform = new RotateTransform() { Angle = Variable1 };
    				}
    			}
    		}

    • Proposed as answer by Chuck HaysModerator Thursday, July 8, 2010 2:24 PM
    • Marked as answer by dt01rrj Thursday, July 8, 2010 4:37 PM
    Thursday, July 8, 2010 2:24 PM
    Moderator
  • Greetings Chuck,

     

    Amazing!!!  I've been struggling with this for days,  I would never have come up with such an elegant, and concise, solution as this (... the first one especially).  I've always thought it a shame that the most skilful code always looks the most unassuming ... :( 

    .... As far as I'm concerned both solutions are a work-of-genius - especially when comparing them to what I had originally coded ... :S

    Many thanks again for your effort and assistance, it is MUCH appreciated.

     

    Best Regards,

    -RJ-

     

    Thursday, July 8, 2010 4:37 PM