How to display data in textbox from a choosen name in listbox?
2 次查看(过去 30 天)
显示 更早的评论
hi my dearest frend, i need some help. I have about 200 names with their personal data. The thing is I dont know how to display data when I choose a certain name in listbox.
here is my coding:
function listbox1_Callback(hObject, eventdata, handles)
a= get(handles.listbox1, 'String');
b= get(handles.listbox1, 'Value');
if(find(strcmp(a,'MOHD RUSDI BIN xxxx')==b))
set(handles.edit6,'String', '780xxxxxxxx');
set(handles.edit7,'String', '7044xxxxxx');
set(handles.edit8,'String', '3x');
set(handles.edit9,'String', 'CIMx');
elseif(find(strcmp(a,'WAN YUSOF BIN xxxxx')==b))
set(handles.edit6,'String', '78xxxxxxxxxx');
set(handles.edit7,'String', '147xxxxxxxxxx');
set(handles.edit8,'String', '3x');
set(handles.edit9,'String', 'CIMx');
elseif(find(strcmp(a,'MD AZHAR BIN xxxx')==b))
set(handles.edit6,'String', '720xxxxxxxxxxxx);
set(handles.edit7,'String', '10xxxxxxxxxxxxxx');
set(handles.edit8,'String', '2x');
set(handles.edit9,'String', 'MBx');
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1016995/image.jpeg)
I want the data displayed on NoIc/passport, akaun,kod bank, and bank boxes when i click on a name in the listbox.
Thank you for your time.
0 个评论
采纳的回答
Voss
2022-5-31
Something like this is what you're after:
function listbox1_Callback(hObject, eventdata, handles)
val = get(handles.listbox1, 'Value');
if ~isscalar(val)
% if no selection in listbox1, or more than one item selected,
% set the edit Strings to empty and return
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
return
end
str = get(handles.listbox1, 'String');
if ischar(str)
% if listbox1 String is a character array, make it a scalar cell array
str = {str};
end
if strcmp(str{val},'MOHD RUSDI BIN xxxx')
set(handles.edit6,'String', '780xxxxxxxx');
set(handles.edit7,'String', '7044xxxxxx');
set(handles.edit8,'String', '3x');
set(handles.edit9,'String', 'CIMx');
elseif strcmp(str{val},'WAN YUSOF BIN xxxxx')
set(handles.edit6,'String', '78xxxxxxxxxx');
set(handles.edit7,'String', '147xxxxxxxxxx');
set(handles.edit8,'String', '3x');
set(handles.edit9,'String', 'CIMx');
elseif strcmp(str{val},'MD AZHAR BIN xxxx')
set(handles.edit6,'String', '720xxxxxxxxxxxx');
set(handles.edit7,'String', '10xxxxxxxxxxxxxx');
set(handles.edit8,'String', '2x');
set(handles.edit9,'String', 'MBx');
else
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
end
However, since you have 200 names with data, this approach would require you to type out the hard-coded information (NoIc/passport, akaun,kod bank, and bank) into 200 if/elseif blocks. Not only is that a really tedious task, it would require you to modify the code any time your data set changes, e.g., if a new name is added, you have to add a new elseif.
It would be much better to store the names and associated data in some variable or file somewhere and have your GUI operate on whatever that variable or file contains. That allows the GUI to be independent of the data, simplifies the GUI code, and makes your life easier.
In this case, you might use a struct array to store stuff:
handles.data = struct( ...
'name', {'MOHD RUSDI BIN xxxx' 'WAN YUSOF BIN xxxxx' 'MD AZHAR BIN xxxx'}, ...
'passport',{'780xxxxxxxx' '78xxxxxxxxxx' '720xxxxxxxxxxxx'}, ...
'akaun', {'7044xxxxxx' '147xxxxxxxxxx' '10xxxxxxxxxxxxxx'}, ...
'kod_bank',{'3x' '3x' '2x'}, ...
'bank', {'CIMx' 'CIMx' 'MBx'});
There I am still hard-coding the information, but it would be better if you store the information in a file, say a spreadsheet, and then read the file and build the same sort of struct array in your GUI's OpeningFcn.
Then, given that handles.data has all the information you need, your listbox callback can be simpler:
function listbox1_Callback(hObject, eventdata, handles)
val = get(handles.listbox1, 'Value');
if ~isscalar(val)
% if no selection in listbox1, or more than one item selected,
% set the edit Strings to empty and return
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
return
end
str = get(handles.listbox1, 'String');
if ischar(str)
% if listbox1 String is a character array, make it a scalar cell array
str = {str};
end
idx = find(strcmp({handles.data.name},str{val}),1);
if isempty(idx)
% selected name not found in handles.data
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
return
end
% populate edit boxes with data
set(handles.edit6,'String',handles.data(idx).passport)
set(handles.edit7,'String',handles.data(idx).akaun)
set(handles.edit8,'String',handles.data(idx).kod_bank)
set(handles.edit9,'String',handles.data(idx).bank)
Or, if the listbox String is populated with the names from handles.data in the first place, its callback can be simplified further, because you can use the listbox Value as the index into handles.data without having to do any strcmp on the listbox String vs the names:
function listbox1_Callback(hObject, eventdata, handles)
val = get(handles.listbox1, 'Value');
if ~isscalar(val)
% if no selection in listbox1, or more than one item selected,
% set the edit Strings to empty and return
set([handles.edit6 handles.edit7 handles.edit8 handles.edit9],'String','');
return
end
% populate edit boxes with data
set(handles.edit6,'String',handles.data(val).passport)
set(handles.edit7,'String',handles.data(val).akaun)
set(handles.edit8,'String',handles.data(val).kod_bank)
set(handles.edit9,'String',handles.data(val).bank)
3 个评论
Voss
2022-6-3
Hello Mr. Voss, Thank you for your willingness to help me. Each one of your suggestions works perfectly fine on my GUI.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!