how to compare (sort) values of dynamically created textboxes? - vb.net

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

Related

Buttons on various panels

I am having headaches with the following, I cannot pass buttons to other panels, my idea is that with the arrangement that I already have of buttons. Pass these created buttons to other panels that I am going to do, can someone help me please.
Dim botones(1, 3) As Button
Dim B As Button
Public Sub crearBotones()
For filas As Integer = 0 To 1
For columnas As Integer = 0 To 3
B = New Button
botones(filas, columnas) = B
With B
.Name = "" & filas.ToString & filas.ToString
.Text = "" & filas.ToString & columnas.ToString
.Left = 100
.Location = New Point(40 + columnas * 70, 5 + filas * 70)
.Height = 25
.Width = 50
Me.Panel1.Controls.Add(botones(filas, columnas))
End With
Next
Next
End Sub
It is showing the buttons on one panel, I am looking for it to show the buttons on both panels.
I mean, the array already makes buttons for me, but I want to put the
buttons on different panels, it's just letting me use it on one panel.
In the panel that is in the for.
Refactor the code just a bit so that your crearBotones() method receives a Panel as a parameter, and only creates the buttons if they have not already been created:
Public Class Form1
Private botones(1, 3) As Button
Public Sub crearBotones(ByVal pnl As Panel)
For filas As Integer = 0 To 1
For columnas As Integer = 0 To 3
If IsNothing(botones(filas, columnas)) Then
Dim B As New Button
With B
.Name = "" & filas.ToString & filas.ToString
.Text = "" & filas.ToString & columnas.ToString
.Left = 100
.Location = New Point(40 + columnas * 70, 5 + filas * 70)
.Height = 25
.Width = 50
End With
botones(filas, columnas) = B
End If
pnl.Controls.Add(botones(filas, columnas))
Next
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
crearBotones(Panel1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
crearBotones(Panel2)
End Sub
End Class

How to retrieve values from dynamically created textboxes in VB?

May i ask how to retrieve values from dynamically created textboxes?
Here's how my program should work.
The program will ask the user how many textboxes should be created. Upon creation, user will input values to these textboxes (burst time textboxes). Then, when a button is clicked, values will be fetched from these textboxes and these will be used to compute for the waiting time and turnaround time which will be displayed in the textboxes waiting time and turnaround time textboxes respectively.
I am working with First Come First Serve Algorithm. Please help me.
This is my code:
Public Class Form6
Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'creates burst time textboxes
GroupBox3.Visible = True
Button1.Visible = True
Dim cnt As Integer
Dim burstbox(15) As TextBox
For cnt = 0 To Val(TextBox1.Text) - 1
burstbox(cnt) = New TextBox
With burstbox(cnt)
.Parent = Me
.Left = 0
.Height = 13
.Width = 80
.Top = .Height * cnt + 50
.Visible = True
.Tag = cnt
.Text = ""
.Name = "burst" & cnt
.Location = New Point(90, 170 + (cnt * 25))
End With
Next cnt
'creates waiting time textboxes
Dim cnt2 As Integer
Dim waitbox(15) As TextBox
For cnt2 = 0 To Val(TextBox1.Text) - 1
waitbox(cnt2) = New TextBox
With waitbox(cnt2)
.Parent = Me
.Left = 0
.Height = 13
.Width = 80
.Top = .Height * cnt2 + 50
.Visible = True
.Tag = cnt2
.Text = ""
.Name = "wait" & cnt2
.Location = New Point(200, 170 + (cnt2 * 25))
.ReadOnly = True
End With
Next cnt2
'creates turnaround time textboxes
Dim cnt3 As Integer
Dim turnaroundbox(15) As TextBox
For cnt3 = 0 To Val(TextBox1.Text) - 1
turnaroundbox(cnt3) = New TextBox
With turnaroundbox(cnt3)
.Parent = Me
.Left = 0
.Height = 13
.Width = 80
.Top = .Height * cnt3 + 50
.Visible = True
.Tag = cnt3
.Text = ""
.Name = "turn" & cnt3
.Location = New Point(310, 170 + (cnt3 * 25))
.ReadOnly = True
End With
Next cnt3
'process labels here
Dim cnt4 As Integer
Dim processlabel(15) As Label
For cnt4 = 0 To Val(TextBox1.Text) - 1
processlabel(cnt4) = New Label
With processlabel(cnt4)
.Parent = Me
.Left = 0
.Height = 13
.Width = 80
.Top = .Height * cnt4 + 50
.Visible = True
.Tag = cnt4
.Text = "P" & cnt4 + 1
.Name = "label" & cnt4
.Location = New Point(30, 170 + (cnt4 * 25))
.ForeColor = Color.DodgerBlue
End With
Next cnt4
End Sub
Thanks in advance!
If you create these controls and assign them to local arrays, you will only be able to access them (later) via form.controls. It might be better to store them in a modular (form-level global) variable. Try this code instead:
Private burstbox As New List(of TextBox)
Public Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'creates burst time textboxes
GroupBox3.Visible = True
Button1.Visible = True
Dim cnt As Integer
For cnt = 0 To Val(TextBox1.Text) - 1
burstbox.add(New TextBox)
With burstbox(cnt)
.Parent = Me
.Left = 0
.Height = 13
.Width = 80
.Top = .Height * cnt + 50
.Visible = True
.Tag = cnt
.Text = ""
.Name = "burst" & cnt
.Location = New Point(90, 170 + (cnt * 25))
End With
Next cnt
'etc
End Sub

Shuffling Text of Button

How can I implement shuffling of text on my button, i.e. text = chr(n+48) to shuffle text on each button.
Dim n As Integer = 0
For i As Integer = 0 To 10
' Initialize one variable
btnArray(i) = New Button
Next i
While n < 10
With btnArray(n)
.Tag = n + 1 ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(n + 48)
FlowLayoutPanel1.Controls.Add(btnArray(n))
AddHandler .Click, AddressOf Me.GenericClickHandler
n = n + 1
End With
End While
I don't know if there is a reason why you are doing that to two steps.
Try something like this:
Private btnArray As New List(Of Button)
For i As Integer = 0 To 10
Dim btn As New Button
With btn
.Tag = i ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(i + 48)
End With
btnArray.Insert(i, btn)
'FlowLayoutPanel1.Controls.Add(btnArray(i)) 'It works also
FlowLayoutPanel1.Controls.Add(btn)
AddHandler .Click, AddressOf Me.GenericClickHandler
Next i
Based on your comments:
Private btnArray As New List(Of Button)
Private btnShuffleArray As New List(Of Button)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
' Initialize one variable
Dim btn As New Button
With btn
.Tag = i ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(i + 48)
btnArray.Insert(i, btn)
' FlowLayoutPanel1.Controls.Add(btnArray(i))
'AddHandler .Click, AddressOf Me.GenericClickHandler
End With
Next i
'Randomize the list
Dim rand As New Random
Dim index As Integer
While btnArray.Count > 0
index = rand.Next(0, btnArray.Count)
btnShuffleArray.Add(btnArray(index))
btnArray.RemoveAt(index)
End While
For i = 0 To 10
FlowLayoutPanel1.Controls.Add(btnShuffleArray(i))
Next
End Sub

Skipping iterations in loops in Visual Basic

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

vb.net disable runtime created buttons

Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.
This is the code i have for that button:
Dim intXX As Integer = 0
Do Until intXX = intX
userAvatar(intXX).Enabled = False
intXX = intXX + 1
Loop
The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?
How i create the buttons are like this:
private sub createButton()
Dim personAvatar As New PictureBox
With personAvatar
.AutoSize = False '> ^
If intX = 7 Then
thePrviousAvatarImg = 0
End If
If intX <= 6 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
ElseIf intX >= 7 And intX <= 14 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
Else
Exit Sub
End If
.Name = "cmd" & nameOfPerson
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BackgroundImage = Image.FromFile(theAvatarDir)
.Tag = nameOfPerson
.BringToFront()
End With
AddHandler personAvatar.Click, AddressOf personAvatar_Click
Me.Controls.Add(personAvatar)
userAvatar(intX) = personAvatar
intX = intX + 1
End With
End Sub
Thanks for your time and help!
David
You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.
So before you create each picture box, declare an array to store them:
Dim MyPictureBoxes() as PictureBox
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.enabled = False
Next
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Public Class Form1
Dim MyPictureBoxes(4) As PictureBox
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim personAvatar As New PictureBox
Dim intX As Integer = 0
While intX < 5
personAvatar = New PictureBox
With personAvatar
.AutoSize = False
.Left = intX * 100
.Top = 100
.Name = "cmd" & intX
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BringToFront()
End With
Me.Controls.Add(personAvatar)
MyPictureBoxes(intX) = personAvatar
intX = intX + 1
End While
End Sub
Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.Enabled = False
Next
End Sub
End Class
Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.