From: Steven Lord on

"riya sen" <nehakalra_15(a)yahoo.com> wrote in message
news:hq24pu$f6v$1(a)fred.mathworks.com...
> Just to make things easier, Ashish, please just post some code about how I
> can correlate the push button "B" to the list box, not the text screen at
> the top, but the listbox. How can, for example, when I click "B", it can
> go to the word "Basic."
>
> That's all I'm asking. Please help me.

Have each pushbutton change the ListboxTop property of the listbox to refer
to the first item in the listbox that starts with that letter.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol_props.html#ListboxTop

How you have each pushbutton determine the appropriate value this property
should have is up to you (hard code it if the strings are not going to
change, use the string manipulation functions to locate the appropriate
index, etc.)

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Ashish Uthama on
On Tue, 13 Apr 2010 13:06:22 -0300, riya sen <nehakalra_15(a)yahoo.com>
wrote:

> Just to make things easier, Ashish, please just post some code about how
> I can correlate the push button "B" to the list box, not the text screen
> at the top, but the listbox. How can, for example, when I click "B", it
> can go to the word "Basic."
>
> That's all I'm asking. Please help me.

Riya/Neha,

The first code snippet I posted was intended to do this (update the
listbox).
You might have been able to adapt from the idea there.

Here's a full example with just A and B (try clicking B then A to have the
editbox show 'BA' and the listbox scroll to Bad).


Please review the code carefully, make sure you have a look at the
corresponding help and understand the flow. It might help you customize
the ideas to build exactly what you are looking for.

(btw: if you manage to polish this, it might make a good FEX submission. )


function exampleGUI
editUI = uicontrol('Style', 'edit','tag','editbox', 'Position', [300, 650,
700, 60]);
A = uicontrol ('Style','PushButton','String','A','Position',[50,550, 130,
80],...
'CallBack', @ButtonPressed);
B= uicontrol ('Style','PushButton','String','B','Position',[200,550,
130,80],...
'CallBack', @ButtonPressed);


function upDateStateWith(newChar)
%Update both the editbox and the listbox based on button pressed.

%Get the current content of the editbox
currentText = get(editUI,'String');
%Append this button's char to the end (I assume this is what you want)
%if you want to replace instead, just set updatedText = newChar instead
%of the line below
updatedText = [currentText, newChar];
%Update the edit box with the new string
set(editUI,'String',updatedText);


%Search for entries in the listbox which match what we have in the
%editbox right now. Please see the help for strncmpi
matchFlags =
strncmpi(updatedText,allWordsInListBox,length(updatedText));
%we want the first match
firstMatchIndex = find(matchFlags,1);
if(~isempty(firstMatchIndex))
%the current text in the editbox matches something in the list box.
%now we update the listbox
set(listbox,'Value',firstMatchIndex);
end
%else - figure out what you want to do here.


end


function ButtonPressed(h, eventdata)
%'h' is the handle to the button which was pressed. We can obtain its
%'string' value directly. No need for multiple callbacks.
buttonChar= get(h,'String');
upDateStateWith(buttonChar);
end




allWordsInListBox =
{'A';'Am';'Are';'Able';'Above';'According';'Account';'Across';...
'Act';'Action';'Activity'; 'Actually';
'Added';'Addition';'After';'Again';
'Against';'Age';'Ago';'Ahead';'Aid';'Air';'All';'Almost';'Alone';...
'Along';'Already';'Also';'Although';'Always';'And';'Another';'Answer';'Anti';'Anyone';'Any';'Anything';'Appear';'Approach';'Area';'Arms';...
'Around';'Art';'Ask';'Asked';'attack';'Attention';'available';'Average';'Away';
'B';'Bad';'Ball';'Based';'Basic';'Basis';'Be';'Beautiful';...
'Became';'Because';'Become';'Bed';'Been';'Before';'Began';'Beginning';'Behind';'Being';'Believe';'Below';
'Best';'Better';'Between';'Beyond';...
'Big';'Bill';'Black';'Blood';'Blue';'Board';'Body';'Book';'Born';'Both';'Bring';'British';'Brought';'Brown';'Building';'Built';'Business';...
'But';'By'};

listbox = uicontrol('style','listbox','units','normalized',...
'Position',[.8, .4, .2 , .46],'String',allWordsInListBox);
% Define listbox Callback property to update edit box
set(listbox, 'Callback', @(src, evt) listCallback(src, editUI));
end


function listCallback(src, editBoxHandle)
% Get current selection number
value = get(src, 'Value');
% Get entire string array.
strArr = get(src,'String');
% Index into string array to retrieve selection
selection = strArr{value};
set(editBoxHandle, 'String', selection);
end
First  |  Prev  | 
Pages: 1 2 3
Prev: Read Image to Matrix in Simulink
Next: Maple or Mupad?