Print Multiple Lines into TextArea Automatically without mentioning the Array (MATLAB App Designer)

32 次查看(过去 30 天)
Hello,
I am trying to create like a message box into app designer using the Text Area component. For example if the user enter the Name and LastName it displays 'Name and LastName Entered successfully'. Else the Text Area displays 'Information Not Provided'. So now when the user selects Gender from drop down box it should displays 'Gender as Male selected'. Text is displayed into the Text Area when a Button is clicked. The button has a functionality to check whether the textField is empty or not empty. And to check whether the textField is empty its done with a help of function checkNames(app). To check the selected Gender from the dropDown Menu I am using a Switch case and the switch case is evaluated using the function checkGender(app). Is there a way so that i dont have to define .Value{1}, .Value{2} .... Thank You
Code
methods (Access = private)
function checkNames(app)
if ~(isempty(app.EditField_Name.Value) || isempty(app.EditField_LastName.Value))
app.MessageBoxTextArea.Value{1} = 'Name and LastName Entered successfully';
else
app.MessageBoxTextArea.Value{1} = 'Information Not Provided';
uiwait(app.checkNames);
end
end
function checkGender(app)
switch app.DropDown_Gender.Value
case 'Male'
app.MessageBoxTextArea.Value{2} = 'Gender as Male Selected';
case 'Female'
app.MessageBoxTextArea.Value{2} = 'Gender as Female Selected';
otherwise
app.MessageBoxTextArea.Value{2} = 'Gender as Others Selected';
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
checkNames(app);
checkGender(app);
end
end
  4 个评论
Rahul
Rahul 2023-1-3
Thank you @Geoff Hayes. It worked for me! Can we also add the functionality of changing the text colour? for example if its 'Data Entered Sucessfully' it should display in the text area with Green as the font colour. And when it is an error it is displayed with a font colour of Red?
Geoff Hayes
Geoff Hayes 2023-1-6
@Rahul - I don't think you can do that for individual lines. There may be some undocumented means of doing that sort of thing (wrapping text in html tags?) but I'm not sure how to do that.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2022-12-21
编辑:Geoff Hayes 2022-12-21
@Rahul - you could add a helper function/method that will add the text for you at the correct index. For example, that function may look like
function addMessageToMessageBox(app, msg)
x = length(app.MessageBoxTextArea.Value);
app.MessageBoxTextArea.Value{x+1} = msg;
end
and then your code would become
function checkNames(app)
if ~(isempty(app.EditField_Name.Value) || isempty(app.EditField_LastName.Value))
addMessageToMessageBox(app, Name and LastName Entered successfully');
else
addMessageToMessageBox(app, 'Information Not Provided');
uiwait(app.checkNames);
end
end
function checkGender(app)
switch app.DropDown_Gender.Value
case 'Male'
addMessageToMessageBox(app, 'Gender as Male Selected');
case 'Female'
addMessageToMessageBox(app, 'Gender as Female Selected');
otherwise
addMessageToMessageBox(app, 'Gender as Others Selected');
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by