Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

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: Re: 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

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
text box Vihang Patil 25 Nov, 2007 02:05:17
keypressfcn Vihang Patil 25 Nov, 2007 02:05:17
buttondownfcn Vihang Patil 25 Nov, 2007 02:05:17
timer Vihang Patil 25 Nov, 2007 02:05:17
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics