Update status bar text

c#, winforms

Solution

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();    
}

Problem

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(); } ```

Original source