Which auto generated picturebox was clicked? VB.net - vb.net

I have the following code. Im trying to find out which of the 64 pictureboxes was clicked:
For i As Integer = 1 To 8
For j As Integer = 1 To 8
SpilleBræt(i, j) = New PictureBox 'Opretter picturebox
If (i + j) Mod 2 = 1 Then
Me.SpilleBræt(i, j).BackgroundImage = Skak.My.Resources.DarkTile
Else
Me.SpilleBræt(i, j).BackgroundImage = Skak.My.Resources.LightTile
End If
'Placering, størrelse, m.v.
Me.SpilleBræt(i, j).Location = New System.Drawing.Point((i - 1) * 103, (j - 1) * 103)
Me.SpilleBræt(i, j).Size = New System.Drawing.Size(100, 100)
Me.SpilleBræt(i, j).Name = "SpilleBrik" & i & j
Me.PanelSpilleBræt.Controls.Add(Me.SpilleBræt(i, j))
Next j
Next i
Thanks.

In order to handle a click event, the first thing you'll need is a click handler. Could be something as simple as this:
Private Sub PictureBox_Click(ByVal sender As Object, ByVal e As EventArgs)
' Do something in here
End Sub
When you create your PictureBox controls, bind the handler to their click event:
AddHandler Me.SpilleBræt(i, j).Click, AddressOf PictureBox_Click
Me.PanelSpilleBræt.Controls.Add(Me.SpilleBræt(i, j))
What this should do is invoke the PictureBox_Click method any time the user clicks on the PictureBox. Within that method, sender is the element which was clicked:
Private Sub PictureBox_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim clickedBox As PictureBox
clickedBox = CType(sender, PictureBox)
' clickedBox is the element which was clicked
End Sub

Related

Creating handle in a Windows Form with a declared object as an array

Im trying to make a Connect 4 game just to practice some windows forms which im new to. What my code does is creates a grid of 7 x 6 regularly spaces blank PictureBox's. But since im creating them in the script and not using the form1 design windows i dont know how i would add Handles to them, especially since the PictureBox's are in an array. Any ideas?
Public Class Form1
Dim Grid(6, 5) As PictureBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Visible = False
Me.FormBorderStyle = FormBorderStyle.FixedSingle
For i As Integer = 0 To 6
For j As Integer = 0 To 5
Grid(i, j) = New PictureBox
Grid(i, j).BackColor = Color.LightGray
Grid(i, j).Size = New Size(90, 90)
Grid(i, j).Location = New Point((i * 100) + 10, (j * 100) + 10)
Grid(i, j).Visible = True
Controls.Add(Grid(i, j))
Next
Next
End Sub
Private Sub Grid_MouseHover(sender As Object, e As EventArgs) Handles Grid(x, y).MouseHover 'Doesnt work
'Run depending on which picturebox in array
End Sub
End Class
I can get an error which is "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."
One possible way would be to set the .Tag property using the coordinates -
add something like into your For..Next loop
Grid(i, j).Tag = i.ToString & j.ToString
and use
AddHandler Grid(i, j).MouseHover, AddressOf Grid_MouseHover
and add this after the one above.
Then, change the first line of your MouseHover Sub to
Private Sub Grid_MouseHover(sender As Object, e As EventArgs)
with no handler on the end.
Finally, change the type of the sender to a PictureBox
Private Sub Grid_MouseHover(sender As Object, e As EventArgs)
Dim Pbox As PictureBox = CType(sender, PictureBox)
Dim i As Integer = Integer.Parse(Pbox.Tag.ToString(0))
Dim j As Integer = Integer.Parse(Pbox.Tag.ToString(1))
End Sub
To access the Picturebox and its properties, just use PBox and if you need the coordinates, use i and j

How do I add buttons with event handlers to a form dynamically?

