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.
Related
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
I want get value 1 in all line for get path string, and programmatically add cover to flowlayoutpanel.
in Resource/Game List.ini (from drag n drop)
Apex Legends,Resource/Cover/Apex Legends.jpg,Resource/Game Info/Apex Legends.txt
Fortnite,Resource/Cover/Fortnite.jpg,Resource/Game Info/Fortnite.txt
PUBG,Resource/Cover/PUBG.jpg,Resource/Game Info/PUBG.txt
here my code :
Private Sub LabelSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelSetting.Click
FlpAddItem.Controls.Clear()
'I am confused in this part to get value 1 in all line for get path string
'Directory.GetFiles(Path) will be replace with streamreader from lines(i) value 1
Dim Path = '???
Dim ImageX As Image = Nothing
Dim x As Int32 = Directory.GetFiles(Path).Count - 1
Dim Img(x) As PictureBox
Dim ImgText(x) As Label
Dim ImgPanel As Panel
For i = 0 To Directory.GetFiles(Path).Count - 1
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = Directory.GetFiles(Path)(i).Replace(Path, "").Replace(".jpg", "").Replace(".png", "")
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(Directory.GetFiles(Path)(i), True)
Img(i).Image = Image.FromFile(Directory.GetFiles(Path)(i))
ImgText(i).Text = Directory.GetFiles(Path)(i)
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
I suggest creating a class for the game name and path information
Public Class GamePath
Public Property GameName As String
Property Path As String
Public Overrides Function ToString() As String
Return GameName
End Function
End Class
I have overridden ToString, so that the game name will automatically be displayed in a ListBox.
When loading the form, I read this information from the INI-file and set it as data source of a listbox, where you will be able to select a game.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim games =
From line In File.ReadLines(IniFilePath)
Let parts = line.Split(","c)
Select New GamePath With {.GameName = parts(0), .Path = parts(1)}
GameListBox.DataSource = games.ToList()
GameListBox.SelectedIndex = 0 'Select first game
End Sub
Note that it is easier to use File.ReadLines than a StreamReader. You will have to add Imports System.IO to the top of the code. Then we use the LINQ syntax to split each line at the comma and to create the game path information.
The user selects a game in the ListBox and then clicks a button. You can get the file path information from the ListBox like this:
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Then read the files only once and assign the result to a variable
Dim files As String() = Directory.GetFiles(gamePath.Path)
Get the file count
Dim fileCount As Integer = files.Count
The whole Click method:
Private Sub StartGameButton_Click(sender As Object, e As EventArgs) Handles StartGameButton.Click
FlpAddItem.Controls.Clear()
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Dim files As String() = Directory.GetFiles(gamePath.Path)
Dim fileCount As Integer = files.Count
Dim ImageX As Image = Nothing
Dim Img(fileCount) As PictureBox
Dim ImgText(fileCount) As Label
Dim ImgPanel As Panel
For i = 0 To fileCount - 1
Dim filePath = files(i)
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
Some details:
In the For-loop you can get the path of an image file with
Dim filePath = files(i)
You can get the name of the image with
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
This automatically removes the directory name and the extension.
Later on, you don't call Directory.GetFiles again:
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
If you only want to read the file paths into a list, you could write
Dim games =
(From line In File.ReadLines(IniFilePath)
Select line.Split(","c)(1)).ToList()
I am trying to make a form grow and shrink when I enter and leave it with my mouse. However, when the mouse pointer goes over a control, it runs the mouse.leave script. How do I stop this?
Mouse.Leave code
Private Sub frm_MouseLeave(ByVal sender As System.Object, ByVal e As EventArgs)
Dim frm1 As Form = DirectCast(sender, Form)
frm1.Opacity = 0.4
frm1.Controls.Clear()
frm1.BringToFront()
frm1.Size = New Size(20, 50)
Dim test As Integer = 1
Dim counter As Integer = 0
Dim yaxis As Integer = 0
Dim fin As Boolean = False
Do Until fin = True
If frm1.Name = "frm" & test Then
yaxis = counter
fin = True
Else
counter += 10 + frm1.Height
test += 1
End If
Loop
frm1.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm1.Width, yaxis)
End Sub
Mouse.enter code
Private Sub frm_MouseEnter(ByVal sender As System.Object, ByVal e As EventArgs)
Dim frm1 As Form = DirectCast(sender, Form)
Dim lbl As Label = New Label
Dim btn As Button = New Button
Dim index As Integer = 0
For Each ch As Char In frm1.Name
If IsNumeric(ch) Then
index = index & ch
End If
Next
index -= 1
frm1.Controls.Add(lbl)
lbl.Text = listbox.Items.Item(index)
lbl.ForeColor = Color.White
lbl.AutoSize = True
lbl.Location = New Point((frm1.ClientSize.Width) / 2, (frm1.ClientSize.Height) / 2)
lbl.Show()
frm1.Controls.Add(btn)
btn.Text = "X"
btn.ForeColor = Color.White
btn.BackColor = Color.Black
btn.Font = New Drawing.Font("Arial", 12)
btn.AutoSize = True
btn.Location = New Point(200 - btn.Width, 0)
frm1.Opacity = 1
frm1.BringToFront()
frm1.Size = New Size(200, 100)
Dim test As Integer = 1
Dim counter As Integer = 0
Dim yaxis As Integer = 0
Dim fin As Boolean = False
Do Until fin = True
If frm1.Name = "frm" & test Then
yaxis = counter
fin = True
Else
counter += 60
test += 1
End If
Loop
frm1.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm1.Width, yaxis)
End Sub
Thanks for the help!!
Everytime you enter one of your controls you will generate a MouseEnter event for the control and a MouseLeave Event for the form, I would look at checking the position of the mouse and if it was contained in the Form exit the MouseLeave event. Try something like to see if it works for you.
If frm1.ClientRectangle.Contains(PointToClient(MousePosition)) Then Exit Sub
Also as an aside if you are not going to be using this code with multiple forms you can just use the Me keyword to reference your Form's Properties
If Me.ClientRectangle.Contains(PointToClient(MousePosition)) Then Exit Sub
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.
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