LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

cursor control in textbox set to indicator

i am implementing a command line interface using LabWindows textbox.
Since only certain portion of the cmd should be writable, I have set the whole text box as indicator and monitor KEY PRESS to indirectly print out keys that are pressed by user.
 
I have succeeded most of the part, but I don't know how to place the cursor where I want it to be.
 
1. for example, pressing "UP ARROW" displays previous commands just like in DOS cmd. The problem is that the cursor moves up as the upper arrow key is pressed while i want it to stay in the same line. Is there any way I can "lock-up" the place of cursor if i want to?
 
2. Another problem with the cursor is that user can't modify the command as he writes. Of course backspace works fine, but if user wants to use left or right arrow to correct typos.
eg) user types "ntional" and realizes he needs to add 'a' to write 'national'. Pressing left arrow moves the cursor left, but if the user types 'a', it actually adds at the end of the string.
 
Please help me!!!
 
THank you.
 
Justin
0 Kudos
Message 1 of 9
(5,112 Views)
1. Keyboard and mouse events can be swallowed (=ignored) by returning a non-zero value in the control callback. So, since you are managing by yourself the update of the string, you can simply swallow the up- or down- arrow key and all keys you want your control to ignore.
 
2. This behaviour is probably due to the facto yuo are using SetCtrlVal to add text to the textbox. SetCtrlVal always append text to the textbox. To insert some text inside a line, you can elaborate the new line wih the letters added and use ReplaceTextBoxLine instead of SetStrlVal


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 9
(5,095 Views)
An example of swallowing mouse and keyboard events on a string control can be found in the source code for the password control (pwctrl.c located in <cvidir>\toolslib\custctrl, function PasswordCtrl_Callback).


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 3 of 9
(5,087 Views)

Thank you so much.

#1 is solved by returning 1. Thank you again.

#2 is still not solved.

doens't ReplaceTextBoxLine always input a newline after the replace?

I don't see the use of ReplaceTextBoxLine to edit one line of string.

Please help...Thank you.

Justin

0 Kudos
Message 4 of 9
(5,067 Views)
Assuming the cursor is on some point inside the textbox, with this code you can edit the line the cursor is on:
 
    GetCtrlAttribute (panel, control, ATTR_TEXT_SELECTION_START, &idx);
    GetTextBoxLineIndexFromOffset (panel, control, idx, &line);
    GetTextBoxLineOffset (panel, control, line, &start);
    GetTextBoxLine (panel, control, line, msg);
    sprintf (target, "%.*s%c%s", idx - start, msg, asciiCode, msg + idx-start);
    ReplaceTextBoxLine (panel, control, line, target);
    SetCtrlAttribute (panel, control, ATTR_TEXT_SELECTION_START, ++idx);

Here some explanations:
ATTR_TEXT_SELECTION_START attribute gives you the start of selected text. If no text is selected (which should be your case) gives you the position of the cursor in bytes (=characters) from the start of the textbox
From this value, I determine the line the cursor is in and the offset of the first character of the line (from which the position of the cursor inside the line can be determined).
Next I update the line and write it back to the textbox.
The last line of code restores cursor position after the new character written (ReplaceTextBoxLine places the cursor at the beginning of the line).
 
This should solve your problem
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 5 of 9
(5,049 Views)
Thank you. It worked out great!!!
I just have one more question.
you had
 
sprintf (target, "%.*s%c%s", idx - start, msg, asciiCode, msg + idx-start);
 
What does "%.*s" mean? It looks like there should be only 3 parameters since there are 3%'s.
However, you had 4 parameters, and i would like to know how "target" string is made.
 
Thank you for your help again.
 
Justin 
0 Kudos
Message 6 of 9
(4,997 Views)
In sprintf (and related functions) you can specify the field width by using the precision specifier (.).  For strings this specifies the maximum number of characters to print.  The * allows the precision specifier to consume one of the parameters in the function call.  This lets you programatically specify the number of characters to print from the source string.  In this example, it allows you to use a substring of the msg string without first creating a separate array to hold a null-terminated version of it.

Assuming that:
msg = "Hand out stars please."
start = 0
idx = 10
asciiCode = '5'

Target would be made of:
The first 10 characters of msg: "Hand out " + asciiCode + the remaining characters of msg starting at index 10: "stars please." or
"Hand out 5 stars please."

----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
Message 7 of 9
(4,994 Views)
Thanks, that really helped.
 
Then How would i be able to get rid of a character in the middle of a string programmatically?
 
Assuming the place of the cursor is acquired by  GetCtrlAttribute (panelHandle, PANEL_CMD, ATTR_TEXT_SELECTION_START, &idx);
 
I know how to remove a character at the end of the string, but a character in the middle of string is tricky.
 
I was thinking of cutting a string into two strings. remove the last character of the first string, and then concatenate two strings.
I think there is function that splices a string at desired character length(in this case, it will be idx).
can anyone help>???
Thanks,
 
JL
0 Kudos
Message 8 of 9
(4,966 Views)
Well, let's take what we just did and go from there...

char szSourceString[ 256 ];
char szResultString[ 256 ];
int idx;

GetCtrlVal  (panelHandle, PANEL_CMD, szSourceString );
GetCtrlAttribute (panelHandle, PANEL_CMD, ATTR_TEXT_SELECTION_START, &idx);
sprintf (szResultString, "%.*s%s", idx, szSourceString, szSourceString+idx+1);
SetCtrlVal (panelHandle, PANEL_CMD, szResultString );

You will need to handle the case where no text is selected and the cursor is at the end of the line.
----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 9 of 9
(4,962 Views)