Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


as the event. This example names the AccessFormMarshal routine presented
earlier in this chapter:

 AddHandler ComPorts.UserInterfaceData, AddressOf AccessFormMarshal


 ComPorts.UserInterfaceData +=
new ComPorts.UserInterfaceDataEventHandler(AccessFormMarshal);
When a routine raises the UserInterfaceData event, the AccessFormMarshal
routine executes on the form. The AccessFormMarshal routine marshals the
textToAdd string to the form for displaying.
In a similar way, other routines can use events to pass data or notify other mod-
ules of events. In an application that uses a separate dialog box with combo
boxes for setting port parameters, clicking the form’s OK button can raise an
event that passes the selected port parameters to the main form.
In the Form that contains the combo boxes, declare the event:


 Friend Shared Event UserInterfacePortSettings(ByVal selectedPort As String, _
ByVal selectedBitRate As Integer, ByVal selectedHandshake As Handshake)


 internal delegate void UserInterfacePortSettingsEventHandler
(string selectedPort, int selectedBitRate, Handshake selectedHandshake);


internal static event UserInterfacePortSettingsEventHandler UserInterfacePortSettings;
In the Click event for the form’s OK button, pass parameters from the combo
boxes. In the example below, the combo boxes are cmbPort, which contains
port names, cmbBitRate, which contains bit rates, and cmbHandshaking,
which contains handshaking options:

 RaiseEvent UserInterfacePortSettings(cmbPort.SelectedItem.ToString, _
CInt(cmbBitRate.SelectedItem), -
DirectCast(cmbHandshaking.SelectedItem, Handshake))


 if ( null != UserInterfacePortSettings )
{
UserInterfacePortSettings( cmbPort.SelectedItem.ToString(),
System.Convert.ToInt32( cmbBitRate.SelectedItem ),
( ( Handshake )( cmbHandshaking.SelectedItem ) ) );
}

Free download pdf