Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you create a WYSIWYG TextBox control editor for vertically centered messages sent to a mobile device?
    primarykey
    data
    text
    <p>I have a WPF TextBox that holds seven lines of text and has word wrapping enbabled. </p> <pre><code>&lt;TextBox TextWrapping="Wrap" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MaxLines="7"/&gt; </code></pre> <p>As you can see in the XAML above the text is centered vertically and horizontally. When I type a short phrase that fits on a single line, the text appears on the 4th line of the control as expected since the VerticalContentAlignment is "Center".</p> <p>The text that is entered by the user is meant to be sent to a mobile device that has a display that holds seven lines of text and uses the "\n" to wrap to the next line. The intent is that the display of text on the mobile device looks the same as what is entered into the TextBox control. At least as far as number of lines of text, centering, and where line breaks occur. </p> <p>So when the the user finishes entering the text into the TextBox control and clicks the "Send Message" button, some post-processing must be done on the entered text before sending it to the mobile device. </p> <p>The text entered into TextBox control needs to have newline (\n) characters added wherever the text wraps to a new line in the TextBox control. For example, in cases where the control is showing multiple lines of text, I copy the TextBox's text and add a newline between the lines wehre the TextBox control wrapped the lines of text that were entered by the user. </p> <p>So when the user clicks a "Send Message" button this is the code that does the post processing:</p> <pre><code> public static String AddNewLineCharsToMessage(TextBox textBox) { String message = String.Empty; if (textBox == null) return message; // First strip all the carriage returns and newline characters // so we don't have duplicate newline characters in the message. // Then add back in just the newline characters which is what the // mobile device uses to wrap lines. // Just assign the text if we have a single line message if (textBox.LineCount &lt; 2) return textBox.Text; var textLines = new List&lt;string&gt;(5); int lineCount = Math.Min(textBox.LineCount, textBox.MaxLines); for (Int32 index = 0; index &lt; lineCount; index++) { if (textBox.GetLineText(index).Length &gt; 0) { textLines.Add(textBox.GetLineText(index)); textLines[index] = textLines[index].Replace("\r", ""); textLines[index] = textLines[index].Replace("\n", ""); } else textLines.Add("\n"); } message = String.Empty; for (Int32 index = 0; index &lt; lineCount; index++) message += textLines[index] + (index &lt; lineCount - 1 ? "\n" : ""); return message; } </code></pre> <p>Given the code above, I would expect the output for a single line of text to look something like: "\n\n\n\nFoo". However, the output is "\nFoo\nFoo\nFoo\nFoo". Setting a break point in the code I see that textBox.GetLineText(index) for indices 0 through 3 returns "Foo" for each index even though "Foo" is only shown once in the TextBox control.</p> <p>So I guess I really have two questions:</p> <p>1) Why does GetLineText return a LineCount of 4 with every line having the same text, when only a single line of text (that fits on one line in the TextBox control) was entered by the user?</p> <p>2) What is an easy way to work around this, keep the entered text centered in the TextBox control, and send the remote device the text message that will be displayed as seen by the user on the TextBox control?</p> <p>Notes: I cannot simply remove duplicate lines of text and replace them with "\n" as the user may have typed in the same text on multiple lines. Also, I could simply align the entered text to the vertical top instead of the vertical center. I have verified this works, but does not give a true WYSIWIG experience.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload