필터 지우기
필터 지우기

How can I make a textbox selectable?

조회 수: 5 (최근 30일)
Josh G.
Josh G. 2023년 7월 13일
답변: Voss 2023년 7월 13일
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.

채택된 답변

Voss
Voss 2023년 7월 13일
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개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by