代码之家  ›  专栏  ›  技术社区  ›  Aleksa Ristic

仅使messagebox可聚焦[复制]

  •  0
  • Aleksa Ristic  · 技术社区  · 6 年前

    button1 放在 form1
    无论是否找到新版本,适当的 MessageBox 表格1 ).

    如何制作 显示 表格1

    0 回复  |  直到 14 年前
        1
  •  45
  •   Otiel user577803    12 年前
    this.Invoke(new Action(() => { MessageBox.Show(this, "text"); }));
    

    这将切换到主线程并显示带有 form1

        2
  •  12
  •   Derek W    10 年前

    而所选答案提供了一种显示 MessageBox DialogResult 从那个特别的地方 对话框 正在放映。

    如果您希望返回 对话框结果 对话框 Form . 那么你需要使用 Func 委托而不是 Action

    行动 函数 具有返回值。

    private DialogResult BackgroundThreadMessageBox(IWin32Window owner, string text)
    {
       if (this.InvokeRequired)
       {
          return (DialogResult) this.Invoke(new Func<DialogResult>(
                                 () => { return MessageBox.Show(owner, text); }));
       }
       else
       {
          return MessageBox.Show(owner, text);
       }
    }
    

        3
  •  5
  •   Arsen Mkrtchyan    14 年前
      if ( Form1.InvokeRequired ) {
                Form1.Invoke((Action)delegate{MessageBox.Show(Form1,"Hello");});
            }
    
        4
  •  0
  •   Carra    14 年前

    尝试使用 backgroundworker .

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
       //Helper thread: Long during task
    }
    
    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //We're in the main thread: Show your messagebox
    }
    
        5
  •  0
  •   Ricardo França    8 年前

    在我的例子中,我在另一个类中,有一个文本框的引用,所以我使用了下面的代码:

    _txtResultado.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate ()
    {
        MessageBox.Show("My message!");
    }));