AdvancedHMI Software

General Category => Open Discussion => Topic started by: Holmux on September 10, 2019, 07:58:16 AM

Title: Dynamic array of basic buttons
Post by: Holmux on September 10, 2019, 07:58:16 AM
Hi All

I am trying to make a dynamic array using AdvancedHMIControls.basicButton and I am strucling a bit with getting a value from each button to the plc.
Code: [Select]
Dim xPos As Integer = 0
        Dim yPos As Integer = 0
        Dim n As Integer = 0
        Dim p As Integer = -1
        Dim s As String = ""

        For i As Integer = 0 To 10
            ' Initialize one variable
            btnArray(i) = New AdvancedHMIControls.BasicButton
        Next i

        While (n < 10)
            With (btnArray(n))
                s = (n + 1).ToString
                .OutputType = 5
                .PLCAddressClick = BValue
                .ValueToWrite = n + 1
                '   .BackColor = Color.DarkGray
                '   .ForeColor = Color.Lime
                .Width = 100 ' Width of button
                .Height = 100 ' Height of button
                p += 1
                If (p = 5) Then ' Location of second line of buttons:
                    xPos = 0
                    yPos += 100
                    p = 0
                End If
                ' Location of button:
                .Left = xPos
                .Top = yPos
                ' Add buttons to a Panel:
                pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
                xPos = xPos + .Width ' Left of next button
                n += 1
            End With
        End While

    End Sub

Any help would be great.
Thanks
Title: Re: Dynamic array of basic buttons
Post by: Archie on September 10, 2019, 08:08:09 AM
What addresses do you want the buttons to write to?
Title: Re: Dynamic array of basic buttons
Post by: Holmux on September 10, 2019, 09:54:23 AM
Hi Archie

I was using the tag "BValue"

/Holm
Title: Re: Dynamic array of basic buttons
Post by: Archie on September 10, 2019, 10:11:56 AM
You need to put BValue in parenthesis since it is a tag name. This will make every button control the same bit
Title: Re: Dynamic array of basic buttons
Post by: Godra on September 10, 2019, 10:56:45 AM
You could also simplify your code to look similar to this:

Code: [Select]
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim xPos, yPos As Integer
        Dim p As Integer = -1

        For i As Integer = 1 To 10
            ' Let panel hold the Buttons - Toggle example
            pnlButtons.Controls.Add(New AdvancedHMIControls.BasicButton With {
                                    .ComComponent = Me.ModbusTCPCom1,
                                    .PLCAddressClick = "0000" & i,
                                    .Text = i,
                                    .UseVisualStyleBackColor = True,
                                    .OutputType = 4,
                                    .Width = 100,
                                    .Height = 100,
                                    .Left = xPos,
                                    .Top = yPos})
            xPos += 100
            p += 1
            If (p = 4) Then ' Location of second line of buttons:
                xPos = 0
                yPos += 100
                p = -1
            End If
        Next
    End Sub

The attached 1st picture shows what it looks like with a MODRSsim2 simulator next to it (for this example).

Unless you really need array and "s" variable, just make changes as you need it to be.

The attached 2nd picture shows what it looks like with Highlight enabled (and also with a MODRSsim2 simulator next to it), the code is slightly changed:

Code: [Select]
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim xPos, yPos As Integer
        Dim p As Integer = -1

        For i As Integer = 1 To 10
            ' Let panel hold the Buttons - Toggle example with Highlight
            pnlButtons.Controls.Add(New AdvancedHMIControls.BasicButton With {
                                    .ComComponent = Me.ModbusTCPCom1,
                                    .PLCAddressClick = "0000" & i,
                                    .PLCAddressHighlightX = "0000" & i,
                                    .Text = i,
                                    .OutputType = 4,
                                    .Width = 100,
                                    .Height = 100,
                                    .Left = xPos,
                                    .Top = yPos})
            xPos += 100
            p += 1
            If (p = 4) Then ' Location of second line of buttons:
                xPos = 0
                yPos += 100
                p = -1
            End If
        Next
    End Sub

Title: Re: Dynamic array of basic buttons
Post by: Holmux on September 11, 2019, 02:01:25 AM
Thanks

This is great stuff  :)