2013-08-27 120 views
3

這是最簡單的可能的事情,但我不能更新狀態欄上的文本...我剛開始在C#中工作,但無法找到解決方案.. 在所有答案中,接受的答案是statusBar1.Text = "text"; 我做了簡單的菜單並在菜單中添加了LOAD項目。圖片加載,所有工作正常,只是狀態文本不更新... 順便說一句,MessageBox還顯示我需要在狀態欄右邊的文本。 這裏是我的代碼,它只是不工作:更新狀態欄文本

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

screenshot1

+3

通過不工作,你的意思是狀態欄不更新呢?或者您沒有看到「加載」消息,但看到「已加載」消息?後者是預料之中的,因爲你拿着UI線程。 –

+0

對不起,如果我錯過了..文本只是仍然「準備好」,並沒有更新。我在狀態欄1的屬性中放置了「Ready」文本 – Mlad3n

+0

@ Mlad3n關閉MessageBox後,您看到了什麼? – I4V

回答

3

也許文本被設置,但只是沒有得到畫,因爲你的線程忙於加載圖片?你可以嘗試強制狀態欄無效,並重新繪製自己:

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

其實我覺得我會封裝成一個方法這一點,類似這樣的:

private void UpdateStatusBarText(string text) 
{ 
    statusBar1.Text = text; 
    statusBar1.Invalidate(); 
    statusBar1.Refresh();  
} 

這樣你try塊看起來像這樣:

UpdateStatusBarText("Loading " + dlg.FileName); 

pbxPhoto.Image = new Bitmap(dlg.OpenFile()); 

UpdateStatusBarText("Loaded " + dlg.FileName); 
MessageBox.Show("Text = " + dlg.FileName); 

編輯

StatusStrip控件是一個容器控件。一個ToolStripStatusLabel項目添加到它,並更改控制而不是的statusBar1的文字:

private void UpdateStatusBarText(string text) 
{ 
    toolStripStatusLabel1.Text = text; 
    statusBar1.Invalidate(); 
    statusBar1.Refresh();  
} 
+0

http://postimg.org/image/vguuz5lvr/ 這裏...我喜歡你說的沒有什麼改變...「準備不更新...」 – Mlad3n

+0

順便說一句,如果我做statusBar1.BackColor =紅色;然後酒吧的背景顏色被改變...所有的工作正常,只是文本更新不想改變:) – Mlad3n

+0

@ Mlad3n請參閱編輯。我想你需要在'ToolStripStatusLabel'上設置'Text'屬性,而不是直接在'statusBar1'上。 –

3

這個答案涉及到WPF,因爲這個問題最初是標記爲WPF

正如@MattBurland所說,UI更新不會與執行同時發生。這意味着爲一個UI屬性設置不同的值是毫無意義的,因爲只有最後一個纔會被更新。相反,您需要使用Dispatcher在UI線程上安排消息。嘗試是這樣的:

private void UpdateStatus(string message) 
{ 
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => 
    { 
     statusBar1.Text = message; 
    })); 
} 

然後在你的方法:

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); 
} 
+0

「正在加載...」狀態可能不會顯示,除非我想加載的圖片很大... 我希望只在狀態欄上看到「Loaded ...」消息,這就是我想要的。 我不能把你的UpdateStatus方法放在我的menuLoad_Click方法中。我得到很多錯誤......不應該只有一行代碼,只是改變狀態欄的文本? – Mlad3n

+0

你怎麼能把任何方法放入另一個方法?你什麼意思?添加'UpdateStatus'方法,然後從你的點擊處理程序調用*方法。你會得到什麼錯誤? – Sheridan

+0

我得到錯誤「名稱'調度員'不存在於當前上下文中...' '使用System.Windows.Threading;'也不被接受... – Mlad3n