Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

TextBox.LineCount execution and instability

$
0
0

Hi,

I think I am facing an unsual probably and maybe someone there can help me out.

So, I am developing this interface, I need to use a TextBox for text input. I also need to scale that TextBox sometimes and translate it according to its size.

Since the TextBox height changes during the typing, due to the wrap property, I need to get the correct height at each new user input. This is the first problem I found.

I tried some ways to get the TextBox height but it always was late updated in relation to the user typing. Which means that the user already reached a new line in his text box, but my 'getHeight' code always returned the old height. On the next user input then the height was updated correctly. But this late response caused me an annoyng effect on the positioning of the TextBox, as I told you, I need to set its position all the time. 

With some luck I found that if I try to access the TextBox.LineCount property just before to get the TextBox height it works perfectly. I guess by acessing the LineCount they force to update the height property immediately. Well, everything was fine but then a new bug appeared...

By using this line of code:

      double lineCount = this.window.MyTextBox.LineCount; //just to force the sdk to update the number of lines of the text 

the execution presented a strange behavior. By the way, lineCount variable is never used. Well, this is what happens, by debbuging the code when the execution hits this line it just does not continue from there. It ignores the remaining part of my function. Additionally it is causing an unusual behavior with another UI element, some grids are being relocated to weird positions. If I do not use this line this does not happen but then I go back to the first problem.

Don't know if I was clear enough, but this is the problem, any help would be appreciated. Here follows the function code I talked about:

public void UpdateTextBox()
    {
      TextStroke textStroke = this.window.AnnotationPadCanvas.Strokes.Last() as TextStroke;

      double test;
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);
      this.window.AnnotationPadCanvas.Strokes.Remove(textStroke);

      this.window.TextBox.TextAlignment = textStroke.note.TextAlignment;

      Point mainSidePoint = new Point();
      Point strokeScreenPoint = new Point(textStroke.StylusPoints[1].X, textStroke.StylusPoints[1].Y);
      PointInScreenCoord(ref strokeScreenPoint);

      if (this.window.TextBox.TextAlignment == TextAlignment.Left)
      {
        mainSidePoint = new Point(Canvas.GetLeft(this.window.LeftTextBoxRect), Canvas.GetTop(this.window.LeftTextBoxRect));
        strokeScreenPoint.X -= 15 + 5 * Global.zoomFactor;
        strokeScreenPoint.Y += 16 - 1 * Global.zoomFactor;
      }
      else
      {
        mainSidePoint = new Point(Canvas.GetLeft(this.window.RightTextBoxRect), Canvas.GetTop(this.window.RightTextBoxRect));
        strokeScreenPoint.X += 50 + 11 * Global.zoomFactor;
        strokeScreenPoint.Y += 16 - 1 * Global.zoomFactor;
      }

      Vector translation = strokeScreenPoint - mainSidePoint;
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      Canvas.SetLeft(this.window.LeftTextBoxRect, Canvas.GetLeft(this.window.LeftTextBoxRect) + translation.X);
      Canvas.SetTop(this.window.LeftTextBoxRect, Canvas.GetTop(this.window.LeftTextBoxRect) + translation.Y);
      Canvas.SetLeft(this.window.RightTextBoxRect, Canvas.GetLeft(this.window.RightTextBoxRect) + translation.X);
      Canvas.SetTop(this.window.RightTextBoxRect, Canvas.GetTop(this.window.RightTextBoxRect) + translation.Y);
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      double textAuxGridWidth = this.window.TextAuxiliarGrid.DesiredSize.Width;
      double textAuxGridHeight = this.window.TextAuxiliarGrid.DesiredSize.Height;
      Canvas.SetLeft(this.window.TextAuxiliarGrid, Canvas.GetLeft(this.window.RightTextBoxRect) - textAuxGridWidth - 5);
      Canvas.SetTop(this.window.TextAuxiliarGrid, Canvas.GetTop(this.window.RightTextBoxRect) - textAuxGridHeight - 5);
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      string text;
      CopyTextBoxText(out text);

      FormattedText note = new FormattedText(text,
                                             System.Globalization.CultureInfo.GetCultureInfo("en-us"),
                                             FlowDirection.LeftToRight,
                                             new Typeface("Segoe360"),
                                             Global.fontSize,
                                             new SolidColorBrush(Global.textColor));
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);


      textStroke.note = note;
      textStroke.color = Global.textColor;
      textStroke.fontsize = Global.fontSize;
      this.window.TextBox.FontSize = textStroke.fontsize;
      this.window.TextBox.Foreground = new SolidColorBrush(Global.textColor);
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      this.window.TextBox.RenderTransform = new ScaleTransform(Global.zoomFactor, Global.zoomFactor, 0, 0);
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      double p1 = Canvas.GetLeft(this.window.RightTextBoxRect);
      double p2 = Canvas.GetLeft(this.window.LeftTextBoxRect) + this.window.LeftTextBoxRect.Width;
      double distance = p1 - p2;
      this.window.TextBox.Width = (distance - 10) / Global.zoomFactor;
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      double lineCount = this.window.TextBox.LineCount; //just to force the sdk to update the number of lines of the text box | in execution sometimes it jumps the remaining function code
      double textHeight = this.window.TextBox.DesiredSize.Height;
      test = Canvas.GetLeft(this.window.LeftTextBoxRect);

      Canvas.SetLeft(this.window.TextBox, Canvas.GetLeft(this.window.LeftTextBoxRect) + this.window.LeftTextBoxRect.Width + 5 + (this.window.TextBox.Width * (Global.zoomFactor - 1)) / 2);
      Canvas.SetTop(this.window.TextBox, Canvas.GetTop(this.window.LeftTextBoxRect) + ((textHeight) * (Global.zoomFactor - 1)) / 2);

      test = Canvas.GetLeft(this.window.LeftTextBoxRect);
      this.window.AnnotationPadCanvas.Strokes.Add(textStroke);
    }

 


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>