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

33 ビュー (過去 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 Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by