?
Solved

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

Posted on 2013-11-10
3
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Simple Linear Regression
Suggested Courses

765 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