VB.NET Make label show upon mouse enter - vb.net

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!

Related

VB.NET Form runs mouse.leave code when I hover over a control

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

Add TextBoxes Dynamically in VB.NET

I want to create TextBoxes dynamically in tabular format. So far i am successful of creating 10 textboxes in a vertical format. But i want to create 10X10 grid of textboxes. Here is the code. This code runs successfully but creates only 10 textboxes. I know there's a small mistake in code but i am not getting it. Please help
Dim XPos, YPos As Integer
Dim i As Integer = 1
Dim j As Integer = 1
Dim newBox As TextBox
XPos = 20
YPos = 30
For i = 1 To 10
For j = 1 To 10
newBox = New TextBox
newBox.Name = "txtR" & i & "C" & j
newBox.Size = New Drawing.Size(54, 22)
newBox.Location = New Point(XPos, YPos)
newBox.Text = newBox.Name
Me.Controls.Add(newBox)
Next
YPos += 30
Next
Below code will help you
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim XPos, YPos As Integer
Dim i As Integer = 1
Dim j As Integer = 1
Dim newBox As TextBox
XPos = 20
YPos = 30
For i = 1 To 10
XPos = 20
For j = 1 To 10
newBox = New TextBox
newBox.Name = "txtR" & i & "C" & j
newBox.Size = New Drawing.Size(54, 22)
newBox.Location = New Point(XPos, YPos)
newBox.Text = newBox.Name
Me.Controls.Add(newBox)
XPos += newBox.Width + 5
Next
YPos += 30
Next
End Sub
Hello, add tasks EXAMPLE
Here this code to add the textbox in runtime to panel (container_control)
then move the scrollbar of the panel to the current and last textbox will add:
Dim txt As TextBox
Dim newLine As Integer = 50
Dim taskN As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Panel1.AutoScrollPosition = New Point()
txt = New TextBox()
txt.Name = "note1"
'txt.BringToFront()
txt.Location = New System.Drawing.Point(111, newLine)
txt.Size = New System.Drawing.Size(318, 68)
txt.Text = "Task" + taskN.ToString()
txt.Font = New Font("Verdana", 30, FontStyle.Regular)
Panel1.Controls.Add(txt)
Me.Text = Panel1.AutoScrollPosition.ToString()
Label1.Text = txt.Location.ToString()
newLine += 60
taskN += 1
'Me.SuspendLayout()
Panel1.ScrollControlIntoView(txt)

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.

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