About listbox item selection: If i have two boxes how can i know from which one the cursor has selected?

I have two listboxes : listbox1 containing lines,and listbox2 containing cercles. And i have a button "Set" (as described in the image) to set the thikness of the selected item(line or circle). So, in the function "Set" it's written this : num =handles.selected % the value selected by the cursor is called num. e(num)=thiknss % Then we give this value a thikness. Now my problem is that i want to know from which box the "num" is selected.I need to know if it's from the box 1 ( a line) or the box2 (a circle). How can i do that?

Answers (1)

That is not a good situation to use listboxes in. You are expecting to take some action based on a single selection, however you will always have two selections because you have two listboxes. You should use either radio buttons, or a popup (drop down list).

7 Comments

Ok supoose that i have only one listbox in which the list is "line1, arc1, acr2, line2, line3....." ,when i click on an item for example "line2",how can i return a variable containing "line2" and not the order of the item in the list? If i do this i will be able to check if the item selected is from the first or the second box.
Try this:
% Get item name
selectedItem = get(handles.lstListbox, 'value');
numberOfItemsSelected = length(selectedItem);
% If more than one is selected, bail out.
if numberOfItemsSelected > 1
return;
end
% If only one item is selected, get it's name.
allItemNames = get(handles.lstListbox, 'string');
selectedItemName = allItemNames{selectedItem};
great answer but there is a little problem in the line selectedItemName = allItemNames{selectedItem} tha ti don't understand.Can you correct it please?
Sorry - I think there's some discrepancy if there is just one item in there in that it mgiht return a string instead of a cell array. Try this instead - I think it might work in both cases:
whos allItemNames
selectedItemName = char(allItemNames(selectedItem));
If not, tell me what shows up in the command window after the whos command runs.
This is the result below, it is approximitely the same with the previous code.It the returns an "L" which is the first letter of the selected item "line2" which i selected. So i need to return a [1,5] array to have all the word "line2" returned. I searched about this but i didn't find how to return the first 5 letters of the word.
Final try. Try this:
selectedItemName = strcat(cell2mat(allItemNames(selectedItem)));
If that doesn't work, upload your fig file and m-file.

Sign in to comment.

Categories

Tags

Asked:

on 1 Feb 2015

Commented:

on 1 Feb 2015

Community Treasure Hunt

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

Start Hunting!