How to display data from editext in listbox in GUI ?
Mostra commenti meno recenti
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

Thank you so much
6 Commenti
Jan
il 16 Gen 2019
This will not work with a standard uicontrol as "edit text" element, because it does not trigger a callback during the string is typed. But you can use the WindowsKeyPressFcn to let it look like the text is directly inserted in the textfield. Please explain, if you use GUIDE, AppDesigner or create the GUI programmatically.
Kevin Phung
il 16 Gen 2019
to add onto this and possibly save you some time, if you would like the keypress to be the enter key, the char equivalent would be char(13)
Walter Roberson
il 16 Gen 2019
char(13) is the return key, but these days the "enter" key on keyboards is generally the newline character, char(10)
Jan
il 17 Gen 2019
I do not understand, what "the keypress to be the enter key" means.
Le Dung
il 18 Gen 2019
Risposta accettata
Più risposte (1)
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 Commenti
Le Dung
il 19 Gen 2019
You have created the function myKeyPress and activated it by:
set(gcf,'KeyPressFcn',@myKeyPress)
The error message means, that the GUI searchs for inputword_KeyPressFcn. Try to rename "myKeyPress" to "inputword_KeyPressFcn".
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.
Categorie
Scopri di più su Graphics Object Properties in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
