Filter löschen
Filter löschen

How to create a popup mesage initially before program running?

1 Ansicht (letzte 30 Tage)
Hye all.
I have a gui program. My question is how can i make my program started with user insert his name first. For example: when the user double click on my program, then there will be a popup message ask the user his name. After the user insert his name, then only the program will run. Can anyone help me on this?

Akzeptierte Antwort

Paulo Silva
Paulo Silva am 26 Feb. 2011
Here's one way to do it, popupgui is the name of the GUI I used to create the code, this was created with the help of GUIDE, it will ask for the user name and if the user doesn't provide one or cancels the dialog the GUI will give an error and close it self, handles.UserName can be used in any other GUI function to do something with the name the user inserted, the name is just a string.
function popupgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to popupgui (see VARARGIN)
% Choose default command line output for popupgui
handles.output = hObject;
prompt = {'Please write your name'}; %what the user must do
dlg_title = 'User Login'; %the title of the dialog
num_lines = 1; %the number of lines that can be inserte
def = {''}; %this is the default name
UserName = inputdlg(prompt,dlg_title,num_lines,def); %the diaglog
if cellfun('isempty',UserName)
uiwait(errordlg('You must insert your name','Program terminated'))
error('You must insert your name')
end
if isempty(UserName)
uiwait(errordlg('You canceled the dialog','Program terminated'))
error('You canceled the dialog')
end
%convert cell to just a string and save it to the handles struct
handles.UserName=cell2mat(UserName); %so it can be used elsewhere
% Update handles structure
guidata(hObject, handles);
  2 Kommentare
slumberk
slumberk am 26 Feb. 2011
i try this code but when 'the user' cancel the program, it does show the program terminated but there is an error like this:
??? Error using ==> fyp_editor>fyp_editor_OpeningFcn at 71
You canceled the dialog
Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure),
varargin{:});
Error in ==> fyp_editor at 42
gui_mainfcn(gui_State, varargin{:});
Paulo Silva
Paulo Silva am 26 Feb. 2011
I know that error, I couldn't get ride of him, it's no big deal because the program closes itself but if you really want to not have him force the user to write his name.
error=0;
while 1
prompt = {'Please write your name'}; %what the user must do
dlg_title = 'User Login'; %the title of the dialog
num_lines = 1; %the number of lines that can be inserted
def = {''}; %this is the default name
UserName = inputdlg(prompt,dlg_title,num_lines,def); %the diaglog
if cellfun('isempty',UserName)
uiwait(errordlg('You must insert your name','You must insert your name'))
error('You must insert your name')
error=1;
end
if isempty(UserName)
uiwait(errordlg('You canceled the dialog','Dialog canceled'))
error=1;
end
if ~error,break,end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 26 Feb. 2011
The "input" command will open a simple dialog box that can be used to prompt for the user's name.

Kategorien

Mehr zu Software Development Tools finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by