重置加载栏

时间:2015-11-09 08:27:13

标签: c#

我目前正在开发一个侧面项目,这是一个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";
        }
    }
}

2 个答案:

答案 0 :(得分:3)

toolStripProgressBar1.ProgressBar.Value = 0;添加到webBrowser1_DocumentCompleted

答案 1 :(得分:0)

根据您的评论,我发现如果发生错误,您希望将其重置为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;
}