title: Creating global event handlers using WithEvents for a GUI application in VB.NET
body: <p>To get familiar with VB, I'm working on a frontend for a cli application by taking input from a user and feeding it as command line parameters to the said backend application. Here's a link to the <a href="https://github.com/vaibhav-y/bsc-gui/tree/changes-manga" rel="nofollow">repository</a> for reference of any kind.
  Also, I'm fairly new to VB/GUI programming, so please excuse the obvious things that I must have missed out on.</p>
  
  <p><strong>A little background on my thought process:</strong></p>
  
  <p>Here is how I thought I would handle events in the application to build my args string, excuse me if I'm being too verbose, but I thought it would be best to describe what I'm doing as best as I can before I tell what the exact problem is</p>
  
  <ol>
  <li>GUI has various components</li>
  <li>Each component has public events that are fired relevantly</li>
  <li>A class (ArgBuilder) listens to these events in the background and updates related variables</li>
  <li>The handler for the 'OK' button, instantiates this class to get the command and creates a new background process using an already available executable.</li>
  </ol>
  
  <p><strong>The question:</strong></p>
  
  <p>My problem lies within (3), whenever I try to say, handle the InFileSelected event from CtlInputFile class (or any Class.PublicEventFromThatClass ) I get an error <code>Handles clause requires WithEvents variable defined in the containing type or one of its base types</code>. </p>
  
  <p>So far, the <a href="http://msdn.microsoft.com/en-us/library/stf7ebaz%28v=vs.90%29.aspx" rel="nofollow">MSDN articles</a> have helped, but the issues in this one's a bit harder to grasp.</p>
  
  <p>What have I missed in say this class as an example:</p>
  
  <pre><code>Public Class CtlInputFile
      Public Event InFileSelected(ByVal inFileName As String)
      Dim inFileName As String
  
      ' Proxy Init Module
      Private Sub ctlInputFile_Initialize()
          inFileName = String.Empty
      End Sub
  
      ' Event handlers below
  End Class
  </code></pre>
  
  <p>And the global event listener that should be able to capture <code>CtlInputFile.InFileSelected(inFileName)</code></p>
  
  <pre><code>Public Class ArgBuilder
      ' Create getters and setters for all relevant vars in the project
      ' Capture events from all over the place
      Dim inFileName As String
  
      Private Sub ctlInputFile_FileSelected(ByVal inFilename As String) Handles _
      ctlInput.InFileSelected
          ' Do something useful
      End Sub
  
  End Class
  </code></pre>
  
  <p>Again, thanks!</p>