Home > Enterprise >  How can I set limit of Lines in TextBox in UWP using XAML and C#
How can I set limit of Lines in TextBox in UWP using XAML and C#

Time:01-03

I want to set the maximum number of lines in Textbox that has TextWrap enabled. I tried multiple place for this answer but I failed. Please help me to find the answer.

UPDATE: The current solutions suggest either counting the total number of characters or splitting the content by line breaks and counting the lines, but these solutions do not provide the expected result when text wrapping is enabled.

CodePudding user response:

So basically you can count of the lines and do some limitations. To count the lines use this code:

    private void textBox_TextChanged(object sender, RoutedEventArgs e)
    {
        int t = textBox.Text.Split('\n').Length;  
        if(t > 9)
        {
            Debug.WriteLine("Limit reached");
            //your code here
        }
    }

This is a basic code, but the principals are the same. To limit the lines you can disable further insertion of new line char, or other limitations.

CodePudding user response:

How can I set limit of Lines in TextBox in UWP using XAML and C#

There is no way to do this perfectly in UWP TextBox. Different from the TextBox of WPF which has a TextBox.MaxLines Property, the TextBox class in UWP doesn't have such a built-in property. Like what the @10 Devlops said, you need to do this on your own.

Besides the solution @10 develops provides, my suggestion is to use the TextBox.MaxLength Property which could limit the max length of the text in the TextBox. You could combine this property with the solution @10 develops provides to get a new solution that depends on your own scenario.

CodePudding user response:

Text wrapping adds a new dimension to restricting the number of lines in a TextBox, to do so we need be aware that the number of lines can be affected by the resolution and zooming factor applied at the operating system level, but also many layout concepts utilised in UWP layout are generally relative or dynamic and affect the visual state is ways that are hard to keep consistent for all users.

For instance, text that just fits on two lines in one user's runtime, might easily wrap into 3 lines for another user or when the width of the form is changed.

Even after doing your best to limit a wrapped text box to a specific number of lines, we have to expect that over time, we will get it wrong, if the number of lines is important for layout reasons then you might consider trimming or truncating the text or even using RichTextBlockOverflow

You can try this solution from How can I measure the Text Size in UWP Apps?, and then validate against the height of the text, not specifically the number of lines.

However I have in the past also found success by setting the TextBox to a fixed height (and of course width) and validated against the Scrollbar state. Unfortunately it is hard to access the vertical scrollbar visibility directly, but we can use the ExtentHeight and the ViewportHeight on the ScrollViewer element that is inside the TextBox template.

If the ExtentHeight is greater than the ViewportHeight then the scrollbar should be visible. If the Visual layout only allows the total number of lines that you want to support then the scrollbar should not ever be visible.

Accessing the ScrollViewer gets interesting, options are discussed here How do access the scrollviewer of a TextBox in code

So try this as an adaptation to the solution from @10 Develops:

private void textBox_TextChanged(object sender, RoutedEventArgs e)
{
    var scroller = (sender as TextBox).FindChild<ScrollViewer>();
    if (scroller.ExtentHeight > scroller.ViewportHeight)
    {
        Debug.WriteLine("Limit reached");
        //your code here
    }
}

With this extension method defined in a static class:
(using pattern matching, so a bit different to the original referenced post)

public static T FindChild<T>(this DependencyObject parent) where T : UIElement
{
    if (parent is T matchedParent) return matchedParent;
    int children = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < children; i  )
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is T matchedChild)
            return matchedChild;
        else
        {
            T tChild = FindChild<T>(child);
            if (tChild != null) return tChild;
        }
    }
    return null;
}

I don't normally use this for validation of content, but I do use it to add buttons to toolbars that allow the user to quickly scroll to the top or bottom of the content and to toggle the visibility of such controls.

  • Related