将面板置于前面或更改可见性后,Application.Run上的ArgumentException

时间:2013-07-09 20:11:30

标签: c# .net winforms panel argumentexception

由于某种原因,ArgumentException表示“参数无效”。在执行以下操作时,Application.Run会被抛出:

private void utilsTabBtn_Click(object sender, EventArgs e)
{
    utilitiesPanel.BringToFront();
}

或者这个:

private void utilsTabBtn_Click(object sender, EventArgs e)
{
    utilitiesPanel.Visible = true;
    accountcreatorPanel.Visible = false;
    aboutPanel.Visible = false;
}

在此代码中执行RequestCaptcha之后:

private void accountcreatorStartBtn_Click(object sender, EventArgs e)
{
    accountcreatorStopBtn.Enabled = true;
    accountcreatorStartBtn.Enabled = false;
    recaptchaSolveBox.Enabled = true;
    recaptchaContinueBtn.Enabled = true;
    recaptchaRenewBtn.Enabled = true;
    MinimalID = accIndexOne.Value;
    CurrentID = MinimalID - 1;
    MaximalID = accIndexTwo.Value;
    RequestCaptcha();
    recaptchaBox.SizeMode = PictureBoxSizeMode.StretchImage;
}

RequestCaptcha是:

private void RequestCaptcha()
{
    LatestKey = ((new Random()).Next() * (new Random()).Next());
    statusLbl.Text = "Captcha key: " + LatestKey.ToString();
    recaptchaBox.Image.Dispose();
    using (WebClient w = new WebClient())
    {
        w.DownloadFile(string.Format(RequestURL, LatestKey), Environment.CurrentDirectory + "/latestCaptcha.jpg");
    }
    try
    {
        recaptchaBox.Image = Image.FromFile(Environment.CurrentDirectory + "/latestCaptcha.jpg");
    }
    catch (Exception ex)
    {
        recaptchaBox.Image = recaptchaBox.ErrorImage;
        MessageBox.Show(ex.Message, this.Text + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

所有控件,除了以“TabBtn”结尾的按钮和面板外,都在我试图隐藏的面板中。

1 个答案:

答案 0 :(得分:2)

在RequestCaptcha()方法中,您实际上正在处理图片框仍在使用的图像。

recaptchaBox.Image.Dispose();

以上行无效。或者你可以设置

pictureBox1.Image = null;

或者如果您打算真正处理图像,可以执行以下操作

Image image = recaptchaBox.Image;
recaptchaBox.Image = null;
image.Dispose();

应解决您的问题:)