Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
active change of textbox color?

Subject: active change of textbox color?

From: Vihang Patil

Date: 25 Nov, 2007 07:03:26

Message: 1 of 2

Hello I am trying to achieve the following;
I have made a figure window with 5 uicontrols of textbox type
and assigned its background color property to "Red".
Now I want the background color property of these textboxes
to change depending upon the numeric value I enter.
For example,
If I press numeric 1 on my keyboard the First TextBox's
background Color should change to "Green". If I press
numeric 2 on my keyboard the second text box should turn
"Green". and so on and so forth.
I am able to achieve the basic requirement using the
following code I have attached.
But the problem is
1. I want the text box to change color only till the period
 I have pressed the respective numeric button. If I release
the button it should turn "Red" again
2. If I press two or three buttons simultaneously they
should change color accordingly.

My code currently does not change the color back to its
original red color once I have stopped pressing the key and
secondly I can only see one color change if I press two or
more keys.
Please guide

Regards
Vihang

Attached Code......................

function tempgui
clear;close all, clc;
handles.h = figure;
handles.h1 = uicontrol('Style', 'text', 'String', '1',...
    'Position', [20 150 50 50],'BackgroundColor','red');
handles.h2 = uicontrol('Style', 'text', 'String', '2',...
    'Position', [100 150 50 50],'BackgroundColor','red');
handles.h3 = uicontrol('Style', 'text', 'String', '3',...
    'Position', [180 150 50 50],'BackgroundColor','red');
handles.h4 = uicontrol('Style', 'text', 'String', '4',...
    'Position', [260 150 50 50],'BackgroundColor','red');
handles.h5 = uicontrol('Style', 'text', 'String', '5',...
    'Position', [340 150 50 50],'BackgroundColor','red');

handles.tmr = timer('TimerFcn',
{@TmrFCN,handles.h},'BusyMode','Queue',...
    'ExecutionMode','FixedRate','Period',0.1);
set(handles.h,'CloseRequestFcn',{@closefig,handles.h},'WindowStyle','Modal');

guidata(handles.h,handles);
start(handles.tmr);

function TmrFCN(src,evnt,handles)
handles = guidata(handles);
val = get(handles.h,'CurrentCharacter');
if val == '1'
    set(handles.h1,'BackgroundColor','green');
else
    set(handles.h1,'BackgroundColor','red');
end
if val == '2'
    set(handles.h2,'BackgroundColor','green');
else
    set(handles.h2,'BackgroundColor','red');
end
if val == '3'
    set(handles.h3,'BackgroundColor','green');
else
    set(handles.h3,'BackgroundColor','red');
end
if val == '4'
    set(handles.h4,'BackgroundColor','green');
else
    set(handles.h4,'BackgroundColor','red');
end
if val == '5'
    set(handles.h5,'BackgroundColor','green');
else
    set(handles.h5,'BackgroundColor','red');
end


function closefig(src,evnt,handles)
handles = guidata(handles);
if strcmp
(get(handles.tmr,'Running'),'on');stop(handles.tmr);end
delete(handles.tmr);
closereq;

Subject: active change of textbox color?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 25 Nov, 2007 20:01:28

Message: 2 of 2

In article <fib6nu$560$1@fred.mathworks.com>,
Vihang Patil <vihang_patil@yahoo.com> wrote:
>Hello I am trying to achieve the following;

>1. I want the text box to change color only till the period
> I have pressed the respective numeric button. If I release
>the button it should turn "Red" again
>2. If I press two or three buttons simultaneously they
>should change color accordingly.

I believe you will have to work at the Java level to achieve
these goals. Matlab provides a KeyPressFcn callback but not
a KeyReleaseFcn callback. Handling multiple buttons simultaneously
will essentially require working at the keyboard scan code level.
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us