[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Communicatie with serialport on main (windows)form, from usercontrol

Hello,

I have a windows form, where different usercontrole forms can be loaded within a panel.
These (sub) usercontrolform have button that should talk with the serialport control on the main form.
For example a button should turn on a relay that is connect to my serial port. The communication works fine if i use everything on one form, but now i want to do this from the sub usercontrol form.

I tried to do this via addhandler and properties, but i can't get it to work.

Any help would be great.

Thanks in advance.

Edward
0
nijmedw
Asked:
nijmedw
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
There are two basic ways to look at it.

(1) UserControls act directly upon the SerialPort
(2) UserControls request that the main form do something on their behalf

For #1 to work, you'd have to pass a reference to the SerialPort (or main form) into the UserControl when you create it.  This obviously can only be done at run-time.  The UserControl would keep that reference as a class level variable so it can use it at will.

For #2, you'd add Custom events to your UserControl that the main form subscribes to.  When the events are received, the main form would act upon the SerialPort for the UserControl.  If there is only one UserControl needing to do this, then you can wire up the custom events by declaring it as WithEvents and using the Handles keyword.  If the UserControls are dynamic in nature, then you'd use AddHandler() to wire up those custom events instead.
0
 
nijmedwAuthor Commented:
Can you give a small example for option #2?
On the main form i've the serial port control. I just need to write and read some data from/to it. I don't need to act on external changes, like an external switch.

Thanks a lot.

Edward
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure...here's a UserControl that raises a custom event when the button is pressed:
Public Class UserControl1

    Public Event ActivateRelay()

    Private Sub btnTurnOnRelay_Click(sender As System.Object, e As System.EventArgs) Handles btnTurnOnRelay.Click
        RaiseEvent ActivateRelay()
    End Sub

End Class

Open in new window


Then, in the main Form, we subscribe to it's events when the UserControl is created:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim UC1 As New UserControl1
        AddHandler UC1.ActivateRelay, AddressOf UC1_ActivateRelay
        Panel1.Controls.Add(UC1)
    End Sub

    Private Sub UC1_ActivateRelay()
        MessageBox.Show("Request Received to Activate Relay!")
        ' ... activate the relay via your SerialPort ...
    End Sub

End Class

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now