06-04-2013 12:36 PM
Hi All,
My goal is to be able to type a multi line text in a string control during run-time that will have certain horizontal limit (indicating how many characters are allowed) as well as certain vertical limit (indicating how many lines are allowed). So for an example if I press the Run button to start VI and start typing text in the string control. It should allow me to type only 19 characters in the first line. Once that point is reached I should be able to continue writing and the program should be smart enough to bring the cursor to the second line and allow me to continue writing until another set of 19 characters has been reached. While this process is being executed I should be able to check if I have reached the maximum number of lines allowed in this case it is 7 lines.
I struggled for few days trying to make this happen but with no luck, therefore I would be really grateful if you guys can help me resolve this problem. I have attached my VI and I am using LV2011.
Thank you
06-04-2013 12:43 PM
I don't have LV in front of me, so I can't look at the code or test this, but I would suggest the following - use the Key Down? event for the control (note the ? which means it's a filter event which allows you to play with the event) to monitor the keyboard. As you get events, check the string. Once the string reaches the relevant length, add a line break to it inside the event case and write it to a local variable. This will give you the line breaks.
Once the string reaches its final length, you can use the Discard? terminal on the right side of the event to ignore any further key down events (although I would suggest always accepting backspace, delete and the arrow keys).
Note that this method won't prevent the user from pasting long text into the control.
06-05-2013 02:37 PM
Hi tst,
First thank you very much for the fast response. I did take your suggestions into consideration and I got it to work partially. I was able to ignore the Key Down event whenever the string has reached its full size. However I was not able to get line break working whenever the string reaches relevant length. Basically whenever it reaches 19 characters and if I keep typing, cursor will not go to the next line instead it keeps filling up that line and then it goes to the next line. Also I would really appreciate if you could show me how to allow backspace,delete and arrow keys in the Key Down event.
I have attached a revised version of my VI.
Thank you
06-06-2013 03:30 AM
OK, so testing this I can see that this doesn't actually work, because the control won't update with the text you add. Your code is wrong because it simply writes a line feed to the control (which overwrites everything else), but my suggestion (which was to add a line feed to the actual value) doesn't work either. One option would be to replace the 19th char with a line feed, but that would cause you to lose a character.
While it probably is possible to write code which will handle this, there's probably a much easier way - find a fixed-width font (do a search, they're also referred to as monospaced) and use that. Then, you can simply set the string control to be wide enough to fit less than 20 chars and it will automatically line-break every 19 chars.
As for the the other keys, look at the other terminals in the event. Specifically, the VKey terminal. You will have not to discard if one of those combinations happens. Checking what values you need is easy enough by creating indicators from the terminals and seeing which values you get when you use those keys.
Also, you might wish to handle the Key Repeat? event.
06-06-2013 05:30 AM
Hmm, I realized that the monospace font actually won't necessary help, because line breaks only happen in mid-word if there are no spaces. If there are spaces, the line will be broken earlier.
The easiest solution I can think of in terms of code is to separate the input from the display - have one control for processing the key down events and another where you will actually show the text, like the attached quick mod. This doesn't show the cursor on the display area, but at least it works. It could probably be made more clever, but it would require coordinating the two controls if you want to select and replace text or other similar things.