Obtain the row, column location, and location of textbox multi-row text in C #

Source: Internet
Author: User

 

When imitating the notebook provided by windows for practice, it is found that the textbox control does not directly obtain the current row and column location, nor does it have the positioning function. After checking the information, we can use Windows API to implement this function. The sendmessage function and em_linefromchar (0xc9) and em_lineindex (0xbb) Message constants must be introduced or defined by ourselves. The following is the test code.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsapplication7
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

// Introduce lresult sendmessage (hwnd, uint MSG, wparam, lparam iparam );
[System. runtime. interopservices. dllimport ("user32.dll")]
Public static extern int sendmessage (intptr hwnd, uint MSG, int wparam, int iparam );

Private const int em_linefromchar = 0xc9;
Private const int em_lineindex = 0xbb;

// Obtain the row and column position
Private point getcursorpos (textbox)
{
Point cursorpos = new point (0, 0 );
Int X, Y;
Y = sendmessage (textbox. Handle, em_linefromchar, textbox. selectionstart, 0 );
X = textbox. selectionstart-sendmessage (textbox. Handle, em_lineindex, Y, 0 );
Cursorpos. Y = ++ y;
Cursorpos. x = ++ X;
Return cursorpos;
}

// After testing, code must be added to the click event, keyup event, and keydown event of textbox in C # To obtain the row and column positions in real time.
Private void textbox#click (Object sender, eventargs E)
{
Point cursorpos = getcursorpos (this. textbox1 );
This. Text = cursorpos. tostring ();
}

Private void textbox1_keyup (Object sender, keyeventargs E)
{
Point cursorpos = getcursorpos (this. textbox1 );
This. Text = cursorpos. tostring ();
}

Private void textbox1_keydown (Object sender, keyeventargs E)
{
Point cursorpos = getcursorpos (this. textbox1 );
This. Text = cursorpos. tostring ();
}

// Locate the row number. em_lineindex obtains its character position based on the specified row number and specifies the return value to selectionstart for positioning.
Private void button#click (Object sender, eventargs E)
{
This. textbox1.selectionstart = sendmessage (this. textbox1.handle, em_lineindex, 3, 0 );
This. textbox1.focus ();

Point cursorpos = getcursorpos (this. textbox1 );
This. Text = cursorpos. tostring ();
}
}
}

Note:

Em_linefromchar is used to obtain and set the current row number based on the "character position". The character position refers to the number of characters from the start to the current position;
Em_lineindex is used to locate the row number. It is actually used to modify the character position to locate the starting position of the specified row.

Help on em_linefromchar in Delphi
An application sends an em_linefromchar message to retrieve the index of the line that contains the specified character index in a multiline edit control. A character index is the number of characters from the beginning of the Edit Control.

Em_linefromchar
Wparam = (wparam) Ich; // Character index
Lparam = 0; // not used; must be zero
 
Parameters

Ich
Value of wparam. specifies the character index of the character contained in the line whose number is to be retrieved. If
Ich parameter is-1, either the line number of the current line (the line containing the caret) is retrieved or, if there is a selection, the line number of the line containing the beginning of the selection is retrieved.

Return values
The return value is the zero-based line number of the line containing the character index specified by ICH.

Remarks
In a rich edit control, if the character index is greater than 64 K, use the message em_exlinefromchar.

Help on em_lineindex in Delphi
An application sends an em_lineindex message to retrieve the character index of a line in a multiline edit control. The character index is the number of characters from the beginning of the Edit Control to the specified line.

Em_lineindex
Wparam = (wparam) line; // line number
Lparam = 0; // not used; must be zero
 
Parameters

Line
Value of wparam. specifies the zero-based line number. A value of-1 specifies the current line number (the line that contains the caret ).

Return values
The return value is the character index of the line specified in the line parameter, or it is-1 if the specified line number is greater than the number of lines in the edit control.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.