GUI display value from child form to parent form

5 views (last 30 days)
Hi I have a parent window abc which has a pushbutton1 and a edittext, when I click on this button a new form/window appears with another pushbutton named hi. Now what I want to do it when I press button Hi the message hi should be set to the the edittext in parent window. How can I do it. Please help
  6 Comments
Geoff Hayes
Geoff Hayes on 22 Aug 2017
Are you using GUIDE? If so, please see the link I provided in my previous comment.
Adam
Adam on 22 Aug 2017
Edited: Adam on 22 Aug 2017
This is the approach I use. It is not trivial, as it uses Object-oriented programming, but it isn't complicated either conceptually.
It is based on the idea that GUIs should never be communicating with each other and taking it upon themselves to give each other instructions.
There should be an underlying program/workflow/set of objects that are communicating and deciding what needs to be done and firing off events/messages. Individual GUIs can then listen for the messages relevant to them and react accordingly. This allows the GUI to be separated from the workflow rather than an integral part of it, so that you could, if wished, just as easily run the workflow without the GUI, just by supplying the paremeters on the command line.
But that is more in the lines of an idealistic approach. If you just want something to work quickly then having GUIs hack into one another and tell each other what to do works...until it doesn't :)

Sign in to comment.

Answers (1)

Jan
Jan on 22 Aug 2017
Edited: Jan on 22 Aug 2017
The general strategy is clear: The 2nd GUI must find the first one, to address the wanted uicontrol. You can either provide the handle of the figure of the 1st GUI during the creation of the 2nd GUI (shown below), or use tags for the recognition.
function CreateGui1
handles.FigH = figure('Tag', 'myGUI1');
handles.edit5 = uicontrol('Style', 'Text', 'String', '');
guidata(handles.FigH, handles);
CreateGui2; % Start the creation from here
end
function CreateGui2
handles.FigH = figure('Tag', 'myGUI2');
handles.button1 = uicontrol('Style', 'PushButton 'String', 'hi', ...
'Callback', @myButtonCallback);
guidata(handles.FigH, handles);
end
function myButtonCallback(ButtonH, EventData)
handles = guidata(ButtonH); % Handles of GUI2!
% Find GUI1:
gui1H = findobj(allchild(groot), 'flat', 'tag', 'myGUI1');
if numel(gui1H) ~= 1 % Either none or multiple
warning('Cannot identify the main GUI uniquely!');
return;
end
% Set the string:
gui1Handles = guidata(gui1H);
set(gui1Handles.edit5, 'String', 'hi');
end
This can be applied to GUIs created by GUIDE also. The dynamic search for the GUI1 fails, if several figures with the same tag are found. Therefore providing the figure handle as input for the creation of the 2nd GUI is smarter:
function CreateGui1
handles.FigH = figure('Tag', 'myGUI1');
handles.edit5 = uicontrol('Style', 'Text', 'String', '');
guidata(handles.FigH, handles);
CreateGui2(handles.FigH); % <== Provide the figure handle
end
function CreateGui2(Gui1H)
handles.Gui1H = Gui1Handle; % <== Store the handle
handles.FigH = figure('Tag', 'myGUI2');
handles.button1 = uicontrol('Style', 'PushButton 'String', 'hi', ...
'Callback', @myButtonCallback);
guidata(handles.FigH, handles);
end
function myButtonCallback(ButtonH, EventData)
handles = guidata(ButtonH); % Handles of GUI2!
handlesGui1 = guidata(handles.Gui1H); % Handles of GUI1!
set(handlesGui1.edit5, 'String', 'hi');
end

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!