How to enable and disable edit text box from a check box?

2 views (last 30 days)
I'm trying to enable and disable editable text box from a checkbox. The ideia is: when checkbox is checked the editable text box must be enable and when not checked it must be disable. My code works well when the editable text box is empty, however, if I type any value in the textbox, when I press the checkbox I get an error.
Error while evaluating uicontrol Callback
Error using handle.handle/set
Invalid or deleted object.
Error in OC_SST>check_min_val_cut_Callback (line 1625)
set(handles.min_val_cut,'enable','on');
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in OC_SST (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)OC_SST('check_min_val_cut_Callback',hObject,eventdata,guidata(hObject))
My code is:
%check box function
function check_min_val_cut_Callback(hObject, eventdata, handles)
tmp = get(hObject,'Value');
if tmp
handles.check_min_val_cut = tmp;
set(handles.min_val_cut,'enable','on'); %editable textbox turns on
else
handles.check_min_val_cut = 0;
set(handles.min_val_cut,'enable','off'); %editable textbox turns off
end
guidata(hObject,handles);
%edit text box function
function min_val_cut_Callback(hObject, eventdata, handles)
handles.min_val_cut = str2double(get(hObject,'String'));
guidata(hObject,handles);
Could anyone help me?
Thank you in advance.
Fabio

Answers (2)

Image Analyst
Image Analyst on 17 Apr 2014
Why are you doing this:
handles.check_min_val_cut = 0;
???? You're blowing away the handle ID of your checkbox by setting the ID number equal to it's check value? Why would you do that? Let's say that handles.check_min_val_cut was 78.1435, and now you're going to set it to true or false, essentially breaking the connection between your handle structure and your checkbox. What was your intent with those lines? What did you want to do or think you were going to do when you put them in there?
  3 Comments
Image Analyst
Image Analyst on 17 Apr 2014
"So, I define handles.check_min_val_cut = tmp" <- and that's your mistake. Don't do that.
Your simplified code looks like it should work, as long as the edit box's tag is "min_val_cut".
Fabio
Fabio on 17 Apr 2014
No! There is no problem with the checkBOX! The checkbox works ok. The problem is when I try to enable a EDIT TEXT BOX from that checkBOX. Look at the above code. The function associated to the checkBOX is called check_min_val_cut_ Callback(hObject, eventdata, handles) and the function associated to the EDIT TEXT BOX is called min_val_cut_ Callback(hObject, eventdata, handles). So, again, if the checkbox is checked the EDIT TEXT BOX must become editable, otherwise the EDIT TEXT BOX must become not editable.

Sign in to comment.


houda
houda on 4 Jul 2016
Hi, I am facing the same problem, did anyone find the solution ? Thanks !
  1 Comment
Image Analyst
Image Analyst on 4 Jul 2016
He said he had a callback function for the edit field called min_val_cut_ Callback(). This means that the tag for the edit field was min_val_cut. He then had a line
handles.min_val_cut = str2double(get(hObject,'String'));
This blew away the handle for the edit field, making it no longer able to be accessed. To fix: Do not have a variable with the same name as the tag of a uicontrol, min_val_cut in his case.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!