Reputation: 213
I have the following code which runs on form's load event. In which, row and column are variable and can have different values every time form loads.
Dim XPos, YPos As Integer
Dim btn As Button
XPos = 80
YPos = 80
For i = 1 To row
XPos = 150
For j = 1 To column
btn = New Button
btn.Name = "btn" & i & j
btn.Size = New Drawing.Size(40, 40)
btn.Location = New Point(XPos, YPos)
Me.Controls.Add(btn)
XPos = XPos + btn.Width + 5
Next
YPos = YPos + btn.Height + 5
Next
Suppose, row=5 and column=5 then output will be like
btn11 btn12 btn13 btn14 btn15
btn21 btn22 btn23 btn24 btn25
btn31 btn32 btn33 btn34 btn35
btn41 btn42 btn43 btn44 btn45
btn51 btn52 btn53 btn54 btn55
Now, If I click on btn32 then I can click only on its adjacent buttons like: btn21,btn22,btn23,btn31,btn33,btn41,btn42,btn43 and I can't click on rest of the buttons.
If I click first on btn54 after then I will be only able to click on btn53,btn43,bt44mbtn45,btn55 , I will not be able to click on rest of buttons.
And rest of the buttons should be remained in same style(colour,text.. etc)
How to disable those rest of the buttons. Help me here...
Upvotes: 0
Views: 136
Reputation: 4256
Instead of extracting the row and column of the clicked button like suggested by Visual Vincent, you could store them in properties of the object itself.
Then, like described already - iterate through Me.Controls
and disable everything which is a button and whose row and column property isn't fitting the requirement to leave enabled.
Class GenBtn
Inherits Button
Private row, col As Integer
Public Function isAdjacent(ByRef Column As Integer, ByRef Row As Integer)
If Column < Me.col - 1 Or Column > Me.col + 1 Then
Return False
ElseIf Row < Me.row - 1 Or Row > Me.row + 1 Then
Return False
Else
Return True
End If
End Function
End Class
Tip: Group the controls to easily point only to the generated controls without accidently disabling main elements.
Upvotes: 1