How to change image in picture box when clicked - vb.net

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.

Related

Show DateTimePicker in Textbox

I've set DatetimePicker.Visible=False, and when I click on Textbox I want It to be displayed inside of Textbox. My code works for 1 Textbox, but not the other one:
Private Sub Show_DTP(Ctl As Control)
Dim x As Integer = 0
Dim y As Integer = 0
Dim Width As Integer = 0
Dim height As Integer = 0
Dim rect As Rectangle
Select Case Ctl.Name
Case "TxtTest"
rect = TxtTest.DisplayRectangle()
x = rect.X + TxtTest.Left
y = rect.Y + TxtTest.Top
Width = rect.Width + 4
height = rect.Height
With My_DTP
.SetBounds(x, y, Width, height)
.Visible = True
.Focus()
End With
Case "Txt2"
rect = Txt2.DisplayRectangle()
x = rect.X + Txt2.Left
y = rect.Y + Txt2.Top
Width = rect.Width + 4
height = rect.Height
With My_DTP
.SetBounds(x, y, Width, height)
.Visible = True
.Focus()
End With
End Select
End Sub
Private Sub Text_Click(sender As Object, e As EventArgs) Handles TxtTest.Click, Txt2.Click
Dim Txt As System.Windows.Forms.TextBox = DirectCast(sender, TextBox)
My_DTP.Visible = False
Show_DTP(Txt)
End Sub
What is wrong here ?
There is no need for the case statement: you do the same things regardless of the actual textbox.
I put two textboxes named "TxtTest" and "Txt2" on a form and added a DateTimePicker named "MyDTP". With the following code it behaved as you appear to want:
Option Infer On
Option Strict On
Public Class Form1
Private Sub Show_DTP(target As TextBox)
Dim rect As Rectangle = target.DisplayRectangle()
Dim x As Integer = rect.X + target.Left
Dim y As Integer = rect.Y + target.Top
Dim width = rect.Width + 4
Dim height = rect.Height
With MyDTP
.SetBounds(x, y, Width, height)
.Visible = True
.Focus()
End With
End Sub
Private Sub Text_Click(sender As Object, e As EventArgs) Handles TxtTest.Click, Txt2.Click
Dim Txt As System.Windows.Forms.TextBox = DirectCast(sender, TextBox)
MyDTP.Visible = False
Show_DTP(Txt)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyDTP.Visible = False
End Sub
End Class

VB.NET Make label show upon mouse enter

I have a code that makes small boxes at the side of the screen that when the mouse hovers over it, it grows and displays information in the label. How could I do this? Currently, the form does not register the label.
This is the button to make the form and the label.
Private Sub MakeForm()
Dim number As Integer = 1
Dim xaxis As Integer = 0
Dim yaxis As Integer = 0
Dim formlist As New List(Of Form)
Dim index As Integer = 0
For Each x In lstDate.Items
Dim frm As New Form
frm.Name = "frm" & number
frm.Text = "New Form"
frm.StartPosition = FormStartPosition.Manual
frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
frm.TopMost = True
frm.Opacity = 0.4
Dim lbl As New Label
lbl.Text = x & vbNewLine & lstAssignments.Items.Item(index) & vbNewLine & lstAN.Items.Item(index)
lbl.ForeColor = Color.White
frm.Controls.Add(lbl)
lbl.Hide()
AddHandler frm.MouseEnter, AddressOf frm_MouseEnter
AddHandler frm.MouseLeave, AddressOf frm_MouseLeave
If DateDiff(DateInterval.Day, Now(), x) <= 1 Then
frm.BackColor = Color.Red
Else
frm.BackColor = Color.Black
End If
frm.AllowTransparency = True
formlist.Add(frm)
frm.Show()
number += 1
frm.Size = New Size(20, 50)
frm.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm.Width, yaxis)
yaxis += frm.Height + 10
index += 1
Next
End Sub
This is the code for mouse entry
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
lbl.Show()
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
This is the code for mouse leave
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.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
Thanks!

How to add picture boxes in rows in vb 2010

I am not sure how to create picture boxes on multiple rows in vb 2010. At the moment, I am only able to create them on
one row and one picture box on the second row. (Eventually I would like to add in 5 rows with 10 pictureboxes on each row) I have used the follow code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = 20
Dim yPosition As Integer = 40
For i As Integer = 1 To 20
Dim pb As New PictureBox
With pb
If i < 20 Then
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(xPosition, yPosition)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
xPosition += 70
ElseIf i > 10 Then
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(20, 120)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
xPosition += 70
End If
Dim thisSeating As New Seating
With thisSeating
.SeatNumber = i
.PB = pb
.Occupied = False
End With
seatingList.Add(thisSeating)
End With
Next
End Sub
If anyone would be willing to help me or direct me to the correct path, I would be very grateful :)
Thanks in advance
Is this what you're looking for?
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = 20
Dim yPosition As Integer = 40
For i As Integer = 1 To 5
For j As Integer = 1 To 10
Dim pb As New PictureBox
With pb
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(xPosition + (j * 70), yPosition + (i * 100))
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
Dim thisSeating As New Seating
With thisSeating
.SeatNumber = i
.PB = pb
.Occupied = False
End With
seatingList.Add(thisSeating)
End With
Next
Next
End Sub

How can i make a point in a chart equal the value of a textbox?

