asa
asa

Reputation: 57

How to update the status bar?

My program keeps track of the number of characters and lines in the text box. I am trying to create a status bar that displays this information to the user. The problem is that when the status bar updates, it does not replace the old information, but simply adds it to the line.

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    int numChars = TextBox.Text.Length;
    int numLines = TextBox.LineCount;
    TextBlock statusText = new TextBlock();

    statusText.Text = "Line " + (numLines) + ", Char " + (numChars);
    Status.Items.Add(statusText);
}

Upvotes: 1

Views: 235

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

You are adding new TextBlock to the StatusBar without clearing its items. Try clearing the items before adding a new one:

Status.Items.Clear();
Status.Items.Add(statusText);

However, a better solution would be to re-use the same TextBlock, instead of adding&clearing it:

// xaml:
<StatusBar>
    <StatusBarItem>
        <TextBlock Name="status" />
    </StatusBarItem>
</StatusBar>

// code behind:
status.Text = "...";

Upvotes: 1

Related Questions