How do you rotate the text in textblock in C# (Code-Behind)~~
Asked Answered
D

3

9

Basically I am currently doing final year project in my college whereby i am touching on surface 2.0 WPF.

My project is a game whereby if a user answer a question wrongly,the next question will be rotated to make it more difficult. But I am unsure how to do it. I saw an example in msdn microsoft but it only shows XAML codes. I need the C# codes.

Here's the XAML example.

http://msdn.microsoft.com/en-us/library/ms754028.aspx

The last example

Here's part of my validation codes. I need to activate the animation if user answer wrongly.

 if (surfaceRadioButton1.IsChecked == true)

{

user_answer = (string)surfaceRadioButton1.Content;

            textBlock2.Text = validateAnswer(user_answer, answer);
            retreiveYellowQns();
            if (textBlock2.Text.Equals("Correct"))
            {
                yellow_coord = yellow_coord + 50;
                Canvas.SetLeft(car, yellow_coord);
                Canvas.SetTop(car, 289);
            }
            else
            {
                if (yellow_coord <= 330)
                {
                    yellow_coord = 330;
                    Canvas.SetLeft(car, yellow_coord);
                    Canvas.SetTop(car, 289);
                }
                else
                {
                    yellow_coord = yellow_coord - 50;
                    Canvas.SetLeft(car, yellow_coord);
                    Canvas.SetTop(car, 289);
                }
            }
        }

Any Help will be glad,thanks in advance.

Dioxide answered 30/1, 2012 at 7:0 Comment(0)
Q
11

Try this one. You can use an animation on the RenderTransform:

var rotateAnimation = new DoubleAnimation(0, 180, TimeSpan.FromSeconds(5));
var rt = (RotateTransform) textblock2.RenderTransform;
rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);

In your Xaml, you can add the RotateTransform:

<TextBlock>
  <TextBlock.RenderTransform>
    <RotateTransform Angle="0"/>
  </TextBlock.RenderTransform>
</TextBlock>
Quiz answered 30/1, 2012 at 8:29 Comment(4)
So should i put that code inside the else { yellow_coord = yellow_coord - 50; Canvas.SetLeft(car, yellow_coord); Canvas.SetTop(car, 289);Dioxide
Yes, put this code to the lines where your text should be rotated (Error validation -> error case) .This will animate your text to be rotated with 180 degrees within 5 Seconds. This will give the user a better visual feedback that it is rotated.Quiz
Thanks,it works. Just one last question. How do i forcefully terminate the animation once the user answer the next question correctly?Dioxide
You'll have to wrap your Animations within a BeginStoryboard. Once you've given this Storyboard a name, you can call StopStoryboard with the given name in your "CorrectAnswer" - Handler. Check out this one: #5864222Quiz
A
7

You will have to use Transformation for this. Try this answer https://mcmap.net/q/972624/-drawing-vertical-text-in-wpf-using-drawingcontext-drawtext

Or You can also try, (I have not tried this) Look at this article for more details

textBlock2.RenderTransform = new RotateTransform(IntegerAngleValue); 
Antisana answered 30/1, 2012 at 7:3 Comment(1)
Works like a charm in Winforms! Thank you, this solved my problem.Stauffer
O
2
            var rotateAnimation = new DoubleAnimation(180, 0, TimeSpan.FromMilliseconds(200));
            UiImage.RenderTransformOrigin = new Point(0.5,0.5);
            UiImage.RenderTransform = new RotateTransform();
            var rt = (RotateTransform)UiImage.RenderTransform;
            rt.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);
Overijssel answered 26/6, 2013 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.