gpt4 book ai didi

c# - 更新状态栏文字

转载 作者:行者123 更新时间:2023-11-30 13:58:05 27 4
gpt4 key购买 nike

这是最简单的事情,但我无法更新状态栏上的文本...我刚开始使用 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

最佳答案

也许文本已设置但没有绘制,因为您的线程正忙于加载图片?您可以尝试强制状态栏失效并自行重绘:

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 block 将如下所示:

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

关于c# - 更新状态栏文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18469495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com