Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

707 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

11 Experts available now in Live!

Get 1:1 Help Now