Enable or Disable Edit Text with a Checkbox

Hi, So i want to Disable and Enable an Edit Text with a Checkbox. So if the Checkbox is checked enable the Edit Text and if its unchecked disable it. But I want it to still show the value in the Edit Text. Can anyone help me with this?
Example Code:
function checkboxMaxResults_Callback(hObject, eventdata, handles) )
checkboxStatus = get(handles.checkboxMaxResults,'Value');
if checkboxStatus == 1
%Allow input for editMaxResults
set(handles.editMaxResults, 'enable', 'on')
else
%Block input for editMaxresults
set(handles.editMaxResults, 'enable', 'off')
end
function editMaxresults_Callback(hObject, eventdata, handles)
%
function editMaxresults_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end

7 commentaires

László Arany
László Arany le 19 Mar 2013
I don't see what is your problem with the given code. It should only change whether the checkbox is enabled or not, if you disable it, the value still remains there. The following code should change the 'BackgroundColor' of the edit text object to a darker grey and disable it (i.e. you can't change the value), but the value should remain in the edit text.
So what exactly is the problem?
Thomas
Thomas le 19 Mar 2013
Well, When I run the GUI nothing happends and it gives an error:
Error while evaluating uicontrol Callback
Attempt to reference field of non-structure array.
Error in testCheckbox>checkboxMaxResults_Callback (line 83) checkboxStatus = get(handles.checkboxMaxResults,'Value');
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in testCheckbox (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)testCheckbox('checkboxMaxResults_Callback',hObject,eventdata,guidata(hObject))
Thomas
Thomas le 19 Mar 2013
I also want it to be a numeric only field on the Text Edit. Any idea on how to do this?
László Arany
László Arany le 19 Mar 2013
Modifié(e) : László Arany le 19 Mar 2013
The only (possible) error I see with that code is that there's two closing brackets at the end of the first line after the input arguments of the function, otherwise it seems to be okay. Check if the 'Tag' of the checkbox in the GUI is the same as the name in the callback in the .m file. Also, check if 'checkboxMaxResults' is indeed a 'checkbox' object.
(By the way, I'm not an expert, but I used this very same code many times, so I think it should work.)
Thomas
Thomas le 19 Mar 2013
It worked, the problem was that I opened the GUI wrong, i clicked the .fig file on the left of matlab in the Current Folder file. When i type in the GUI's name in the command window it works fine. Now for the numeric only value. Any idea how this works with my code?
László Arany
László Arany le 19 Mar 2013
Modifié(e) : László Arany le 19 Mar 2013
Well, if your 'edit text' is named say 'MyEditText', then you can go to its callback and use the following:
function MyEditText_Callback(hObject,eventdata,handles)
input = str2num(get(handles.MyEditText,'String'));
if (isempty(input))
set(handles.MyEditText,'String','0')
end
This will turn any input that is not a number into zero, but only after the user "leaves" the edit text. That is, you can type numbers into the edit text, but once you finished typing, it changes it to 0 if it contains text.
This works because if 'str2num' encounters a string that contains letters, then it sets 'input' empty. (you can also use hObject instead of handles.MyEditText)

Connectez-vous pour commenter.

Réponses (1)

Hello, I have a similar Problem, regarding to the 'Disable Edit Textbox'.
I want to enable it, when the Checkbox is checked, and disable it, when the Checkbox is unchecked. The Enable-Process works fine, but I can't disable it.
function White_Noise_Callback(hObject, eventdata, handles)
WN = get(handles.White_Noise,'Enable');
if WN == 'on'
set(handles.WN_per,'enable','on');
else WN == 'off'
set(handles.WN_per,'enable','off');
end
Also, there is no Error given.

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by