Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to switch between two text?

How I can switch the text of my button between "Show Comment" and "Hide Comment" whenever I click the button?

private void GenerateCommentContent()
{
     var button = new Button();
     button.Text = question.Comment == null ? $"+ {Translator.Buttons_AddComment}" : $"{Translator.Buttons_ShowComment}";
     button.WidthRequest = 100;
     button.Clicked += (sender, e) => OnButtonClicked(commentlabel, frame, button);

        .......
}

    private void OnButtonClicked(Label label, Frame frame, Button button)
    {
        label.IsVisible = !label.IsVisible;
        frame.IsVisible = !frame.IsVisible;
        button.Text = $"{Translator.Buttons_HideComment}";
    }

So basically the first GenerateCommentContent will render a dynamic button, label and editor (comment textbox). If the question.Comment is null, the first text of the button is "Add a Comment", but if there is a data, the button should switch to "Hide Comment" and "Show Comment" only every time the button is click.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Create a simple bool value to store the current state:

private bool commentShown = true;

private void OnButtonClicked(Label label, Frame frame, Button button)
    {
        label.IsVisible = !label.IsVisible;
        frame.IsVisible = !frame.IsVisible;
        commentShown = !commentShown;
        button.Text = commentShown ? $"{Translator.Buttons_HideComment}" : $"{Translator.Buttons_ShowComment}";
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading