How can I make a textbox selectable?
7 views (last 30 days)
Show older comments
I'm trying to trigger an event when a textbox is selected. If I manually set the "Selected" property to "on", the box is highlighted. But I can't figure out how to do this via user interaction.
Minimal example of the type of interface I'm trying to create:
f = figure;
a = axes(f);
foo = annotation('textbox','String','Foo','BackgroundColor','white');
bar = annotation('textbox','String','Bar','BackgroundColor','white');
somePlot = plot([1, 2],[1, 1]);
xlim([0 3])
ylim([0 2])
xscaled = (somePlot.XData - a.XLim(1))/(a.XLim(2) - a.XLim(1));
yscaled = (somePlot.YData - a.YLim(1))/(a.YLim(2) - a.YLim(1));
foo.Position(1:2) = [xscaled(1)*a.Position(3) + a.Position(1) - foo.Position(3)/2,...
yscaled(1)*a.Position(4) + a.Position(2) - foo.Position(4)/2];
bar.Position(1:2) = [xscaled(2)*a.Position(3) + a.Position(1) - bar.Position(3)/2,...
yscaled(2)*a.Position(4) + a.Position(2) - bar.Position(4)/2];
foo.Selected = 'on';
Ideally I'd like to be able to change the selected textbox with a mouse click. I've tried setting the PickableParts property to 'all' but that doesn't change anything.
0 Comments
Accepted Answer
Voss
on 13 Jul 2023
One way to set the Selected property via mouse click is to define a ButtonDownFcn for each textbox.
For example this will allow multiple textboxes to be selected at any time (i.e., click to select, click to de-select, independently):
set([foo bar],'ButtonDownFcn',@cb_select_textbox);
function cb_select_textbox(src,evt)
if evt.Button ~= 1 % only allow left-clicks
return
end
if strcmp(src.Selected,'off')
src.Selected = 'on';
else
src.Selected = 'off';
end
end
And this will allow at most one textbox to be selected at any time (i.e., click to select thus deselecting all others, click to de-select):
set([foo bar],'ButtonDownFcn',{@cb_select_textbox,[foo bar]});
function cb_select_textbox(src,evt,textboxes)
if evt.Button ~= 1 % only allow left-clicks
return
end
was_off = strcmp(src.Selected,'off');
set(textboxes,'Selected','off');
if was_off
src.Selected = 'on';
end
end
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!