[英]Edit box clearing on mouse click in MATLAB GUI
我想在 MATLAB GUI 中有一個“編輯”框,上面寫着“在此處鍵入搜索”。 當用戶在框內單擊時,我希望“TYPE SEARCH HERE”消失並為用戶提供一個空的編輯框以開始輸入...
有任何想法嗎?
至少在我的系統上,當我使用以下代碼設置用戶輸入框/窗口時
prompt = 'Enter search terms:';
dlg_title = 'My input box';
num_lines = 1;
defAns = {'TYPE_SERACH_HERE'};
answer = inputdlg(prompt, dlg_title, num_lines, defAns);
默認文本TYPE_SEARCH_HERE
突出顯示,因此我可以開始輸入以將其替換為我想要的內容。
編輯或者,如果你有一個現有的uicontrol
編輯框,你可以做類似如下:
function hedit = drawbox()
hedit = uicontrol('Style', 'edit',...
'String', 'deafult',...
'Enable', 'inactive',...
'Callback', @print_string,...
'ButtonDownFcn', @clear);
end
function clear(hObj, event) %#ok<INUSD>
set(hObj, 'String', '', 'Enable', 'on');
uicontrol(hObj); % This activates the edit box and
% places the cursor in the box,
% ready for user input.
end
function print_string(hObj, event) %#ok<INUSD>
get(hObj, 'String')
end
克里斯,您必須單擊 uicontrol 邊框才能使 ButtonDownFcn 發生。 如果您在編輯框內單擊,則不會發生
好的,所以我有一個解決問題的方法,它完美無缺!!
但是,我很沮喪,因為我完全不知道它為什么有效......
使用以下代碼:
函數 myEditBoxTagGoesHere_ButtonDownFcn(hObject, eventdata, handles)
% 將“啟用”狀態切換為 ON
set(hObject, 'Enable', 'On');
% 創建 UI 控件
uicontrol(handles.myEditBoxTagGoesHere);
如果有人能解釋為什么 uicontrol 在鼠標左鍵單擊時突出顯示文本,那就太好了!
Hooplator15,它起作用是因為當啟用為關閉時,編輯文本就像按鈕:
如果Enable == 'on'(編輯文本Enable),函數_ButtonDownFcn在5像素邊框內鼠標按下時執行;
否則,它會在 5 像素邊框或編輯文本上按鼠標執行,就像按鈕一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.