无模式表单等待按钮单击 [英] Modeless form wait on button click

查看:66
本文介绍了无模式表单等待按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SolidWorks API进行编程,但这无关紧要.我正在使用无模式窗口形式(就我而言,我不能使用模式形式).当表单打开时,我如何达到此目的,代码等待单击此表单上的按钮.因此,当我单击无模式表单上的按钮并且不希望这样做时,我想编写代码.

假设我有以下代码:

I''m programming in SolidWorks API, but this doesn''t matter. I''m using modeless windows form (and for my case I can''t use modal form). How can I reach that when the form opens, the code wait to click on button on this form. Therefore, I want to code go forward when I click button on modeless form and don''t before.

Let say that I have this code:

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;



   namespace Example.csproj
   {
       public partial class SolidWorksMacro
       {
           public void Main()
           {
               Debug.Print("This is main ");

               Form1 frm = new Form1();
               frm.Show();

               //wait on click to Butoon1 on Form1

               Debug.Print("We are on the end of programe ");
           }

           /// <summary>
           ///  The SldWorks swApp variable is pre-assigned for you.
           /// </summary>
           public SldWorks swApp;
       }
   }





以及此代码的形式:





and this code for form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Example.csproj
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // With this butoon I want to continue program but don't before and I can't use modal form
        }
    }
}




现在,由于代码连续且完成并因此关闭了表单,因此表单一打开就被关闭.我要等待代码,直到单击按钮.但是我真的需要无模式形式.

我知道这是一个非常基本的问题,但我找不到有用的答案.我认为我可以通过事件解决此问题,但我不知道如何解决.我骑着很多事情.您能为我提供上述代码的具体解决方法吗?




Now the form is closed as soon as it''s opened because the code continoue and it is finished and becose of this the form is closed. I want to the code wait until I will click button. But I realy need modeless form.

I know that this is very basic question but I don''t find useful answer. I think that I can resolve this problem with events but I don''t know how. I rode a lot about events. Can you give me the concrete resolve for this above code.

推荐答案

以子形式出现一个事件:
Rais an event in the child form:
public partial class Form1 : Form
{
    public event EventHandler OkayToProceedChanged;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // With this butoon I want to continue program but don't before and I can't use modal form
        EventHandler eh = OkayToProceedChanged;
        if(eh != null)
        {
            eh(this, new EventArgs());
        }
    }
}


并在主程序中订阅该事件:


And subscribe to that event in the main program:

private bool _okayToProceed = false;

public void Main()
{
    Debug.Print("This is main ");
 
    Form1 frm = new Form1();
    frm.OkayToProceedChanged += new EventHandler(Form1_OkayToProceedChanged);
    frm.Show();

    while(!_okayToProceed)
    {
        System.Threading.Thread.Sleep(10);
    }

    //wait on click to Butoon1 on Form1
    Debug.Print("We are on the end of programe ");

    /// <summary>
    ///  The SldWorks swApp variable is pre-assigned for you.
    /// </summary>
    public SldWorks swApp;
}

private void Form1_OkayToProceedChanged(object sender, EventArgs e)
{
    _okayToProceed = true;
}


虽然不是很漂亮,但您可能想看一下使用main和您的表单都可以访问的标志的方向.

While it ain''t pretty, you might want to look in the direction of using a flag that is accessible by both main and your form.

public class AppGlobals
{
  public static bool IsFormFinished = false;
}


private void button1_Click(object sender, EventArgs e)
{
  AppGlobals.IsFormFinished = true; 
}


public void Main()
{
  Debug.Print("This is main ");
 
  Form1 frm = new Form1();
  frm.Show();
 
  //wait on click to Butoon1 on Form1
  while(!AppGlobals.IsFormFinished)
  {
    System.Threading.Thread.Sleep(10);
  }

  Debug.Print("We are on the end of programe ");
}


如果在对话框显示时禁用主窗体是可以接受的,则可以这样做,即设置Enabled = false.重要的是,不要阻塞主线程,这可以防止使用Thread.Sleep,如您所知.

这个想法的一个非常简单的演示:
One way to do this if it is acceptable for the main form to be disabled while the dialog is showing is to do just that, i.e. set Enabled = false. It''s important that the main thread is not blocked, which precludes the use of Thread.Sleep, as you have found out.

A very simple demo of the idea:
namespace FauxModalDialogTest {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    protected override void OnShown(EventArgs e) {
      ShowPopup();
      base.OnShown(e);
    }

    private void ShowPopup() {
      Form f = new Form();
      f.Text = "Popup";
      f.FormClosed += new FormClosedEventHandler(f_FormClosed);
      f.Show(this);
      this.Enabled = false;
    }

    private void f_FormClosed(object sender, FormClosedEventArgs e) {
      this.Enabled = true;
    }
  }
}


艾伦.


这篇关于无模式表单等待按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