In my project I have created a chart and I want there to be the same number of points as in my textboxes, how can i do that? this is my code
Public Class Form1
Dim a As Object
Dim b As Object
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Chart1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
Chart1.ChartAreas(0).AxisX.Maximum = 20
Chart1.ChartAreas(0).AxisY.Maximum = 30
Chart1.ChartAreas(0).AxisX.Minimum = -20
Chart1.ChartAreas(0).AxisY.Minimum = -30
Chart1.ChartAreas(0).AxisY.Interval = 5
Chart1.ChartAreas(0).AxisX.Interval = 5
Chart1.ChartAreas(0).AxisX.Crossing = 0
Chart1.ChartAreas(0).AxisY.Crossing = 0
Chart1.ChartAreas(0).AxisX.LineWidth = 2
Chart1.ChartAreas(0).AxisY.LineWidth = 2
Chart1.ChartAreas(0).AxisY.MajorGrid.LineColor = Color.Black
a = New DataPoint(0, 0)
a.Label = "#VALX : #VALY"
a.MarkerStyle = MarkerStyle.Circle
a.MarkerSize = 5
Chart1.Series(0).Points.Add(a)
End Sub
And I want the datapoints to be like
a =(val(textbox1.text),val(textbox2.text))
In the below example I just created a collection of textboxes and looped through them adding the x and y coordinates from the textboxes
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim Xtbs() As TextBox = {TextBox1, TextBox3, TextBox5}
Dim Ytbs() As TextBox = {TextBox2, TextBox4, TextBox6}
For i As Integer = 0 To Xtbs.Length - 1
Dim x As Double = Xtbs(i).Text
Dim y As Double = Ytbs(i).Text
Dim pt = New DataPoint(x, y)
Chart1.Series(0).Points.Add(pt)
Next
End Sub

How Can i target one of my pictureboxes created on runtime? VB.NET

So basically, i have successfully randomized certain pictureboxes in a grid to contain mines, and show that mine, and for testing purposes, those mines are currently showing. What do you think I need to do to be able to say:
If, you click this box and mine = 1 (there's a mine), then you lose.
Else, keep going.
simple enough, but i want to apply this to all boxes, no matter how big the grid is. (ZonesX * Zones Y)
The furthest I have gotten is being about to pop up a MsgBox() when anyone of them is clicked.
This is all created on runtime. Heres my code.
Public Class Form1
Inherits System.Windows.Forms.Form
Dim images(8) As Image 'declares image array
Dim zonesY As Integer = 10
Dim zonesX As Integer = 10
Dim Guy As Object
Dim pbxNewZone As PictureBox = DirectCast(Guy, PictureBox) 'declares pbxNewZone as a picturebox variable
Dim generator As New Random
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
images(0) = Image.FromFile("blank.png")
images(1) = Image.FromFile("1.png")
images(2) = Image.FromFile("2.png")
images(3) = Image.FromFile("3.png")
images(4) = Image.FromFile("4.png")
images(5) = Image.FromFile("5.png")
images(6) = Image.FromFile("clear.png")
images(7) = Image.FromFile("hit.png")
images(8) = Image.FromFile("mine.png")
Dim x As Integer 'declares x as an integer variable
Dim y As Integer 'declares y as an integer variable
Me.SuspendLayout() 'suspends creation of layout
For y = 1 To zonesY 'starts a For loop (1 to zonesY number of loops)
For x = 1 To zonesX 'starts a For loop (1 to zonesX number of loops)
Dim zonesize1 As Integer
Dim zonesize2 As Integer
pbxNewZone = New PictureBox
Dim blockStatus As Integer
Dim allZones As Integer
allZones = zonesX * zonesY
blockStatus = generator.Next(0, allZones)
pbxNewZone.Name = y & ", " & x
If blockStatus < (allZones / 10) Then
pbxNewZone.Tag = True
If pbxNewZone.Tag = True Then
pbxNewZone.Image = images(8)
End If
Else
pbxNewZone.Tag = False
If pbxNewZone.Tag = False Then
pbxNewZone.Image = images(0)
End If
End If
pbxNewZone.Height = 16
pbxNewZone.Width = 16
pbxNewZone.Tag = 0
zonesize1 = pbxNewZone.Height 'sets out all of the boxes on the form.
zonesize2 = pbxNewZone.Width
pbxNewZone.Left = ((x - 1) * zonesize1 + 15)
pbxNewZone.Top = ((y - 1) * zonesize2 + 15)
Me.Controls.Add(pbxNewZone)
' Wire this control up to an appropriate event handler
AddHandler pbxNewZone.Click, AddressOf pbxNewZoneClicked
Next
Next
Me.Height = (pbxNewZone.Height * zonesY + 63) 'sets the height of fmmGame
Me.Width = (pbxNewZone.Width * zonesX + 40) 'sets the width of frmGame
End Sub
Private Sub pbxNewZoneClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim pbTag As Boolean = DirectCast(sender, Boolean)
If pb.Tag = True Then
pb.Image = images(7) 'Hit Image
Else
pb.Image = images(6) 'Clear Image
End If
MsgBox(pb.Tag)
End Sub
End Class
A quick way to do it would be to use the Tag element of the PictureBox to keep track of whether there is a mine or not (or any of your other conditions).
pbxnewzone.Tag = 1 'can assign tags to all of your pictureboxes in a loop or at random if need be
Then, access the Tag property by casting the sender argument of your pbxNewZoneClicked method back to a PictureBox:
Dim pb As PictureBox = DirectCast(sender, PictureBox)
If (pb.Tag = 1) Then
'change game accordingly
End If
Unless I'm missing something, set minePictureBox1.Tag = true then check it on the click event. Or you could set it to 1 or whatever. Tag is an object.
Is that what you're looking for? Just a way to know if it's a mine or not?
I would think one panel for the entire grid would be easier to manage and less resource hungry then a picturebox for each and every cell.
But for your click issue:
Private Sub pbxNewZoneClicked(ByVal sender As Object, ByVal e As EventArgs)
With DirectCast(sender, PictureBox)
MsgBox(.Name)
End With
End Sub