How to enable and disable edit text box from a check box?
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Image Analyst
2014-4-17
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 个评论
Image Analyst
2014-4-17
"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".
houda
2016-7-4
Hi, I am facing the same problem, did anyone find the solution ? Thanks !
1 个评论
Image Analyst
2016-7-4
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!