Is it possible to do this dynamically? How?
Code:
Public Class Form1
Dim c(40) As Integer
Dim IMG As PictureBox
Dim lbl_n(40) As Label
Dim lbl_pr(40) As Label
Dim lbl_ref(40) As Label
Dim lbl_dim(40) As Label
Dim lbl_col(40) As Label
Dim btn_add(40) As Button
Dim btn_rmv(40) As Button
Dim tb_qt(40) As TextBox
AddHandler btn_add(0).Click, AddressOf btn_add0_Click
AddHandler btn_add(1).Click, AddressOf btn_add1_Click
[...]
AddHandler btn_add(40).Click, AddressOf btn_add40_Click
Public Sub btn_add0_Click(ByVal sender As Object, ByVal e As System.EventArgs)
c(0) = c(0) + 1
tb_qt(0).Text = c(0)
End Sub
Public Sub btn_add1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
c(1) = c(1) + 1
tb_qt(1).Text = c(1)
End Sub
[...]
Public Sub btn_add40_Click(ByVal sender As Object, ByVal e As System.EventArgs)
c(40) = c(40) + 1
tb_qt(40).Text = c(40)
End Sub
These are the images with program running (they are edited):
Form1
Form2
I want to dynamically, because I could use more than 40 products! And I need to do 40 addhandlers, so that more 40 to remove!
How can I do that?
Yes, You can do that dynamically.
In this example, I put those buttons and textboxes into Panel.
There is example with comments which line is for what :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'dynamically adding buttons (so You don't need create every button separately)
For x = 1 To 40
Dim btn As New Button 'create new button
btn.Name = "btn_add" & x.ToString 'set ID for button (example: btn_add1, btn_add2, ... btn_add40)
btn.Text = "+" 'set button text
btn.Tag = x.ToString 'set button NO. for finding corrent textbox whos belong to this button (for example: tb_qt1 belong to buttons btn_add1 and btn_rem1)
btn.Width = 24 'this is for styling
btn.Top = (x * btn.Height) + 3 'for styling, too. Top poistion of button
btn.Left = 0 'for styling, too. Left position of button
AddHandler btn.Click, AddressOf btnAdd_Click 'creating sub called btnAdd_Click (this sub will handle all, in this case 40, buttons)
Panel1.Controls.Add(btn) 'for this example I put buttons and textboxes into panel (autoscroll set to True)
btn = New Button 'same thing just for remove button
btn.Name = "btn_rem" & x.ToString
btn.Text = "-"
btn.Tag = x.ToString
btn.Width = 24
btn.Top = (x * btn.Height) + 3
btn.Left = btn.Width + 3
AddHandler btn.Click, AddressOf btnRem_Click 'creating sub called btnRem_Click (this sub will handle all, in this case 40, buttons)
Panel1.Controls.Add(btn)
Dim txt As New TextBox 'same thing for textboxes
txt.Name = "tb_qt" & x.ToString
txt.Text = "0"
txt.Tag = x.ToString
txt.Top = (x * btn.Height) + 3
txt.Left = btn.Left + btn.Width + 3
txt.TextAlign = HorizontalAlignment.Right
Panel1.Controls.Add(txt)
Next
End Sub
Public Sub btnAdd_Click(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button) 'detect which button clicked. You can add line: MsgBox(btn.Name) to see what happening
Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0) 'find textbox which belong to this button. this is why set .Tag into buttons
txt.Text = CInt(txt.Text) + 1 'just do math and change value, by adding one(1), in textbox (by this way I avoid using Your Dim c(40) As Integer)
End Sub
Public Sub btnRem_Click(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button) 'same thing like for btnAdd_Click, but we doing subtract for one(1) value in textbox
Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0)
txt.Text = CInt(txt.Text) - 1
End Sub
Because I use panel like container for all those buttons and textboxes, You have to set AutoScroll=True for panel.
I hope so You will understand how it's work for start.

Dynamic TableLayout and Control

created dynamic table layout and add some controls in to that table layout.my code
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
dynamicTable.ColumnCount = 5
dynamicTable.RowCount = 1
For i = 1 To 5
Dim button= New button()
button.Text = i.ToString()
dynamicTable.SetColumn(button, i)
dynamicTable.Controls.Add(button)
Next
Next
End Sub
Now i added 5 buttons to the table layout.
Now i am going to click on the button. How can i know ,on which button i clicked ?
Try like this
Dim dynamicTable As New TableLayoutPanel
dynamicTable.ColumnCount = 5
dynamicTable.RowCount = 1
For i = 1 To 5
Dim button = New Button()
button.Name = "button" & i
AddHandler button.Click, AddressOf Click1
Button.Text = i.ToString()
dynamicTable.SetColumn(button, i)
dynamicTable.Controls.Add(button)
Next
Me.Controls.Add(dynamicTable)
Private Sub Click1(ByVal sender As System.Object, ByVal e As System.EventArgs)
If sender.Name = "button1" Then
MsgBox("Hi")
End If
End Sub

