Solved

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

Posted on 2013-11-10
3
371 Views
Last Modified: 2013-11-16
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
Comment
Question by:nijmedw
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39637152
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
 

Author Comment

by:nijmedw
ID: 39637399
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39637440
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question