How to display data from editext in listbox in GUI ?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi everyone!
My problem is:
I have a GUI contains a listbox and a edittext.
Listbox have a array of strings:
a
an
able
back
bath
bench
car
create
common
claim
dad
did
dear
eat
edit
english
enable
far
father
fish
jacket
jet
kiss
gather
get
gun
...
and so on
Now, i want to import a word "jacket" (for example) on edittext. i expect when i import word "j" on edittext, the listbox will display all word that begin with word "j". Similarly, when i import consecutive word, "a", (now, i'm having "ja" on edittext). the listbox will display all word that contains words "ja". and so on![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200855/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200855/image.png)
Thank you so much
댓글 수: 6
채택된 답변
Kevin Phung
2019년 1월 16일
the contains() function can help you out here, or if you want to get fancy, regexp()
Set your callback for the 'edit' box to retrieve the strings from the list box,
and do
contains(list_box_string,edit_box_string)
This will give you a logical array, simply use that array as indexing for the strings in the listbox.
Note: You may want to save the main list under a variable to be called when edit box is empty, so that you preserve it and it doesnt get lost when you change the string of the listbox.
Let me know if this helps or if you need more clarification.
댓글 수: 13
Kevin Phung
2019년 1월 21일
Nope, Chinese. But my parents are from there so vietnamese culture is heavily a part of mine!
추가 답변 (1개)
Jan
2019년 1월 18일
편집: Jan
2019년 1월 22일
UNTESTED!!!
function yourGUI
StrList = {'a', 'an', 'able', 'back', 'car', 'create'};
H.Fig = figure('Position', [100, 100, 400, 600], ...
'KeyPressFcn', @myKeyPress);
H.Input = uicontrol(H.Fig, 'Style', 'edit', 'Position', [10, 550, 200, 30], ...
'Enable', 'inactive', 'String', '');
H.List = uicontrol(H.Fig, 'Style', 'listbox', 'Position', [10, 10, 200, 530], ...
'String', StrList, ...
'Min', 0, 'Max', 2); % Max-Min > 1: Multiple selections allowed
guidata(H.Fig, H);
end
function myKeyPress(FigH, EventData)
H = guidata(FigH);
Str = get(H.Input, 'String');
StrList = get(H.List, 'String');
Match = find(strncmpi(Str, StrList, length(Str)));
switch edata.Key
case 'return'
% If only 1 string in the string list is matching,
% RETURN copies it to the edit field:
if numel(Match) == 1
Str = StrList{Match}; % [EDITED]
end
case 'backspace'
if ~isempty(Str)
Str(end) = [];
end
case 'delete'
... ???
% case Arrow keys?!
otherwise
c = EventData.Character;
if ~isempty(c)
Str = [Str, c];
end
end
set(H.Input, 'String', Str);
Match = find(strncmpi(Str, StrList, length(Str)));
set(H.List, 'Value', Match); % Select all matching strings in the list
if isempty(Match) % Move fiorst selected string to top
set(H.List, 'ListBoxTop', 1);
else
set(H.List, 'ListBoxTop', Match(1));
end
end
This function was written inthe forum's editor and not checked. It is thought only to demonstrate, how the KeyPressFcn of the figure can be used to emulate an edit field with running a callback after each keystroke.
댓글 수: 5
Jan
2019년 1월 22일
편집: Jan
2019년 1월 22일
My code should behave almost the same. If you type "ju", all strings in the list starting with "ju" are selected. When only one selection is matching, pressing ENTER wil copy it.
I cannot post exactly matching code, because I do not see the original code and the GUI. But I assume, the shown methods should be sufficient to allow you to apply the required modifications.
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!