Add TextBoxes Dynamically in VB.NET - 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)

Related

Adding multiple pictureboxes to a form programmatically in vb.net ?

i have to add pictueboxes in to a panel as per my requirement .
"Adding multiple pictureboxes to a form programmatically in vb.net " In this question PictureBox are drawn in random but i want it in a synchronous way
enter code here
Dim i As String = ListBox1.Items.Count
For j As Integer = 0 To i
Dim PicBox As New PictureBox
PicBox.Width = 40
PicBox.Top = 25
PicBox.Left = j + 15
PicBox.SizeMode = PictureBoxSizeMode.StretchImage
PicBox.BorderStyle = BorderStyle.FixedSingle
Me.Panel1.Controls.Add(PicBox)
Next
i want to use counter which automatically check the value of i ?
Any idea or suggestion ?
Thank you
How about something like this:
Private Sub PicBoxTestButton_Click(sender As System.Object, e As System.EventArgs) Handles PicBoxTestButton.Click
Try
Dim numberOfPics As Integer = ListBox1.Items.Count
Dim lastLeft As Integer = 15
Const spacer As Integer = 5
For parser As Integer = 0 To numberOfPics
Dim PicBox As New PictureBox
PicBox.Width = 40
PicBox.Top = 25
PicBox.Left = lastLeft
lastLeft = PicBox.Width + PicBox.Left + spacer
PicBox.SizeMode = PictureBoxSizeMode.StretchImage
PicBox.BorderStyle = BorderStyle.FixedSingle
Me.Panel2.Controls.Add(PicBox)
Next
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub

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!

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

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