如何在显示Windows窗体后返回值。 [英] How Do I Return A Value After Displaying A Windows Form.

查看:145
本文介绍了如何在显示Windows窗体后返回值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天Coders,请帮助我解决代码中的逻辑错误。我正在从一个类初始化一个表单,然后在表单内部调用一个方法来显示它,然后将一个布尔值返回给类中的调用方法。我的问题是,一旦显示表单,焦点就不会回到等待返回布尔值的方法。下面是一个示例代码:



Good day Coders, kindly help me resolve a logical error within my code. I'm initializing a form from one class and then call a method inside the form to display it and then return a boolean value, to the calling method in the class. My problem is, once the form is displayed, the focus is not going back to the method awaiting the boolean value to be returned. Below is a sample code:

class MessageProcessor
{
    string responseMsg = string.Empty;
    
    public void isFormDispalyed()
    {
        ItemMonitor itemMonitor = new ItemMonitor (); //Form initialization
        
        IntPtr handle = itemMonitor .Handle;
        
        if (itemMonitor.RemoteMonitor())
        {
            responseMsg =  "Surveillance Initiated";
        }
        else
        {
            responseMsg = "Surveillance Couldnt Be Started";
        }   
    }
}


public partial class ItemMonitor : Form
{
    public bool RemoteMonitor()
    {
        this.Invoke(new ClearOutPutBoxEvent(ClearOutPutBox));
        
        isStartButtonClicked = true;
        
        object isOpen = null;
        
        isOpen = this.Invoke(new RemoteScanEvent(ComPortSetup));
        
        this.Invoke(new Action(() = this.ShowDialog()));//pragram never proceeds until the form is closed.
        
        return Convert.ToBoolean(isOpen);//Must return this value to the calling method
    } 
}

推荐答案

即使我还没有完全理解您的代码选择,我也会在这里发布一个想法;如果这对你不起作用,也许如果你解释为什么它不起作用,我们会更好地理解你想要做什么。



我假设如果对RemoteMonitor的调用返回false,则表示您不希望显示itemMonitor表单实例:
Even though I do not fully understand your code choices yet, I'm going to post an "idea" here; if this would not work for you, perhaps if you explain why it would not work, we'll have a better understanding of what you are trying to do.

I assume that if the call to 'RemoteMonitor returns 'false that you will not want to show the 'itemMonitor Form instance:
public class MessageProcessor
 {
     private string responseMsg = string.Empty;

     private ItemMonitor itemMonitor = new ItemMonitor(); //Form initialization

     public void isFormDispalyed()
     {
         if (itemMonitor.RemoteMonitor())
         {
                responseMsg = "Surveillance Initiated";
                
                // edit #2:
                
                // these settings should be set at design-time for the Form
                // they are shown here for educational purposes only
                itemMonitor.ShowInTaskbar = false;
                itemMonitor.TopMost = true;
                
                // this must be set in code
                itemMonitor.TopLevel = true;
                
                itemMonitor.Show();
                
                // handle sending SMS messages here ?
         }
         else
         {
                // edit #1: close the instance of the 'ItemMonitor: probably unnecessary
                // see: http://stackoverflow.com/questions/3097364/c-sharp-form-close-vs-form-dispose
                // for why 'Close is used here
                itemMonitor.Close();
                
                responseMsg = "Surveillance Couldnt Be Started";
         }
     }
 }

 public partial class ItemMonitor : Form
 {
     public bool RemoteMonitor()
     {
         this.Invoke(new ClearOutPutBoxEvent(ClearOutPutBox));

         isStartButtonClicked = true;

         object isOpen = true;

         isOpen = this.Invoke(new RemoteScanEvent(ComPortSetup));

         return Convert.ToBoolean(isOpen); //Must return this value to the calling method
     }
 }


我已经听取了您的信息性建议,并决定更改表单的访问方式。现在问题已经解决了。谢谢大家的帮助和时间。
I have listened your informative suggestion and decided to change the way the form is accessed. The problem has been laid to rest now. Thank you all for your kind help and time.


这篇关于如何在显示Windows窗体后返回值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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