creating a control

Post Reply
bill-lancaster
Posts: 88
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

creating a control

Post by bill-lancaster » Thursday 19th March 2020 8:43am

I can create a series of button controls with:-

Code: Select all

  for i = 0 to 5
    With hBtn = New Button(Me) As "BtnTest"
      .X = 10 * i
      .Y = 10
      .W = 10
      .H = 10
      .Name = "Btn" & i
      .text = "+"
    End With
  next
How can I access or change the properties of a particular button?
This is the only way I have at the moment:-

Code: Select all

Dim hControl As Control
  For Each hControl In Me.Controls
    If hControl.Name = "Btn5" Then hControl.Hide
  Next
Any advice would be welcome

User avatar
cogier
Site Admin
Posts: 428
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: creating a control

Post by cogier » Thursday 19th March 2020 10:58am

Hi Bill,

Try this code (Note I use the 'gb' button on the Forum not the '</>' button to get Gambas code formatting)
hBtn As Button
hButtons As New Button[]

Public Sub Form_Open()

  Dim i As Integer
  Dim Label1 As Label
  
  With Label1 = New Label(Me)
    .Height = 28
    .Width = 200
    .Text = "Click a button"
  End With

 For i = 0 To 5
    With hBtn = New Button(Me) As "BtnTest" 
      .X = 50 * i
      .Y = 40
      .W = 50
      .H = 50
      .Name = "Btn" & i
      .text = "+"
    End With
    hButtons.Add(hBtn)
Next

End

Public Sub BtnTest_Click()

hButtons[0].Text = "H"
hButtons[1].Text = "E"
hButtons[2].Text = "L"
hButtons[3].Text = "L"
hButtons[4].Text = "O"
hButtons[5].Text = "!"

End

bill-lancaster
Posts: 88
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: creating a control

Post by bill-lancaster » Thursday 19th March 2020 1:49pm

That's brilliant! Thanks Cogier

Post Reply