【问题标题】:Only assignment, call, increment, decrement, and new object expressions can be used as a statement : Messagebox只有赋值、调用、递增、递减和新对象表达式可以用作语句:消息框
【发布时间】:2013-11-11 03:24:39
【问题描述】:

这就是我想要达到的目标

  if( this.BeginInvoke((Action)(
        () => MessageBox.Show("Test", 
                              MessageBoxButtons.YesNo) == DialogResult.No)))

上面给出了错误。我尝试将委托与外部方法分开,例如

delegate void test(string text);
private void SetTest(string text)
{
 if(MessageBox.Show(text,"", MessageBoxButtons.YesNo) == DialogResult.No)

}

但这违反了我需要委托的原因。我发现第一个作品适合我,但我不知道如何将它放在 if/else 语句中。请以更好的方式提供任何帮助,我将不胜感激。

  if(this.BeginInvoke((Action)(
      () => MessageBox.Show("Test", 
                            MessageBoxButtons.YesNo) == DialogResult.No)))

【问题讨论】:

    标签: c# messagebox


    【解决方案1】:

    另一种变体......这很丑:

    if ((bool)this.Invoke((Func<bool>)delegate
      {
      return MessageBox.Show("Test Message", 
                             "Test Title", 
                             MessageBoxButtons.YesNo) == DialogResult.No;
      }))
    {
        Console.WriteLine("No was indeed selected.");
    }
    

    这应该适用于任何版本的 C#:

    private void button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread T = new System.Threading.Thread(Foo);
        T.Start();
    }
    
    delegate bool SetTestDelegate(string text);
    private bool SetTest(string text)
    {
        return (MessageBox.Show(text, "", MessageBoxButtons.YesNo) == DialogResult.No);
    }
    
    private void Foo()
    {
        if ((bool)this.Invoke(new SetTestDelegate(SetTest), new object[] {"test"}))
        {
            Console.WriteLine("No was indeed selected.");
        }
    }
    

    编辑:您可以将该代码重构为更有用的东西...

    private void button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread T = new System.Threading.Thread(Foo);
        T.Start();
    }
    
    private void Foo()
    {
        if (Response("test") == System.Windows.Forms.DialogResult.No)
        {
            Console.WriteLine("No was indeed selected.");
        }
    }
    
    delegate DialogResult ResponseDelegate(string text);
    private DialogResult Response(string text)
    {
        if (this.InvokeRequired)
        {
            return (DialogResult)this.Invoke(new ResponseDelegate(Response), new object[] { "test" });
        }
        else
        {
            return MessageBox.Show(text, "", MessageBoxButtons.YesNo);
        }
    }
    

    【讨论】:

    • 我更喜欢其他的答案。我试图尽可能地复制你原来的线路正在做的事情。在我看来看起来不太好......
    • 我想你了……随便给自己点分数吧:)
    • 我给出了异常“由于'System.Action'返回void,return关键字后面不能跟对象表达式”。顺便说一句,你能定义 Func 吗?我想我不明白 Func 可能是。
    • V2.0 但是,当我检查框架时,3 和 3.5 似乎可用
    • 我想可能我可以使用 System.Action。我试过它给出了同样的异常“由于'System.Action'返回void,return关键字后面不能跟对象表达式”
    【解决方案2】:

    您似乎需要Invoke() 方法,而不是BeginInvoke():

    var messageBoxResult = (DialogResult)Invoke(new Func<DialogResult>(
        () => MessageBox.Show("Test", "Test", MessageBoxButtons.YesNo)));
    if(messageBoxResult == DialogResult.No)
    {
    }
    

    BeginInvoke 异步执行委托,这意味着调用BeginInvoke 的线程上不会立即获得结果。

    【讨论】:

    • 我完全按照你说的操作做了,它给出了参数计数错误异常。
    • Action(和Action&lt;T&gt;)委托代表一个不返回任何内容的方法。 Func&lt;T&gt; 表示返回 T 类型值的方法:delegate T Func&lt;T&gt;();。您不能在此处使用Action 代替Func。
    【解决方案3】:

    为什么不让您(和您的程序员同事)的生活更轻松,并执行以下操作:

    var user_said_no = this.BeginInvoke((Action)(
       () => MessageBox.Show("Test", MessageBoxButtons.YesNo) == DialogResult.No));
    

    然后有:

    if (user_said_no)
       cancel_nuclear_attack() ; // Or whichever things you need to do.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多