title: Click A MessageBox button programmatically
body: <p>As the title suggests, I'm trying to simulate a button-click in a MessageBox programmatically. I earlier tried to close the MessageBox by finding its handle via its caption, and applying <code>WM_CLOSE</code> or <code>SC_CLOSE</code> in <code>SendMessage()</code>. However, due to the presence of Yes/No buttons, that did not work (the X button is grayed out). </p>
  
  <p>Now I'm trying to click the No button as follows -:</p>
  
  <pre><code>List&lt;IntPtr&gt; result = new List&lt;IntPtr&gt;();
  GCHandle listHandle = GCHandle.Alloc(result);
  try
  {
       IntPtr Window_hWnd = CloseMessageBox.FindWindowByCaption("#32770", "LastQuestion"); //Could use null as the first argument too. "#32770" represents classname Dialog.
       CloseMessageBox.EnumChildWindows(Window_hWnd, (hWnd, lParam) =&gt;
       {
            StringBuilder sb = new StringBuilder();
            foreach (var control in GCHandle.FromIntPtr(lParam).Target as List&lt;IntPtr&gt;)
            {
                  CloseMessageBox.GetWindowText(control, sb, 250);
                  if (sb.Equals("&amp;No"))
                  {
                      CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseDown, 0, 0);
                      CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseUp, 0, 0);
                  }
            }
            return false;
       }, GCHandle.ToIntPtr(listHandle));
  
   }
   catch (Exception e)
   {
       MessageBox.Show(e.ToString());
   }
   finally
   {
       if (listHandle.IsAllocated)
          listHandle.Free();
   }
  </code></pre>
  
  <p>Having come this far on the advice of someone from IRC, I find that a few edits earlier, I was getting the button handle (only the "&amp;Yes" button) but not all of them. He then suggested this approach, but the <code>control</code> List is not populated and hence it never goes inside the <code>foreach</code>. What do I do to remedy this?</p>