MATLAB: How to change color of the TEXT EDIT control when I am entering text and change the color again after I am done typing using MATLAB 7.9 (R2009b)

guiMATLAB

In UICONTROL, I want to change the color of the TEXT EDIT when I am typing something into the text box. Once I am done entering the text and click somewhere outside the text box (or hit any key), I want the color of the TEXT EDIT to change again.

Best Answer

The TEXT EDIT color can change during and after typing.
To change the edit text control color while entering text, you can define a ‘KeyPressFcn’ property to change ‘BackgroundColor’. Once done entering text, the color is changed again by hitting the “enter” key or clicking anywhere outside the text box. This can be achieved in the ‘Callback’ function. The following code implements this idea. Save the function and execute it. Enter some text in the text control box and you should see the color changes to red. Click outside the text box or hit “enter”, the color changes to gray.
function trial
uicontrol('Style','edit','KeyPressFcn',@changeColorFcn,'Callback',@resetColorFcn);
function resetColorFcn(hObject, ~)
set(hObject,'BackgroundColor',[.8 .8 .8]);
function changeColorFcn(hObject,~)
set(hObject,'BackgroundColor',[1,.5,.5]);
Related Question