Skipping iterations in loops in Visual Basic - vb.net

I am trying to make this loop skip 20 pixels and then draw the next box. I draw the boxes side by side perfectly the way I coded it. But I cannot figure out how to make it skip pixels. I think this is a simple addition but I cannot see it. I have googled other for loops as well, but I cannot pick out what I am missing, and I have read a few books I have as well.
here is what I have:
Public Class Form1
Const CellWidth As Integer = 50
Const cellHeight As Integer = 50
Const xOffset As Integer = -20
Const yOffset As Integer = 25
Private DEFAULT_BACKCOLOR As Color = Color.White
Public Sub DrawBoard()
Dim location As New Point
'---draws the boxes
For row As Integer = 1 To 9
For col As Integer = 1 To 9
location.X = col * (CellWidth + 1) + xOffset
location.Y = row * (cellHeight + 1) + yOffset
Dim lbl As New Label
With lbl
.Name = col.ToString() & row.ToString()
.BorderStyle = BorderStyle.Fixed3D
.Location = location
.Width = CellWidth
.Height = cellHeight
.TextAlign = ContentAlignment.MiddleCenter
.BackColor = DEFAULT_BACKCOLOR
.Font = New Font(.Font, .Font.Style Or FontStyle.Bold)
.Tag = "1"
End With
Me.Controls.Add(lbl)
Next
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DrawBoard()
End Sub
End Class

Related

Access a Label object from a Panel, all within a ToolStripMenuItem

I am trying to move a Label object from a Panel object, in this case I have a parent panel that has Panel and Label objects as child, these are dynamically created. The objective is that when executing from the ToolStripMenuItem, when the Panel object is moved, the Label object is also moved.
I made the following code, but I think I could not move the Label object. From what I understand, what I am doing is generating the variable that is called the same as the Label that I need, but what I need is to refer to the existing object, not the new one. (this is correct?)
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
Dim posX = clickedPanel.Location.X + 120
Dim posY = clickedPanel.Location.Y
Dim namelabel As New Label With {
.Name = "Label" & clickedPanel.Name.Last
}
namelabel.Location = New Point((posX), posY)
End Sub
Could you please guide me?
Note: I Forgot something, in this case, If I move the Panel1, Label1 will move too, if I move Panel2, Label2 will move too, etc, etc.
This is the code, where the label is created inside the panel dynamically, and move together.
Private Sub NavButton15_ElementClick(sender As Object, e As NavElementEventArgs) Handles NavButton15.ElementClick
Dim pos As Int32 = 50
Dim poslabel As Int16 = 26
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Dim counter as Int16 = 1
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\Example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & counter
}
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Panel " & counter,
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & counter
}
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
End Sub
This is the ToolStripMenuItem where move the panel whit the label.
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub

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

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)

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 to compare (sort) values of dynamically created textboxes?

i'm currently doing a program that aims to sort values of dynamically created textboxes. how to do this in vb.net? This is my code for creating the textboxes:
For cnt = 0 To Val(TextBox1.Text) - 1
arrivalbox.Add(New TextBox)
With arrivalbox(cnt)
.Parent = Me
.Left = 0
.Height = 13
.Width = 65
.Top = .Height * cnt + 50
.Visible = True
.Tag = cnt
.Text = ""
.Name = "arrival" & cnt
.Location = New Point(380, 120 + (cnt * 25))
.TextAlign = HorizontalAlignment.Center
.Enabled = False
Me.Controls.Add(New TextBox)
End With
Something like this will loop through all the textboxes and add them to a sortedlist which will sort all the textboxes according to the values.
I'm not sure what you are trying to achieve here. But im guessing once you get these sorted then you can change the position based on their position in the sorted list
Dim listedboxes As SortedList(Of Double, TextBox)
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
listedboxes.add(ctrl.value,ctrl)
End if
Next
EDIT
maybe something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim listedboxes As New SortedList(Of Double, TextBox)
For i As Integer = 0 To TextBox1.Text.Length - 1
Dim tb As New TextBox
With tb
.Parent = Me
.Left = 0
.Height = 13
.Width = 65
.Top = .Height * i + 50
.Visible = True
.Tag = i
.Text = TextBox1.Text.Substring(i, 1)
.Name = "arrival" & i
.TextAlign = HorizontalAlignment.Center
.Enabled = False
Me.Controls.Add(New TextBox)
End With
listedboxes.Add(TextBox1.Text.Substring(i, 1), tb)
Next
Dim j = 0
For Each kvp As KeyValuePair(Of Double, TextBox) In listedboxes
kvp.Value.Location = New Point(380, 120 + (j * 25))
j += 1
Next
End Sub