【问题标题】:Messagebox function show error消息框功能显示错误
【发布时间】:2014-01-02 19:46:10
【问题描述】:

在我的 Windows 应用程序中,我希望当用户尝试从 datagridview 中删除一行时,应该有一个打开的消息框,要求用户确认删除该行。

为此我写了这段代码:

DialogResult res = MessageBox.Show("Are You Sure", MessageBoxButtons.OKCancel);

并检查用户用户响应,但此行显示错误。

这段代码有什么问题?

请帮帮我。

当我只写这段代码时

MessageBox.Show("Are You Sure");

然后它工作正常,但我想再次确认用户,所以我想要他的回应。

【问题讨论】:

  • 您遇到错误了吗?如果是这样,您需要告诉我们它是什么

标签: c# winforms window


【解决方案1】:

你快到了!如果您想指定MessageBoxButtons,您需要以这种方式添加标题以及标题(或消息文本):

MessageBox.Show(string, string, MessageBoxButtons);   

已完成,您的代码应如下所示:

DialogResult res = MessageBox.Show("Are you sure?", "Title", MessageBoxButtons.OKCancel);

您收到错误消息,因为您的调试器期望标题为 string,而改为接收 MessageBoxButtons。阅读this文章了解详细解释。

【讨论】:

    【解决方案2】:

    根据MessageBox APIShow(String, MessageBoxButtons)没有方法重载

    我想你想要

    Show(String, String, MessageBoxButtons)
    

    【讨论】:

      【解决方案3】:

      改用 MessageBoxResult:

      MessageBoxResult res= MessageBox.Show("Are You Sure",MessageBoxButtons.OKCancel);
      

      【讨论】:

      • 没有像MessageboxResult这样的对象..我找不到它
      • 我猜是 .net 4.5,其他帖子显示正确的方法签名。使用它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2019-10-14
      相关资源
      最近更新 更多