Search code examples
c#xamltextboxtextblock

How do I move a text input by user in a textbox, into a textblock in C#?


menuText is the textblock and commentTextbox is the textbox. I have cleared the textbox when text is inputted into the textbox. How do I make the text input in the textbox to appear in the textblock when the commentButton is clicked?

private void commentButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(commentTextbox.Text))
            {

            }
            else
            {
                commentTextbox.Text = string.Empty;
                menuText.Text += commentTextbox.Text;
            }
        }

Solution

  • You are clearing the text box first, so, since commentTextbox.Text is an empty string, your last line is effectively doing menuText.Text += string.Empty;

    You should invert the order of those calls, like that:

    private void commentButton_Click(object sender, RoutedEventArgs e)
    {
        if (String.IsNullOrWhiteSpace(commentTextbox.Text) == false)
        {
            menuText.Text += commentTextbox.Text;
            commentTextbox.Text = string.Empty;
        }
    }