How to automatically show the last characters of a long string in a edit box?
5 views (last 30 days)
Show older comments
Dear all, i am using the GUI and when i set a edit string (set(handles.edit1,'String',str)) it appears from the beginning, but i'd like to show the last parto of the long string (it is a path and i would like to show the name of the selected file but showing all the path)
Can you plese help me?
2 Comments
Geoff Hayes
on 13 Aug 2016
Luigi - you could try https://www.mathworks.com/matlabcentral/answers/154589-how-to-automatically-scroll-a-textbox-to-end-of-line though now, two years later, I can't seem to get this code to work! :)
Answers (2)
Stalin Samuel
on 13 Aug 2016
Get the file name and path name in separate variables and pass only the file name to the edit box
2 Comments
Geoff Hayes
on 15 Aug 2016
Stalin - please clarify how this will show the last characters of the long string in the edit box. And, what are the file and path name that you are referring to?
Stalin Samuel
on 16 Aug 2016
Edited: Stalin Samuel
on 16 Aug 2016
If you use the command
[FileName,PathName] = uigetfile('*.file extension','Select the file');
then name of the selected file will be stored in variable 'FileName'
Walter Roberson
on 16 Aug 2016
Put the edit box inside a uipanel.
When you have written the string into the edit box, get the "extent" property of the edit box. That basically tells you how many pixels wide and high would be needed to render what is in the string.
Now using the information you extracted from the extent property, you can set the Position of the edit box relative to the uipanel so that the end of the text is visible inside the uipanel.
If the user is to be able to edit the contents of the box, then you will need a KeyPressCallback to re-run the calculation of the size. You would not be able to use click positioning to move around in the edit box without the code getting confused.
Caution: if you get the String property of the edit box while you are inside a KeyPressCallback then it will not reflect any changes made to the contents since the last time the edit box either had return pressed or lost focus. You need to keep track of all characters typed by the user including taking into account backspaces. And remember that if you are using a proportional font then you cannot just multiply number of characters by a width to figure out the number of pixels needed to render: you will need to toss the text into an edit box and ask for its extent (and then either hide or destroy the temporary edit box before it becomes visible to the user.)
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!