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
Related
My code creates 40 buttons dynamically I can change properties if the button is clicked using sender but how do I reference them when I click a different button
Public Sub loadButtons()
For i As Integer = 0 To 39
If i > 19 Then
gap = i + 10
Else
gap = i
End If
Dim B As New Button
Form1.panSeats.Controls.Add(B)
B.Height = 30
B.Width = 37
B.Left = ((i Mod 10) * 47) + 322
B.Top = ((gap \ 10) * 31) + 114
B.Text = Chr((i \ 10) + Asc("A")) & i Mod 10 + 1
B.Tag = i
Buttons.Add(B.Text, B)
If isBooked(i) = True Then
B.BackColor = Color.Red
Else
B.BackColor = Color.Orange
End If
AddHandler B.Click, AddressOf Form1.Button_Click
Next
End Sub
I want to be able to change the backcolor B isn't declared have tried using the button.tag but not working
Private Sub btnTestCancel_Click(sender As Object, e As EventArgs) Handles btnTestCancel.Click
B.BackColor = Color.Orange
End Sub
The Addhandler statement wires the events for all dynamically added buttons. The sender object is the one being clicked, we just need to unbox it.
AddHandler B.Click, AddressOf btns_Click
Private Sub btns_Click(sender As Object, e As EventArgs) 'no handles clause needed
Dim btn As Button = DirectCast(sender, Button)
btn.BackColor = Color.Orange
End Sub
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.
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.
i'm currently doing a program that aims to sort values of dynamically created textboxes. how to do this in vb.net? This is my code for creating the textboxes:
For cnt = 0 To Val(TextBox1.Text) - 1
arrivalbox.Add(New TextBox)
With arrivalbox(cnt)
.Parent = Me
.Left = 0
.Height = 13
.Width = 65
.Top = .Height * cnt + 50
.Visible = True
.Tag = cnt
.Text = ""
.Name = "arrival" & cnt
.Location = New Point(380, 120 + (cnt * 25))
.TextAlign = HorizontalAlignment.Center
.Enabled = False
Me.Controls.Add(New TextBox)
End With
Something like this will loop through all the textboxes and add them to a sortedlist which will sort all the textboxes according to the values.
I'm not sure what you are trying to achieve here. But im guessing once you get these sorted then you can change the position based on their position in the sorted list
Dim listedboxes As SortedList(Of Double, TextBox)
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
listedboxes.add(ctrl.value,ctrl)
End if
Next
EDIT
maybe something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim listedboxes As New SortedList(Of Double, TextBox)
For i As Integer = 0 To TextBox1.Text.Length - 1
Dim tb As New TextBox
With tb
.Parent = Me
.Left = 0
.Height = 13
.Width = 65
.Top = .Height * i + 50
.Visible = True
.Tag = i
.Text = TextBox1.Text.Substring(i, 1)
.Name = "arrival" & i
.TextAlign = HorizontalAlignment.Center
.Enabled = False
Me.Controls.Add(New TextBox)
End With
listedboxes.Add(TextBox1.Text.Substring(i, 1), tb)
Next
Dim j = 0
For Each kvp As KeyValuePair(Of Double, TextBox) In listedboxes
kvp.Value.Location = New Point(380, 120 + (j * 25))
j += 1
Next
End Sub
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.