Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
50
UltraTextEditor1 Multiline count
posted

On a TextBox I can calculate the number on lines with the following code:

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    linecount = SendMessage(TextBox1.Handle.ToInt32, EM_GETLINECOUNT, -1, 0)
    lblLine1.Text = "Line Count: " & (linecount).ToString
    lblLine2.Text = "Char Count: " & TextBox1.Text.Length

    If linecount > 6 Then
      TextBox1.Focus()
      SendKeys.Send("{BKSP}")
    End If

  End Sub

When I use an UltraTextEditor control  the code does not work.

I need to restrict the number the lines. Is there a way to do this?

John 

Parents
No Data
Reply
  • 4940
    Offline posted

    Not to take away from the valuable information already shared, but isn't using the Win32 API for something as trivial as a line count a little over the top?

    Another way to impose a line limit is to use the KeyDown event on the UltraTextEditor. Using this event we can call a method to get the line count, and then allow the key press or suppress it.

    C#

     

    private int CountLinesInString(string s)
    {
        int count = 1, start = 0;
        while ((start = s.IndexOf('\n', start)) != -1)
        {
            count++;
            start++;
        }
        return count;
    }
    
    private const int MAXLINES = 6;
    private void txtEditor_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
            return;
    
        String PendingValue = txtEditor.Text + (char)e.KeyValue;
        if (CountLinesInString(PendingValue) > MAXLINES)
            e.SuppressKeyPress = true;
    }
    

     

    VB

     

    Private Function CountLinesInString(s As String) As Integer
    	Dim count As Integer = 1, start As Integer = 0
    	While (InlineAssignHelper(start, s.IndexOf(ControlChars.Lf, start))) <> -1
    		count += 1
    		start += 1
    	End While
    	Return count
    End Function
    
    Private Const MAXLINES As Integer = 6
    Private Sub txtEditor_KeyDown(sender As Object, e As KeyEventArgs)
    	If e.KeyCode = Keys.Back Then
    		Return
    	End If
    
    	Dim PendingValue As [String] = txtEditor.Text + CChar(e.KeyValue)
    	If CountLinesInString(PendingValue) > MAXLINES Then
    		e.SuppressKeyPress = True
    	End If
    End Sub
    

     

Children