How to select multiple edit fields?

2 ビュー (過去 30 日間)
xiaojuezi
xiaojuezi 2022 年 1 月 25 日
回答済み: T.Nikhil kumar 2023 年 10 月 30 日
Hi,
In my user interface, users can specify a dimension, for exam m x n, then I draw a m x n table, where each element is an edit field such that users can change values in any edit field.
% Horizontal margin of the canavas
hMargin = 0.02;
% Vertical margin of the canavas
vMargin = 0.02;
% Element width
elemWidth = min((1-2*hMargin) / n, (1-2*vMargin)/m);
% Create an edit field for each entry
for j = 1 : m
for i = 1 : n
pos = [hMargin+(j-1)*elemWidth 1-vMargin-i*elemWidth elemWidth elemWidth];
e = uicontrol('Style','edit',...
'Units','normalized',...
'Position',pos);
end
end
But sometimes, users might want to have a row with same values:
for example in the image, users will have to change the values on row 5 one by one from 0.5 to 1.
Is there a way to have something like a brush, that for the places with the same values, after modifying one edit field, the others can be simply "brushed" to have the same values? Or is there a way that users can jump between each edit field with the arrow keys (currently they always have to use a mouse to select)?
Thank you very much!
  2 件のコメント
Benjamin Thompson
Benjamin Thompson 2022 年 1 月 25 日
Can you post some sample code?
xiaojuezi
xiaojuezi 2022 年 1 月 26 日
Hi! I have posted the sample code for creating the edit field entries.

サインインしてコメントする。

回答 (1 件)

T.Nikhil kumar
T.Nikhil kumar 2023 年 10 月 30 日
Hello,
I understand that you want to know if after modifying one edit field, the other fields in the same row with same value can be made to have the same edited value or if users can jump between each edit field with the arrow keys.
I can suggest you a way to achieve the latter task. You can allow users to navigate through the cells using the arrow keys by leveraging the KeyPressFcn property of the edit fields to capture keyboard input(arrows) and navigate accordingly. Refer to the below mentioned procedure:
  • Add a KeyPressFcn’ argument and pass a callback function’s handle as its value, for each edit field. This callback will listen for arrow key presses.
  • In the definition of the callback function, you can detect when an arrow key is pressed and programmatically determine the next cell to focus on and set focus to that cell using the uicontrol function.
Refer to the below mentioned code snippet for better understanding:
% Horizontal margin of the canavas
hMargin = 0.02;
% Vertical margin of the canavas
vMargin = 0.02;
%Define rows and columns number
m=5;
n=5;
% Element width
elemWidth = min((1-2*hMargin) / n, (1-2*vMargin)/m);
% Create an edit field for each entry
for j = 1 : m
for i = 1 : n
pos = [hMargin+(j-1)*elemWidth 1-vMargin-i*elemWidth elemWidth elemWidth];
e = uicontrol('Style','edit',...
'Units','normalized',...
'Position',pos,'KeyPressFcn', @(src, event)navigateEditFields(src, event, m, n, i, j));
end
end
Refer to the definition of an example callback function ‘navigateEditFields’:
function navigateEditFields(src, event, rows, columns, i, j)
% Get the key that was pressed
key = event.Key;
% Check the arrow key direction
switch key
case 'uparrow'
% Move to the edit field above
if j > 1
j = j - 1;
uicontrol(src.Parent.Children(i*j+(rows-j)*(i-1)));
end
case 'downarrow'
% Move to the edit field below
if j < rows
j = j + 1;
uicontrol(src.Parent.Children(i*j+(rows-j)*(i-1)));
end
case 'leftarrow'
% Move to the edit field on the left
if i > 1
i = i - 1;
uicontrol(src.Parent.Children(i*j+(rows-j)*(i-1)));
end
case 'rightarrow'
% Move to the edit field on the right
if i < columns
i = i + 1;
uicontrol(src.Parent.Children(i*j+(rows-j)*(i-1)));
end
end
end
You can learn more about setting focus and defining callbacks for ‘uicontrol’ object here:
Hope this helps!

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by