重置加载栏
本文关键字:加载 | 更新日期: 2023-09-27 18:21:06
我目前正在做一个Web浏览器的辅助项目,我似乎陷入了困境。
我在网络浏览器的左下角有一个加载栏,它功能齐全,只是它没有重置绿色加载栏(它在导航时用绿色填充栏,但之后不会重置)。
万一我遇到一些奇怪的情况,它会因为某种原因而出现错误,我最近遇到了一些奇怪的错误,我正试图将它们排除在外。
我正在寻找我可以尝试的建议和窍门。
这是源代码:
namespace WeWolf_Browser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//This Function Will Terminate The Software From The MenuStrip (File > Exit)
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
//This Function Will Make a Message Box Pop Up On The Users Screen With Information About The Software.
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Simple Browser Created By Me");
}
//This Will Navigate the browser to the users input.
private void button1_Click(object sender, EventArgs e)
{
NavigateToPage();
}
//This is the "Core "Function" " That Will Navigate The URL With The Enter KeyChar
private void NavigateToPage()
{
toolStripStatusLabel1.Text = "Navigation Loading";
webBrowser1.Navigate(textBox1.Text);
}
//This Action will allow people use use the KeyPress Enter to navigate to the desired URL.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)ConsoleKey.Enter)
{
NavigateToPage();
}
}
//This WIll Make The Progress Bar Load While The Browser Is Loading.
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (e.CurrentProgress > 0 && e.MaximumProgress > 0)
{
toolStripProgressBar1.ProgressBar.Value = (int)(e.CurrentProgress * 100 / e.MaximumProgress);
}
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
toolStripStatusLabel1.Text = "Navigation Has Finished";
}
}
}
将toolStripProgressBar1.ProgressBar.Value = 0;
添加到webBrowser1_DocumentCompleted
从我读到的评论中,如果发生错误,您希望将其重置为0
。您需要使用try ... catch
语句来处理错误。
在每个事件处理程序上,放置一个try ... catch
块并重置进度条:
try
{
// your actual code
}
catch (Exception ex)
{
// show it to the user
MessageBox.Show(ex.Message);
// reset progress bar
toolStripProgressBar1.ProgressBar.Value = 0;
}