How to call a dynamically created label from its associated dynamically created button's click in vb.net

I have a tab in a form. On form load, I am getting text from a text file line by line and displaying them as labels on a form Tabcontrol Tabpage along with dynamically displaying buttons beside them. Now on those buttons click I want to copy the text in the associated labels. Can anyone suggest what to put in the Nextbtn_Click event?
Dim FILE_NAME As String = "D:\1.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
i += 1
Next
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
End Sub
Store the assc. label in the tag property and you can cast it back when you click on the button. The sender object is the button that is currently clicked.
Dim FILE_NAME As String = "D:\1.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
Nextbtn.Tag = NextLabel
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
i += 1
Next
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
End Sub
Private Sub Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
TextBox2.Text = b.Name
Clipboard.SetText(b.Name)
End Sub

vb.net deleting lots of dynamically created buttons

I'm a new programmer to vb.net, so apologise for what is likely to be ignorance.
I’m building a simple gui for a database interface, with many parent and child items within it. Upon a form I create buttons depending on how many items (parents/children). I've got the creation of the buttons thus:
For RowNumber As Integer = 0 To NoOfRows
Dim Buttoni As New Button
Buttoni.Location = New Point(LocationX, LocationY)
Buttoni.Width = 100
Buttoni.Height = 40
Buttoni.Visible = True
Buttoni.Text = DatasetA.Tables(0).Rows(RowNumber).Item("Name")
ButtonName = "Button" + RowNumber.ToString
If LocationX < FormWidth - (SpacePerButtonX * 2) Then
LocationX = LocationX + SpacePerButtonX
Else
LocationX = 50
LocationY = LocationY + SpacePerButtonY
End If
AddHandler Buttoni.Click, AddressOf DynamicButtonClick
Me.Controls.Add(Buttoni)
Buttoni.BringToFront() 'brings newest buttons to front!
Next
But I’m struggling with a way to delete the buttons to make way for a new set to replace them... I can delete a single one upon its click, but I’d like to delete all of the buttons that have been created in this way before re-creating them.
I hope that makes sense and there is a fairly simple way to accomplish this..?
I will add, in your creation loop, some value to the Tag property.
This will help to differentiate the buttons created dinamically from the buttons created statically in your form.
Buttoni.Tag = 1
Then, to delete a button, loop in reverse order on the Me.Controls collection,
check if you get a button and if the Tag property IsNot Nothing
For x as Integer = Me.Controls.Count - 1 to 0 step -1)
Dim b as Button = TryCast(Me.Controls(x), Button)
If b IsNot Nothing AndAlso b.Tag IsNot Nothing then
b.Dispose() '' NOTE: disposing the button also removes it
End If
Next
It's hard to know exactly what you want to do. I guess you could just use the same technique in reverse, something like
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
Dim ctrl = Me.Controls(i)
If TypeOf (ctrl) Is Button Then
ctrl.Dispose() '' NOTE: disposing the control also removes it
End If
Next
Create a button and delete it with double click!
Easy Code :
Dim b As New Button
Private btn As Button ' this is a reference object
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodebtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Clicks = 2 Then
b.Dispose()
End If
If e.Button = MouseButtons.Left Then
drag = True
btn = CType(sender, Button)
ptX = e.X : ptY = e.Y
End If
End Sub
Private Sub nodebtn_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
Me.Refresh()
End If
End Sub
Private Sub nodebtn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
b.Location = New Point(10, 10)
b.Size = New Size(110, 29)
b.BringToFront()
b.Text = "Button"
AddHandler b.MouseDown, AddressOf nodebtn_MouseDown
AddHandler b.MouseMove, AddressOf nodebtn_MouseMove
AddHandler b.MouseUp, AddressOf nodebtn_MouseUp
Me.Controls.Add(b)
End Sub