this is simplest possible thing, but i cant update text on status bar... I just started working in c# but cannot find solution..
in all answers, accepted answer is statusBar1.Text = "text";
I made simple menu and added LOAD item in menu. Picture is loaded, all works fine, just status text doesn't update...
Btw, MessageBox also displays right text that i need in status bar.
Here is my code, and it just doesn't work:
 private void menuLoad_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Title = "Load Photo";
        dlg.Filter = "jpg files (*.jpg)"
        + "|*.jpg|All files (*.*)|*.*";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            try
            {
                statusBar1.Text = "Loading " + dlg.FileName;
                pbxPhoto.Image = new Bitmap(dlg.OpenFile());
                statusBar1.Text = "Loaded " + dlg.FileName;
                MessageBox.Show("Text = " + dlg.FileName); 
            }
            catch (Exception ex)
            {
                statusBar1.Text = "Unable to load file " + dlg.FileName;
                MessageBox.Show("Unable to load file: " + ex.Message);
            }
        }
        dlg.Dispose();
    }

Maybe the text gets set but just doesn't get painted because your thread is busy loading a picture? You can try to force the status bar to invalidate and repaint itself:
statusBar1.Text = "Loading " + dlg.FileName;
statusBar1.Invalidate();
statusBar1.Refresh();    
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
statusBar1.Text = "Loaded " + dlg.FileName;
statusBar1.Invalidate();
statusBar1.Refresh();    
MessageBox.Show("Text = " + dlg.FileName); 
Actually I think I'd encapsulate this into a method, like this:
private void UpdateStatusBarText(string text)
{
    statusBar1.Text = text;
    statusBar1.Invalidate();
    statusBar1.Refresh();    
}
This way your try block would look like this:
UpdateStatusBarText("Loading " + dlg.FileName);
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
UpdateStatusBarText("Loaded " + dlg.FileName);
MessageBox.Show("Text = " + dlg.FileName); 
EDIT
The StatusStrip control is a container control. Add a ToolStripStatusLabel item to it, and change the text of that control instead of that of statusBar1:
private void UpdateStatusBarText(string text)
{
    toolStripStatusLabel1.Text = text;
    statusBar1.Invalidate();
    statusBar1.Refresh();    
}
                        This answer relates to WPF because this question was originally tagged as
WPF.
As @MattBurland mentioned, UI updates do not happen at the same time as execution. This means that setting different values for a UI property is pointless, because only the last one will actually be updated. Instead, you need to schedule a message on the UI thread using the Dispatcher. Try something like this:
private void UpdateStatus(string message)
{
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
    {
        statusBar1.Text = message;
   }));
}
And then in your method:
try
{
    UpdateStatus("Loading " + dlg.FileName);
    pbxPhoto.Image = new Bitmap(dlg.OpenFile());
    UpdateStatus("Loaded " + dlg.FileName);
    MessageBox.Show("Text = " + dlg.FileName); 
}
catch (Exception ex)
{
    UpdateStatus("Unable to load file " + dlg.FileName);
    MessageBox.Show("Unable to load file: " + ex.Message);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With