Click here to Skip to main content
15,894,176 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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:

C#
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
    } 
}
Posted
Updated 17-Sep-14 16:00pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 18:05pm    
Unfortunately, the question, as formulated, does not seem to make any sense. The term "focus" is related exclusively to certain UI. At a time, there is on control in the whole desktop, which holds keyboard focus and responds to major input from the keyboard (not counting hot keys, keyboard accelerators, hooks, etc.). The focus cannot "go back to method", because this notion is not applicable to methods. Even though there could be some real problem behind your post (or not real :-), you did not explain it. The code sample also does not help much to see the problem, because nearly nothing tells us how different point in this code are related to your textual description. RemoteMonitor returns bool (why?), what what?..
—SA
Admire Mhlaba 18-Sep-14 2:21am    
Thank you for your kind response, well RemoteScanEvent invokes ComPortSetup Method that returns a boolean value to denote a success or failure and that is the value I'm trying to return back such that some other events can be triggered based on that.
Sergey Alexandrovich Kryukov 18-Sep-14 2:44am    
First, modal ShowDialog won't return before you close it. There is nothing you can do it. If it does not work for you, change the UI design. If this method called from some other, not UI thread? (In not, you don't need this.Invoke. And why not Invoking the whole call to this method?)
—SA
Admire Mhlaba 18-Sep-14 2:50am    
Thank you for your kind input,i will consider your suggestion. Just a quick note, if I use this.Show() it does return the focus back but the form becomes unresponsive or hangs? Any idea how to allay this.
Sergey Alexandrovich Kryukov 18-Sep-14 2:55am    
You really should learn using threads with UI. This is the key. Well, could you show the code with Show()? Could you explain your goals in sufficient detail.
—SA

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:
C#
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
     }
 }
 
Share this answer
 
v5
Comments
BillWoodruff 18-Sep-14 3:34am    
In that case: remove your 'RemoteMonitor function from the 'ItemMonitor Form class, and put it in the body of the MessageProcessor class. Then: only create an instance of the 'ItemMonitor Form when the call to 'RemoteMonitor returns 'true.
BillWoodruff 18-Sep-14 4:18am    
See the edited code in my answer. If you are only creating this Class once, and creating the Form once, I wouldn't worry about the need to dispose of the Form.

Once again: why do you want the 'ItemMonitor Form shown modally ?
BillWoodruff 18-Sep-14 4:47am    
Until you explain why you want to use 'ShowModal, we will probably keep going in circles :)

An alternative would be to re-locate the SMS sending code in the instance of 'ItemMonitor ?
BillWoodruff 18-Sep-14 4:57am    
I've revised the code one last time so the 'ItemMonitor form is shown, and is set to:

1. not show in the TaskManager

2. always be in front of all other Forms.
BillWoodruff 18-Sep-14 5:12am    
My goal here is to help you learn how to help yourself. And, I can't "force" you to answer my questions. Until you spend the time and effort necessary to fully describe what you are doing, I can't go further than I've already gone.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900