更新状态栏文本 [英] Update status bar text
问题描述
这是最简单的事情,但我无法更新状态栏上的文本...我刚开始使用 c# 但找不到解决方案..在所有答案中,接受的答案是 statusBar1.Text = "text";
我制作了简单的菜单并在菜单中添加了 LOAD 项目.图片已加载,一切正常,只是状态文本没有更新...顺便说一句,MessageBox 还会在状态栏中显示我需要的正确文本.这是我的代码,但它不起作用:
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();
}
这样你的 try
块看起来像这样:
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);
编辑
StatusStrip
控件是一个容器控件.向其中添加一个 ToolStripStatusLabel
项,并更改该控件的文本而不是 statusBar1
的文本:
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();
}
这篇关于更新状态栏文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!