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
Hi,
Have you tried this outside of TextChanged?
I just tested it out using a button with AlwaysInEditMode set to both true and false and it's working fine for me either way. I didn't try it inside the TextChanged event, but perhaps you are running into some sort of timing issue with that event.
Hello,
I am using the NetAdvantage 2011 volume 1.
Unfortunately, the GetLineCount API call is still not working. The number of rows is allways 0. I am using AllwaysInEditMode = false for my UltraTextEditor.
const int EM_GETLINECOUNT = 0xBA; public static void AutoSize(UltraTextEditor ut, int minLines, int maxLines) { var rows = (int)SendMessage(ut.Handle, EM_GETLINECOUNT, IntPtr.Zero, IntPtr.Zero); ut.Height = rows * ut.Font.Height + 1 + ut.Margin.Vertical; } [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
When I try to use AllwaysInEditMode to true, the number of line counted is too high when I set the text property programatically. I am using this AutoSize function in the Text_Changed event.
When I am editing the text inside the box, it works fine as soon I hit a key that trigger a line count change (as return, or erasing a CR)
Can I have a confirmation whether this is a bug or not, or maybe I am missing something ?
Thank you ^^
Hi Harry,
In NetAdvantage 2010 Volume 2, we added support for GetLineCount and some other SendMessage calls on our editor controls. So you can now use them directly.
Anyway... I don't think I have a sample of the syntax for this. I recommend checking out Microsoft's documentation on calling API's from DotNet. Or just do a search on the web, there are a few sites out there that have already written the declarations of these API's for you.
Also note that the API you are looking for here is SendMessage. EM_GETLINECOUNT is the message you will send, not the API itself.
John;
I'd like to use your code, with the .Controls[0] modification...what's the declaration for EM_GETLINECOUNT?
Do I need a Using System.xml or something ?
Thanks.
Harry Gross
The SendMessage API will not work on UltraTextEditor, because it's not a windows TextBox.
However, when the UltraTextEditor is in edit mode, it displays a derived TextBox control over itself to allow the user to edit. So if AlwaysInEditMode is true or the control has focus, you can get the TextBox control by using something like:
UltraTextEditor1.Controls(0)
And call SendMessage on the child textbox and that might work.