How to delete a component in a listbox?

hi I used the below code for adding components for a listbox and it finally works but know I want to know if I want to delete a component from the listbox what should I do please? hope you can help me guys
this is the code that I have used
prompt=('please enter the component name: '); name=('input component name'); numlines=1; defaultanswer={''};
% prompt the user for some input answer = inputdlg(prompt,name,numlines,defaultanswer,'on'); % get the current list box selections selections = get(handles.listbox1,'String'); % append the answer to the list selections = [selections ; answer]; % update the list box with the new selection set(handles.listbox1,'String',selections);

 採用された回答

Ben11
Ben11 2014 年 7 月 14 日
編集済み: Ben11 2014 年 7 月 14 日

0 投票

You do pretty much the same thing as before, i.e. modify the string array and put it back in the listbox with the set(...) command.
Let's say your string looks like this:
selections = {'A' 'B' 'C'};
Then you delete say the 2nd element:
selections(2) = []
selections =
'A' 'C'
And you can put it back in the listbox:
set(handles.listbox1,'String',selections);

7 件のコメント

Jihad Chamseddine
Jihad Chamseddine 2014 年 7 月 14 日
thanks it is working, but I have another question please. I want the entries in the list box to be numbered, for example if I add a first entry it will be number 1 and if I add another entrie it will automatically take number 2 etc..... can that be done?
Ben11
Ben11 2014 年 7 月 14 日
Great! Please accept the answer if it answers your original question (click on the green check mark at the top left of my answer).
If I understand your other question correctly, entries in a listbox are automatically "numbered" depending on their position in the cell array. For example if your listbox contains those elements:
{'Element A' 'Element B' 'Element C,}
then 'Element A' is assigned the first position, hence its "number" is one. For instance calling this line:
cellstr(get(handles.listbox1,'String'))
returns a cell array where each element occupies a position in the cell array, so in that sense they are numbered.
If you would like the entries of the listbox to actually contain numbers, for example like this:
{'1)Element A' '2)Element B' '3)Element C,}
then you have enter numbers manually.
Jihad Chamseddine
Jihad Chamseddine 2014 年 7 月 14 日
Jihad Chamseddine
Jihad Chamseddine 2014 年 7 月 14 日
as shown in the picture, when I enter the name I want to the listbox, I want it to be automatically numbered. take a look at the picture
Image Analyst
Image Analyst 2014 年 7 月 14 日
BE AWARE, if you have the last item in the listbox selected (let's say it's #4) and you delete it, now the list is only 3 items long but the value is still 4 - it doesn't automatically update - and so the listbox won't display at all (it will be invisible). You have to detect and handle that.
Nam Hoang Le
Nam Hoang Le 2017 年 2 月 16 日
Do you know how to solve this? I also had this problem
Marcel Maeder
Marcel Maeder 2018 年 12 月 13 日
You need to set your listbox value back to '1' so that the first can be selected (the listbox is made invisible since it is trying to select a number in the list that no longer exists).
set(handles.listbox,'Value',1);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by