creating a control

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

creating a control

Post by bill-lancaster »

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: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: creating a control

Post by cogier »

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: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: creating a control

Post by bill-lancaster »

That's brilliant! Thanks Cogier
Post Reply