Click here to Skip to main content
13,086,662 members (61,318 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have created a ModbusPollFormFull() form with DataGridView in it. The DataGridView will continuously update the data every second. I put a "NEW FORM" menu item/button in order to create the exact form. I want whenever i created those new forms,they have the ability to update the data in DGV as well but having its own control.
My problem is, those new forms did not showed me any data at all during the runtime. How can i solves this problem?

Thank you

Private Sub NewMenuItem_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        Start += 1
            For i As Integer = 0 To Start
            NewForm = New ModbusPollFormFull()
            NewForm.Text = "ModbusPoll:" & i

i already have a different class for reading the hardware, which is "Modbus" class to help me populate the data.
I use the MainForm() to call the Modbus()class functions (using PollFunction() method and timer elapsed as well) and i create another form,ModBusPollFormFull()to read the data through DataGridView1 ("Address","ID","Value" columns).

I have two forms (MainForm and ModbusPollFormFull()).
ModbusPollFormFull() consists of DataGridView1, and and two label (poll count label and NoConnection label).
There is a three button on MainForm. ("New Form" )button to create multiple instance of ModbusPollFormFull().
("Start") button to start populate DataGridView1 data in ModbusPollFormFull() and ("Stop") button to stop populate DataGridView1 data in ModbusPollFormFull().

The DataGridView1 is populate during runtime(timer-elapsed) and obviously the values in DataGridView1 are changing every second.
Poll Count label shows the counter when the ("Start") button is clicked. NoConnection label will be visible when the ("Stop") button is clicked.

When i clicked ("New Form") button, i want the new ModbusPollFormFull() will be able to start the Poll Count from zero
and the DataGridView1 in the new ModbusPollFormFull() will be able to populate data as in the existing ModbusPollFormFull().

Private Sub NewForm_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewForm.Click
Dim nf As Form
For i As Integer = 0 To Start
nf = New ModbusPollFormFull()
nf.Text = "ModbusPollForm:" & i
nf.Name = "ModbusPollFormFull" & i
For j As Integer = 20 To Inc 'Positioning the new form
nf.Left = j
'i dont know what im should do starts from here. 

Private Sub PollFunction()
'Update GUI:(delegates function)
pollCount += 1
ModbusPollformFull.mpfDoGUIStatus("Poll count:" & pollCount.ToString())
'Create array to accept read values:
Dim values(CInt(QuantityCB.Text)) As Short
Dim pollStart As UShort
Dim pollLength As UShort
If AddressCB.Text <> "" Then
pollStart = Convert.ToUInt16(AddressCB.Text)
pollStart = 0
End If
If QuantityCB.Text <> "" Then
pollLength = Convert.ToUInt16(QuantityCB.Text)
pollLength = 20
End If
'Read Registers and display data in desired format:
Modbus.ReadHoldingRegister_Fc3((Convert.ToByte(SlaveIDCB.Text)), pollStart, pollLength, values)
Catch ex As Exception
DoGUIStatus("Error in modbus read : " & ex.Message)
End Try
Dim itemString As String
Dim itemString2 As String
Dim itemString3 As String
Select Case dataType
Case "Hexadecimal"
For i As Integer = 0 To pollLength - 1
itemString = Convert.ToString(pollStart + i)
itemString2 = Convert.ToString(pollStart + i + 1)
itemString3 = values(i).ToString("X")
DoGUIUpdate(itemString, itemString2, itemString3) 'Delegates functions
Next i
Case "Decimal"
For i As Integer = 0 To pollLength - 1
itemString = Convert.ToString(pollStart + i)
itemString2 = Convert.ToString(pollStart + i + 1)
itemString3 = values(i).ToString("")
DoGUIUpdate(itemString, itemString2, itemString3)
Next i
End Select
End Sub

Can you help me?
Posted 15-Nov-12 4:09am
Updated 20-Nov-12 5:26am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Well, there is nothing showing that you pass any data to the form, except Text.

You can add any kind of write-only or read-write properties to your form class, to pass any data. You can also add another constructor to the form class, with parameters used to pass data in place of the code fragment you show.

juitanis22 16-Nov-12 2:38am
hi SA,
Do i have to add the dgv functions from ModbusPollFormFull()again to the NewForm()?for example let say if ModbusPollFormFull() has DataGridView1. So its that means the NewForm() has the dgv with the same name DataGridView1 as well?
Sergey Alexandrovich Kryukov 16-Nov-12 11:41am
I don't understand what do you mean by "again", I only address the passing data. Basically, as your method is the instance method (non-static), it also has the hidden parameter accessible as "this", so, you can access any members of the declaring class. Go from there.

However, from your comment I feel that you might need to review of the design. You really want to isolate UI from other aspects as much as possible. No forms should "know" anything about the Modbus, hardware at all. You need to introduce some abstraction layer, so for the form everything would look as abstract data. It could be as simple as just one class. And the hardware control part should know nothing about UI at all. It will save you from a lot of trouble in future -- the UI tend to change, the hardware does not change often, and some algorithms last forever. You need to make it all in the form of highly independent building blocks.

And, do you use threading for hardware control/acquisition? It you don't, you already move in wrong direction. But if you do, you also need to develop UI based in invocation of methods to UI thread (Dispatcher.Invoke, Dispatcher.BeginInvoke, Control.Invoke, Control.BeginInvoke). Do you know all that stuff? If not, you urgently need it.

Sergey Alexandrovich Kryukov 19-Nov-12 11:37am
I'll see, but -- could you move it all in the body of the question where it belongs? Use "Improve question", remove this comment and add a short one, so I could be notified.
In the body of question, you can format the code properly, but no in comment.
Thank you,
juitanis22 20-Nov-12 2:30am
i already made the changes
Sergey Alexandrovich Kryukov 20-Nov-12 11:29am
I added proper formatting to the code; is it possible you proper format it, in terms of indents? It probably was lost because you did not put it in "pre" tags...
The code has many problems at first glance...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100