c# - 将面板置于前面或更改可见性后,Application.Run 出现 ArgumentException

标签 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”结尾的按钮和面板除外)都位于我试图隐藏的面板中。

最佳答案

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

recaptchaBox.Image.Dispose();

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

pictureBox1.Image = null;

或者如果您确实打算处理该图像,您可以执行以下操作

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

这应该可以解决你的问题:)

关于c# - 将面板置于前面或更改可见性后,Application.Run 出现 ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557299/

相关文章:

c# - Entity Framework 和 MVC 应用程序 - 如何

.net - VS2017 15.3 解决方案文件中的新 GlobalSection 是什么?

c# - 如何在具有单行ListView 的移动屏幕上快速显示附加数据?

c# - 从 winforms 应用程序到 Azure SQL 的安全连接字符串

c# - 闪烁的 ToolStripButton

c# - 下载 PDF 后重定向页面

c# - 在 C# 中避免 namespace 和类型名称冲突的准则是什么?

c# - 将组合框数据从列表绑定(bind)到属性

c# - 为什么我会收到此异常 : "he ' Microsoft. ACE.OLEDB.12。 0' provider is not registered on the local machine"?

c# - 在c#中画线