2017-06-15 7 views
1

私のGUIには2つの編集ボックス(uicontrol)があり、左クリックして背景色を変更したいと思います。左マウスクリックの場合、ButtonDownFcnはuicontrol Enableプロパティが 'inactive'または 'off'に設定されている場合にのみ機能します。uicontrolのButtonDownFcn

タブキーを押すと、自分の背景色を白に再初期化し、次の編集ボックスの背景色を変更したいと思います。問題は、タブキーを押すと、Uicontrol Enableプロパティが 'off'または 'inactive'なのでフォーカスが変わらないことです。 回避策はありますか?

ここまでは私のコードです。 (EDIT1とEDIT2は同じコードを持つ)

function edit1_ButtonDownFcn(hObject, eventdata, handles) 
set(hObject, 'Enable', 'on', 'BackgroundColor', [0.5,1,0.7]) % change enable and background color properties 
uicontrol(hObject) % focus on the current object 

function edit1_Callback(hObject, eventdata, handles) 
set(hObject, 'Enable', 'inactive', 'BackgroundColor', [1 1 1]) % reinitialize the edit box 

答えて

1

あなたはmoueフォーカスが達成されたときに適切なアクションを設定するのuicontrolの文書化されていない機能を使用することができます。

これは、基になるJavaオブジェクトを見つけて、適切なコールバックを設定することによって行われます。

Javaオブジェクトは"findjobj" function which you can download from the Mathworks FEX

function test 
    %% Create the figure and uicontols 
    hFig = figure; 
    uic(1) = uicontrol ('style', 'edit', 'position', [100 300 200 50], 'parent', hFig); 
    uic(2) = uicontrol ('style', 'edit', 'position', [100 200 200 50], 'parent', hFig); 
    % for each of the uicontrols find the java object and set the FocusGainedCallback 
    for ii=1:2 
    jObj = findjobj (uic(ii)); 
    set(jObj,'FocusGainedCallback', @(a,b)gainFocus(hFig, uic, ii)); 
    end 
    % set the defaults. 
    gainFocus(hFig, uic, 1); 
end 
function gainFocus(hFig, uic, uicIndex) 
    switch uicIndex 
    case 1 
     index = [1 2]; 
    case 2 
     index = [2 1]; 
    end 
    uic(index(1)).BackgroundColor = [1 1. 1]; 
    uic(index(2)).BackgroundColor = [0.5 1. 0.7]; 
end 
+0

を使用して発見され、それは完璧に動作、ありがとうございます。ちょうど1つの詳細:gainFocus関数(hFig)の最初の引数は使用されないため、削除することができます。 – oro777

関連する問題