Solved

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

Posted on 2013-11-10
3
368 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now