How to automatically show the last characters of a long string in a edit box?

4 次查看(过去 30 天)
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 个)

Stalin Samuel
Stalin Samuel 2016-8-13
Get the file name and path name in separate variables and pass only the file name to the edit box
  2 个评论
Geoff Hayes
Geoff Hayes 2016-8-15
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
Stalin Samuel 2016-8-16
编辑:Stalin Samuel 2016-8-16
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
Walter Roberson 2016-8-16
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.)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by