Welcome! Log In Create A New Profile

Advanced

[WD23] How to insert a string at cursor position? [Solved!]

Posted by Ola 
Ola
[WD23] How to insert a string at cursor position? [Solved!]
October 05, 2020 03:44PM
Hi all,

I am trying to find a way to insert a string in a multi-line memo-type field at the cursor position.
The problem is, how to get the cursor position. The online help gives wrong advise:
MySelf = Left(MySelf, MySelf..Cursor) + sString + Right(MySelf, MySelf..Cursor)
Unfortunately that does not work. When I am testing, regardless where the cursor is (it is in the field!), it keeps the first and the last character of Myself and puts the sString between them. Everything else in Myself is sent to bit-heaveneye popping smiley.

The cursor property has no clue at all, at which row it is.

Any help will be appreciated.

Best regards
Ola



Edited 1 time(s). Last edit at 10/07/2020 05:03PM by Ola.
Re: [WD23] How to insert a string at cursor position?
October 06, 2020 08:24AM
Hi Ola,

The insertion should be take action from a Button. The Button must be an Interrupt type. So the use of keyword Myself is not valid and you must use the name of the Edit Control.

I hope to help.

Vassilis Boutsikas
Hi,

Something like this...

ls_Word is string="Hello"
li_Start is int=Edit1..Cursor
Edit1=Left(Edit1,li_Start)+ls_Word+Middle(Edit1,li_Start+1,Length(Edit1))
Ola
Re: [WD23] How to insert a string at cursor position?
October 06, 2020 08:27PM
Hello Vassilis and Harry,

Thank you for the hints. I tried them. No luck. Exactly the same result as described in my first message. The problem is the ..Cursor property. It gives a correct result only on a one-line string field, but not on a multi-line memo-type text field.

Any other hints, tips, ideas?

Best regards
Ola



Edited 1 time(s). Last edit at 10/06/2020 08:40PM by Ola.
Re: [WD23] How to insert a string at cursor position?
October 07, 2020 10:20AM
Hi Ola,

In order to overcome the limitation of multi-line memo, maybe you can copy the contents of edit field to a String and with some string operations within it (extractstring, contains, ....) you can insert the sString you want to String and then assign it back to the memo control.

Regards

Vassilis Boutsikas
Ola
Re: [WD23] How to insert a string at cursor position?
October 07, 2020 12:54PM
Thanks Vassilis,

I thought that too, but it does not solve the basic problem of not knowing the cursor position. So I don't know, where to insert the string, regardless whether the text is in the control or in a separate string. When the control contents is moved to a separate string, the cursor is no longer there, so it also cannot have a position.

The text has to be cut in two at the cursor postition in order to be able to insert the string, but there seems to be no way of finding the cursor position in a multi line text. It might help if I could somehow find on which row the cursor is and then check the cursor position in that row, but I have found no way to calculate the rows in a multi-line text control. And the number of rows can change when the control size changes, so it would never be the same. Well, even knowing the cursor row would be sufficient, because the insertion would probably be done on an empty row.

I'm really astonished not to be able to do this very simple text processing operation in Windev. A typical use case would be answering an email: We have the original text at the bottom of the control, then we add the reply text at the top, and finally we would like to add a standard piece of text, the replyer's signature, between them, at the cursor position.

Or maybe I'm just missing something very obvious, as usual?

Best regards
Ola
Re: [WD23] How to insert a string at cursor position?
October 07, 2020 01:03PM
Hi Ola,

I know that is very difficult to "predict" the cutting position using string functions, instead of human eye-brain, but could you give to the system several keywords to search for?
Something like AI.

Regards

Vassilis Boutsikas
Re: [WD23] How to insert a string at cursor position?
October 07, 2020 02:35PM
Hi Ola,

I did a small test and it works for me. I pput this piece of code in a button and WINDEV is perfectly inserted at the right spot, no matter what line the cursor is on. (btw: you don't use the proper count in the Right() part)

Trace(EDT_Test,,Cursor)
EDT_Test = Left(EDT_Test, EDT_Test..Cursor - 1) + "WINDEV" + Right(EDT_Test, Length(EDT_Test)-EDT_Test..Cursor + 1)

However, when I did a debug to follow the code I saw a very short redraw and the cursor moved to the beginning of the control, hence cursor=1 !!

So maybe it has to do with the way you start the insertion? Maybe there are other actions in between (like opening a window for the user to enter text), causing the cursor to be moved ?

Arie



Edited 1 time(s). Last edit at 10/07/2020 04:39PM by ArieM.
Ola
Re: [WD23] How to insert a string at cursor position?
October 07, 2020 03:05PM
Hi Arie,

You seem to have found the reason for the the problem!
Indeed, I do open another window in the middle of the process, to select the standard text to be inserted! I'll recheck my code with this knowlegde.
Thank you!

Best regards
Ola
Ola
Re: [WD23] How to insert a string at cursor position? [Solved!]
October 07, 2020 04:53PM
Hi again!

Arie hit the bull's eye! Now I got the code working:

//This is the code in the "Key down" event of the text control:
//=============================================================
sPos is int = MySelf..Cursor //The cursor position must be saved first!
sString is string = ""
IF KeyPressed(kpControl) AND KeyPressed(Asc("G")) THEN //G = "Get"
	//Select the text to be inserted from a parameter file:
	IF Open("YPARAMI_TBLQ_PARKOHDE","Select","TEXT","STD","",0,"","",sString) THEN
		//Cut the text in two parts and insert sString between them:
		nLenS is int = Length(sString) //Length of sString
		sLeft is string = Left(MySelf,sPos) //The first part
		nLen1 is int = Length(sLeft) //Length of the 1st part
		sRight is string = Middle(MySelf,sPos) //The 2nd part
		MySelf = sLeft + sString + sRight //Concatenate all
		//Place the cursor at the end of the inserted string:
		MySelf..Cursor = nLenS + nLen1 + 1
		Soundi("DONE") //Wake up the user
	END
END
Thank you Arie for opening my eyes! Thanks also to Vassilis and Harry; it's always good to get suggestions from other developers. It makes one think the problem from many sides.

Best regards
Ola



Edited 2 time(s). Last edit at 10/07/2020 05:03PM by Ola.
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: