Ho to make a popup box enable/disable visibility of textbox?

10 visualizaciones (últimos 30 días)
Hi guys!
I'm building a GUI interface and i want to disable and enable the visibility of a textbox (entry user information box), from popup list. How cand i do that?
My popup has 4 differents options, the first 2 options i want enable and for 3 an 4 option disable.
One more thing, i already saw some structures on web, but it doesn't seem to work form, but i suspect that i'm using it in the wrong place of my script, where should i write this commands?
Cheers!

Respuesta aceptada

Nick
Nick el 17 de Nov. de 2018
You need to add a callback that checks for the value of your dropdown menu and edits the enable value of your edit textbox. As we do not know the structure of your code here is a small example how you would approach it:
function exampleGui
handles = struct;
hFig = figure;
handles.dropdown= uicontrol('Parent', hFig,...
'Style','popupmenu',...
'String',{'Can edit 1', 'Can edit 2', 'Cannot edit 1', 'Cannot edit 2'},...
'Units','normalized',...
'Callback', @onEditDropdown,...
'Position',[0.1 0.4 0.2 0.2]);
handles.textbox = uicontrol('Parent', hFig,...
'Style','edit',...
'Units','normalized',...
'Position',[0.4 0.4 0.5 0.2]);
function onEditDropdown(src,event)
if src.Value>2
handles.textbox.Enable = 'off';
else
handles.textbox.Enable = 'on';
end
end
end
  3 comentarios
Nick
Nick el 20 de Nov. de 2018
Its a bit hard for me to see it in your code as it requires a .fig file to run and I am not very familiar with GUIDE as I always coded UI's using classes. But the basic idea is the same: you need a callback function that sets the visible property of the textbox.
You mention that it works when you press the "Calcular" button but not immediatly after you click the popup menu. From your code i am assuming the popup menu is "escolha_lista_de_curto". All the code that checks the value of escolha curto is in the "pushbutton1_callback" which means it will only execute when that specific pushbutton is pressed.
You also have a "escolha_curto_Callback" i am assuming this is the callback linked to your popup menu (not 100% sure but you could check in GUIDE). That is where you would want to set something similar to my code example
So it would be something like this:
function escolha_curto_Callback(hObject, eventdata, handles)
% hObject handle to escolha_curto (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
escolha_lista_de_curto=get(hObject,'value');
% you could do switch or if else for each value but for now
% I just assume item 1-2 = enabled / item 3-4 = disabled
if escolha_lista_de_curto>2
set(handles.textboxHandle, 'visible','off'); % no longer visible
% or if you still want it to be visible but grayed out and uneditable
set(handles.textboxHandle, 'enable','off'); % gray textbox + uneditable
set(handles.textboxHandle, 'string',''); % empty string is textbox is disabled to remove previous values
else
set(handles.textboxHandle, 'visible','on'); % visible
% or alternative:
set(handles.textboxHandle, 'enable','on'); % white textbox + editable
end
end
Hope that helps. Obviously the "textboxHandle" should be whatever your textbox is called but I could not find that in your code as I could not understand the meaning of each variable name. Enable grays out a textbox and makes it uneditable while visible will make it appear or disappear in your GUI, you can choose whatever fits your application best.
Jucimar Carpe
Jucimar Carpe el 20 de Nov. de 2018
Hi Nick, thank you very much, it worked exactly as you described!!!
I even set the edit box to be visible off as well and worked.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by