I need to program a form application for a university project. The application has a 2d array of panels that make up a grid that user can interact with. I have some experience with Java, so what I am trying to ask is if there is anyway to translate this line into Visual Basic:
pnl[x][y].addMouseListener(new MouseListener(){
/**do stuff开发者_开发技巧
**/};
Make sure to use control arrays, so you will only need one Click event shared among all form elements. http://www.vb6.us/tutorials/vb6-control-array-tutorial
That depends on which of the VB.NET UI libraries you are using, i.e., WinForms or WPF (what's the VB6 tag doing in your question, BTW?).
For example, to capture mouse moves in WinForms, you could do something like this:
AddHandler pnl(x)(y).MouseMove, AddressOf MyMouseMoveMethod
This attaches a handler function (see below) to the event that you want to handle.
Private Sub MyMouseMoveMethod(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
... ''# The Mouse has been moved over the panel... do something
End Sub
The MouseListener
thing in Java is an implementation of the Observer pattern. In .net, the same problems are solved through events and event handlers instead. To find out which events are available and what signature is required for the event handler, check the MSDN documentation page of the Panel control you are using.
精彩评论