Path: news.mathworks.com!not-for-mail
From: "helper " <spamless@nospam.com>
Newsgroups: comp.soft-sys.matlab
Subject: Listbox mousedown problems
Date: Mon, 12 May 2008 12:30:31 +0000 (UTC)
Organization: Timothy S. Farajian, Inc.
Lines: 105
Message-ID: <g09d97$ir0$1@fred.mathworks.com>
Reply-To: "helper " <spamless@nospam.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210595431 19296 172.30.248.38 (12 May 2008 12:30:31 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 12 May 2008 12:30:31 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1272923
Xref: news.mathworks.com comp.soft-sys.matlab:467885


Ok, I give up...

I can't even begin to list all the dozens of things I've 
tried to make this work, but this problem is fighting back 
like I killed its pet hamster.

I have a listbox uicontrol and an edittext uicontrol 
working in conjuction to allow me to change the elements of 
the listbox.  The following is code which reproduces it:

%-----------------------------------------%
function ListBoxTest
figure
hListbox = uicontrol('style','listbox',...
  'pos',[20 20 100 100],...
  'String',{'a','b','c'},...
  'backg','w',...
  'Callback',@(x,y)Listbox_Callback);
hEditText = uicontrol('style','edit',...
  'pos',[20 130 100 20],...
  'String','',...
  'backg','w',...
  'Callback',@(x,y)EditText_Callback);
Listbox_Callback

  function Listbox_Callback
    val = get(hListbox,'Value');
    str = get(hListbox,'String');
    set(hEditText,'String',str{val})
  end

  function EditText_Callback
    val = get(hListbox,'Value');
    str = get(hListbox,'String');
    newStr = get(hEditText,'String');
    str{val} = newStr;
    % pause(1)
    set(hListbox,'String',str)
  end
end
%-----------------------------------------%


Now, typing in the editbox changes the elements of the list 
and everything is working fine there.

To see my problem, perform the following steps:

1. Select element 1 in the listbox
2. Click in the editbox and edit the value
3. Click element 3 in the listbox

You will see that the listbox jumps back to element 1, and 
the user must click on the listbox again to change the 
value.

The problem is caused by the fact that the editbox's 
callback changes the "String" property of the listbox, thus 
cancelling any mousedown that has previously occurred on 
the listbox.

Try uncommenting the PAUSE command, and in step 3 above, 
hold the mouse button down on element 3.  You will see the 
SET command altering the listbox selection.

The root of the problem is that the listbox's callback 
doesn't fire until the mouseup, however the editbox's 
callback will fire on the mousedown.

Things I have tried:

1. Using the listbox's "ButtonDownFcn" rather than 
its "Callback", however this doesn't work for left-click 
when "Enable" is "on".

2. Using the FEX's FINDJOBJ function to be able to get a 
callback when a mousedown event occurs on the listbox.  
However, setting the "MousePressedCallback" (and many other 
callback options) doesn't seem to fire when the mouse is 
pressed.  This is probably the solution, but I can't seem 
to get it to work.

3. Using MANY methods for the editbox's callback to 
recognize it has been fired by a mousedown event on the 
listbox so it can avoid setting the string property until 
the listbox's callback has fired.  Note that:
  a) checking the PointerPostion property of the root 
object to see if it is over the listbox doesn't help 
because the callback could have fired from a keypress (tab 
or enter).
  b)  The currentcharacter property (of the figure) will 
tell me that the enter key was pressed, however it will not 
tell me that the tab key was pressed.
  c) checking the CurrentObject doesn't work because a 
mousedown event on the listbox doesnt change the 
CurrentObject.
  d) e) f) g) h) ... z)  I can go on 

 
I have enjoyed battling this killer rabbit, however it has 
beaten me badly and I am crawling outta the cave dragging 
my shield behind me.

Does anyone feel like whacking a rabbit?