Author Topic: Dynamic array of basic buttons  (Read 1494 times)

Holmux

  • Newbie
  • *
  • Posts: 34
    • View Profile
Dynamic array of basic buttons
« 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

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Dynamic array of basic buttons
« Reply #1 on: September 10, 2019, 08:08:09 AM »
What addresses do you want the buttons to write to?

Holmux

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Dynamic array of basic buttons
« Reply #2 on: September 10, 2019, 09:54:23 AM »
Hi Archie

I was using the tag "BValue"

/Holm

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Dynamic array of basic buttons
« Reply #3 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

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Dynamic array of basic buttons
« Reply #4 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

« Last Edit: September 10, 2019, 09:22:21 PM by Godra »

Holmux

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Dynamic array of basic buttons
« Reply #5 on: September 11, 2019, 02:01:25 AM »
Thanks

This is great stuff  :)