Difference between revisions of "Multilines"

From Team Developer SqlWindows Wiki
Jump to: navigation, search
m (Added sample to multiline text selection tip)
m (Updated the text selection tip)
Line 6: Line 6:
 
<!------------------------------------------------------------------------------------------------------------------------------>
 
<!------------------------------------------------------------------------------------------------------------------------------>
 
{{TipHeader|How to place the cursor at a specific location in the text, text selection and insertion}}
 
{{TipHeader|How to place the cursor at a specific location in the text, text selection and insertion}}
 +
This applies for datafields and multilines.<br>
 +
<br>
 +
Using Windows API messages you can do the following:<br>
 +
<br>
 +
*Set the cursor/caret at a specific location within the text
 +
*Select a portion or all of the text
 +
*Insert text at a specific location within the text
 +
*Replace a preselected portion of text with another text
 +
*Delete a preselected portion of text
 +
*Get the current location/selection of text
 +
<br>
 
First define these WinApi constants
 
First define these WinApi constants
 
<pre>
 
<pre>
Line 14: Line 25:
 
</pre>
 
</pre>
  
To place the cursor/caret at a specific character location (nPos).<br>
 
<sub>0 (zero) means the first location from the left</sub>
 
  
 +
=== Place the cursor/caret at a specific character location ===
 +
 +
The first character position from the left is 0 (zero).<br>
 +
To position the cursor/caret to a specific location, send EM_SETSEL message to the field having wParam and lParam the same value.<br>
 +
<br>
 
<pre>
 
<pre>
 
   ! Place the cursor at position 200 in the text. Set left and right position at the same value
 
   ! Place the cursor at position 200 in the text. Set left and right position at the same value
Line 26: Line 40:
 
   Call SalSendMsg( hWndField, EM_SCROLLCARET, 0, 0 )
 
   Call SalSendMsg( hWndField, EM_SCROLLCARET, 0, 0 )
 
</pre>
 
</pre>
 +
<br>
 +
To set the cursor at the end of the text, use -1 in wParam and lParam.<br>
 +
<br>
 +
===Select portion of the text===
  
When text should be selected, use different values for nLeftPos and nRightPos
+
For this you have to define the starting and ending character position.<br>
 +
The wParam holds the start and lParam the end position.<br>
  
 
<pre>
 
<pre>
Line 35: Line 54:
 
</pre>
 
</pre>
  
To deselect text use
+
To select all the text in the field, use wParam = 0 and lParam = -1
 +
 
 +
===Deselect text===
 +
 
 +
To deselect the text, use -1 as value for wParam and lParam.<br>
 +
 
 
<pre>
 
<pre>
 
   Call SalSendMsg( hWndField, EM_SETSEL, -1, -1 )
 
   Call SalSendMsg( hWndField, EM_SETSEL, -1, -1 )
 
</pre>
 
</pre>
  
To insert text at a specific location
+
===Insert text at a specific location===
 +
 
 +
First place the cursor at the location you want to insert new text (see above).<br>
 +
Then send the EM_REPLACESEL message to the field using VisSendMsgString.<br>
 +
 
 
<pre>
 
<pre>
 
   ! Place the cursor at position 200 in the text. Set left and right position at the same value
 
   ! Place the cursor at position 200 in the text. Set left and right position at the same value
Line 51: Line 79:
 
</pre>
 
</pre>
  
To replace text which is selected, like above except use the appropriate nLeftPos and nRightPos values.
+
===Replace text which is selected===
 +
 
 +
Like above except use the appropriate nLeftPos and nRightPos values.<br>
 +
So first select the part of the text and then send the EM_REPLACESEL message.<br>
 +
<br>
 +
 
 +
===Get the current position/selection boundaries===
 +
 
 +
Send the EM_GETSEL message to the field.<br>
 +
It will return the start and end position of the cursor/caret.<br>
 +
The return value contains the start position in Low number and end position in the High number.<br>
  
Now to get the current position/selection boundaries
 
 
<pre>
 
<pre>
 
   Set nReturn = SalSendMsg( hWndField, EM_GETSEL, 0, 0 )
 
   Set nReturn = SalSendMsg( hWndField, EM_GETSEL, 0, 0 )

Revision as of 11:21, 17 December 2012

This page covers Multiline tips & tricks.

Contents


Pointer2.png How to place the cursor at a specific location in the text, text selection and insertion Pointer.png

This applies for datafields and multilines.

Using Windows API messages you can do the following:

  • Set the cursor/caret at a specific location within the text
  • Select a portion or all of the text
  • Insert text at a specific location within the text
  • Replace a preselected portion of text with another text
  • Delete a preselected portion of text
  • Get the current location/selection of text


First define these WinApi constants

   Number: EM_GETSEL         = 0x00B0
   Number: EM_SETSEL         = 0x00B1
   Number: EM_SCROLLCARET    = 0x00B7
   Number: EM_REPLACESEL     = 0x00C2


Place the cursor/caret at a specific character location

The first character position from the left is 0 (zero).
To position the cursor/caret to a specific location, send EM_SETSEL message to the field having wParam and lParam the same value.

   ! Place the cursor at position 200 in the text. Set left and right position at the same value
   Set nLeftPos = 200
   Set nRightPos = 200
   Call SalSendMsg( hWndField, EM_SETSEL, nLeftPos, nRightPos )
   !
   ! When the cursor is out of view, the field does not scroll automatically, so you should scroll it by using :
   Call SalSendMsg( hWndField, EM_SCROLLCARET, 0, 0 )


To set the cursor at the end of the text, use -1 in wParam and lParam.

Select portion of the text

For this you have to define the starting and ending character position.
The wParam holds the start and lParam the end position.

   Set nLeftPos = 200
   Set nRightPos = 210
   Call SalSendMsg( hWndField, EM_SETSEL, nLeftPos, nRightPos )

To select all the text in the field, use wParam = 0 and lParam = -1

Deselect text

To deselect the text, use -1 as value for wParam and lParam.

   Call SalSendMsg( hWndField, EM_SETSEL, -1, -1 )

Insert text at a specific location

First place the cursor at the location you want to insert new text (see above).
Then send the EM_REPLACESEL message to the field using VisSendMsgString.

   ! Place the cursor at position 200 in the text. Set left and right position at the same value
   Set nLeftPos = 200
   Set nRightPos = 200
   Call SalSendMsg( hWndField, EM_SETSEL, nLeftPos, nRightPos )
   !
   ! Now insert some text at the current caret position
   Call VisSendMsgString( hWndField, EM_REPLACESEL, TRUE, "This will be inserted" )

Replace text which is selected

Like above except use the appropriate nLeftPos and nRightPos values.
So first select the part of the text and then send the EM_REPLACESEL message.

Get the current position/selection boundaries

Send the EM_GETSEL message to the field.
It will return the start and end position of the cursor/caret.
The return value contains the start position in Low number and end position in the High number.

   Set nReturn = SalSendMsg( hWndField, EM_GETSEL, 0, 0 )
   !
   Set nLeftPos = SalNumberLow( nReturn ) 
   Set nRightPos = SalNumberHigh( nReturn ) 

If no selections are performed above, you should first set the focus to the field

   Call SalSetFocus( hWndField )

Here you can download a sample:



Pointer2.png How to make a scrollable but disabled multiline Pointer.png

When you disable the field, the scrollbars are also disabled.

Use this instead to have scrolling

   Call VisWinSetFlags ( hWndField, WF_DisplayOnly, TRUE )