Buttons on various panels - vb.net

I am having headaches with the following, I cannot pass buttons to other panels, my idea is that with the arrangement that I already have of buttons. Pass these created buttons to other panels that I am going to do, can someone help me please.
Dim botones(1, 3) As Button
Dim B As Button
Public Sub crearBotones()
For filas As Integer = 0 To 1
For columnas As Integer = 0 To 3
B = New Button
botones(filas, columnas) = B
With B
.Name = "" & filas.ToString & filas.ToString
.Text = "" & filas.ToString & columnas.ToString
.Left = 100
.Location = New Point(40 + columnas * 70, 5 + filas * 70)
.Height = 25
.Width = 50
Me.Panel1.Controls.Add(botones(filas, columnas))
End With
Next
Next
End Sub
It is showing the buttons on one panel, I am looking for it to show the buttons on both panels.

I mean, the array already makes buttons for me, but I want to put the
buttons on different panels, it's just letting me use it on one panel.
In the panel that is in the for.
Refactor the code just a bit so that your crearBotones() method receives a Panel as a parameter, and only creates the buttons if they have not already been created:
Public Class Form1
Private botones(1, 3) As Button
Public Sub crearBotones(ByVal pnl As Panel)
For filas As Integer = 0 To 1
For columnas As Integer = 0 To 3
If IsNothing(botones(filas, columnas)) Then
Dim B As New Button
With B
.Name = "" & filas.ToString & filas.ToString
.Text = "" & filas.ToString & columnas.ToString
.Left = 100
.Location = New Point(40 + columnas * 70, 5 + filas * 70)
.Height = 25
.Width = 50
End With
botones(filas, columnas) = B
End If
pnl.Controls.Add(botones(filas, columnas))
Next
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
crearBotones(Panel1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
crearBotones(Panel2)
End Sub
End Class

Related

Specify which button to run on event vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rows, column, j, i
rows = 4
column = 13
For j = 0 To rows - 1
Dim r As New TableRow()
For i = 0 To column - 1
Dim c As New TableCell()
If i = 0 And j = 0 Then
c.Text = "TIME/COURT"
r.Cells.Add(c)
ElseIf i = 0 And j = 1 Then
c.Controls.Add(New LiteralControl("Court A"))
r.Cells.Add(c)
ElseIf i = 0 And j = 2 Then
c.Controls.Add(New LiteralControl("Court B"))
r.Cells.Add(c)
ElseIf i = 0 And j = 3 Then
c.Controls.Add(New LiteralControl("Court C"))
r.Cells.Add(c)
ElseIf i >= 1 And j = 0 Then
c.Controls.Add(New LiteralControl(x & "-" & x + 1))
r.Cells.Add(c)
x += 1
Else
btn = New Button
btn.ID = "btnr" & j & "c" & i
AddHandler btn.Click, AddressOf Change_Colour
c.Controls.Add(btn)
r.Cells.Add(c)
End If
Next
booktable.Rows.Add(r)
Next
End Sub
Protected Sub Change_Colour(sender As Object, e As EventArgs)
btn.BackColor = Drawing.Color.LightGreen
End Sub
When I click on any of the button, only the last button turns light green.
How can I set the button to run the event when I click it?
Can I call the button by button.ID? How?
If multiple buttons can call your Change_Colour routine them you will need to actually determine which one did call it. so you will need something like:
Protected Sub Change_Colour(sender As Object, e As EventArgs)
Dim btn as Button = Ctype(sender, Button)
'here you will need to determine which button is the sender which you could do by checking its name for example
'eg Select case btn.name
'case add stuff here
'end select
btn.BackColor = Drawing.Color.LightGreen
End Sub

Computer Click Event Visual Basic

How do I create a random click event driven by the computer? Where the user doesn't get to choose where it clicks?
I have created a table that is 5 buttons wide and 5 buttons tall, and created a click event where the user clicks on a button and the random number value is added onto their score.
Dim RandomNumbers = Enumerable.Range(0, 100).ToList()
Dim RandomNumber As New Random()
For Me.TableColunm = 0 To 4 Step 1
For Me.TableRow = 0 To 4 Step 1
Dim TemporaryNumber = RandomNumbers(RandomNumber.Next(0, RandomNumbers.Count))
RandomNumbers.Remove(CInt(TemporaryNumber))
TableButtons = New Button()
With TableButtons
.Name = "TextBox" & TableColunm.ToString & TableRow.ToString
.Text = TemporaryNumber.ToString
.Width = CInt(Me.Width / 4)
.Left = CInt(TableColunm * (Me.Width / 4))
.Top = TableRow * .Height
'.Tag = TemporaryNumber
AddHandler TableButtons.Click, AddressOf UserTableButtonClickEvent
End With
GameScreen.Controls.Add(TableButtons)
Next TableRow
Next TableColunm
Catch ex As Exception
MsgBox(ex.StackTrace & vbCrLf & "index1= " & TableColunm & vbCrLf & "index2= " & TableRow)
End Try
Private Sub UserTableButtonClickEvent(sender As Object, e As EventArgs)
CType(sender, Button).BackColor = Color.LightSteelBlue
OverAllScoreInteger += CInt(CType(sender, Button).Tag)
GameScreen.UserScoreBox.Text = OverAllScoreInteger.ToString
Dim TableButton As Button = CType(sender, Button)
TableButton.Text = "#"
TableButton.Enabled = False
End Sub
How do I make another event like this but at random?
Enumerate all controls in the GameScreen.Controls collection and add them to a temporary list if they are a button. Then generate a random number in the range of the length of the list and click the button with this index:
Dim TempButtonList As New List(Of Button)
For Each c as Control in GameScreen.Controls
If TypeOf(c) Is Button Then TempButtonList.Add(CType(c, Button))
Next
Dim Rnd as New Random
TempButtonList(rnd.Next(0, TempButtonList.Count-1)).PerformClick()
The PerformClick() method does exactly the same as if you would click that button with your mouse. The If in the loop makes sure you only add buttons in the controls collection, because there may of course be other controls inside.
Complete example
It's probably easier that way (Add a FlowLayoutPanel with Size=275; 275 and two Labels to a Form):
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Rnd As New Random
For i = 1 To 25
Dim b As New Button With {.Text = Rnd.Next(0, 100)}
b.Width = 40
b.Height = 40
FlowLayoutPanel1.Controls.Add(b)
AddHandler b.Click, AddressOf b_hndl
Next
End Sub
Private MyScore As Integer = 0
Private HisScore As Integer = 0
Dim Zug As Boolean = True
Private Sub b_hndl(sender As Object, e As EventArgs)
Dim ThisB As Button = CType(sender, Button)
Dim CurrScore As Integer = CInt(ThisB.Text)
Dim CurrZug As Boolean = Zug
Zug = Not Zug
ThisB.Enabled = False
ThisB.Text = "#"
If CurrZug Then
MyScore += CurrScore
Label1.Text = MyScore.ToString
ComputerMove()
Else
HisScore += CurrScore
Label2.Text = HisScore.ToString
End If
End Sub
Private Sub ComputerMove()
Dim TempButtonList As New List(Of Button)
For Each c As Control In FlowLayoutPanel1.Controls
If TypeOf (c) Is Button Then
Dim thisb As Button = CType(c, Button)
If thisb.Enabled Then TempButtonList.Add(thisb)
End If
Next
If TempButtonList.Count = 0 Then Exit Sub
Dim Rnd As New Random
TempButtonList(Rnd.Next(0, TempButtonList.Count - 1)).PerformClick()
End Sub
End Class
The Dim Zug As Boolean = True determines if the player or the computer is currently in queue to select a button. It is switched on every button press, so the players take turns. The ComputerMove function is executed after the player clicked a button.

How to change image in picture box when clicked

I have used the following code in a program that I am designing to book seats. Each picturebox is a seat, and when each picturebox is clicked, the image should change from Seating_No_Person to Seating_With_Person to show that the seat has been selected. I am currently getting a problem with the changing image, as when clicked, none of the pictureboxes swap images. Anyone got any suggestions?
Thanks
Public Class Form1
Public Class Seating
Public SeatRow As Integer = 0
Public SeatColumn As Integer = 0
Public PB As PictureBox = Nothing
Public Occupied As Boolean = False
End Class
Private seatingList As New List(Of Seating)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = -50
Dim yPosition As Integer = -25
For i As Integer = 1 To 5
'Number of rows
For j As Integer = 1 To 10
Dim pb As New PictureBox
With pb
.Name = "PictureBox" & i.ToString & j.ToString
'Name of Picture box i.e. if i = 1 (row 1), j = 3 (column 3), name is PictureBox13
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
'Size of seat is 60 by 60
.Location = New Point(xPosition + (j * 70), yPosition + (i * 70))
'Location of picture box is: -50 + (columnnumber * 70), -25 + (rownumber * 70)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
Dim thisSeating As New Seating
With thisSeating
.SeatRow = i
.SeatColumn = j
.PB = pb
.Occupied = True
End With
seatingList.Add(thisSeating)
End With
Next
Next
End Sub
Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim seatRowNum As Integer = CInt(pb.Name.Replace("PictureBox", ""))
Dim seatColumnNum As Integer = CInt(pb.Name.Replace("PictureBox", ""))
Dim qry = From seat As Seating In seatingList Where seat.SeatRow = seatRowNum And seat.SeatColumn = SeatColumnNum
If qry.Count = 1 Then
If qry.First.Occupied = True Then
pb.Image = My.Resources.Seating_No_Person
qry.First.Occupied = False
Else
pb.Image = My.Resources.Seating_With_Person
qry.First.Occupied = True
End If
End If
End Sub
End Class
I would suggest setting a breakpoint and debugging to see where you're going wrong. If you just call DirectCast(sender, PictureBox).Image = My.Resources.Seating_With_Person inside Private Sub PictureBox_Click it works, which suggests that there is problem with the logic inside your If block.

Shuffling Text of Button

How can I implement shuffling of text on my button, i.e. text = chr(n+48) to shuffle text on each button.
Dim n As Integer = 0
For i As Integer = 0 To 10
' Initialize one variable
btnArray(i) = New Button
Next i
While n < 10
With btnArray(n)
.Tag = n + 1 ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(n + 48)
FlowLayoutPanel1.Controls.Add(btnArray(n))
AddHandler .Click, AddressOf Me.GenericClickHandler
n = n + 1
End With
End While
I don't know if there is a reason why you are doing that to two steps.
Try something like this:
Private btnArray As New List(Of Button)
For i As Integer = 0 To 10
Dim btn As New Button
With btn
.Tag = i ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(i + 48)
End With
btnArray.Insert(i, btn)
'FlowLayoutPanel1.Controls.Add(btnArray(i)) 'It works also
FlowLayoutPanel1.Controls.Add(btn)
AddHandler .Click, AddressOf Me.GenericClickHandler
Next i
Based on your comments:
Private btnArray As New List(Of Button)
Private btnShuffleArray As New List(Of Button)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
' Initialize one variable
Dim btn As New Button
With btn
.Tag = i ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(i + 48)
btnArray.Insert(i, btn)
' FlowLayoutPanel1.Controls.Add(btnArray(i))
'AddHandler .Click, AddressOf Me.GenericClickHandler
End With
Next i
'Randomize the list
Dim rand As New Random
Dim index As Integer
While btnArray.Count > 0
index = rand.Next(0, btnArray.Count)
btnShuffleArray.Add(btnArray(index))
btnArray.RemoveAt(index)
End While
For i = 0 To 10
FlowLayoutPanel1.Controls.Add(btnShuffleArray(i))
Next
End Sub

vb.net disable runtime created buttons

Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.
This is the code i have for that button:
Dim intXX As Integer = 0
Do Until intXX = intX
userAvatar(intXX).Enabled = False
intXX = intXX + 1
Loop
The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?
How i create the buttons are like this:
private sub createButton()
Dim personAvatar As New PictureBox
With personAvatar
.AutoSize = False '> ^
If intX = 7 Then
thePrviousAvatarImg = 0
End If
If intX <= 6 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
ElseIf intX >= 7 And intX <= 14 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
Else
Exit Sub
End If
.Name = "cmd" & nameOfPerson
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BackgroundImage = Image.FromFile(theAvatarDir)
.Tag = nameOfPerson
.BringToFront()
End With
AddHandler personAvatar.Click, AddressOf personAvatar_Click
Me.Controls.Add(personAvatar)
userAvatar(intX) = personAvatar
intX = intX + 1
End With
End Sub
Thanks for your time and help!
David
You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.
So before you create each picture box, declare an array to store them:
Dim MyPictureBoxes() as PictureBox
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.enabled = False
Next
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Public Class Form1
Dim MyPictureBoxes(4) As PictureBox
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim personAvatar As New PictureBox
Dim intX As Integer = 0
While intX < 5
personAvatar = New PictureBox
With personAvatar
.AutoSize = False
.Left = intX * 100
.Top = 100
.Name = "cmd" & intX
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BringToFront()
End With
Me.Controls.Add(personAvatar)
MyPictureBoxes(intX) = personAvatar
intX = intX + 1
End While
End Sub
Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.Enabled = False
Next
End Sub
End Class
Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.