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
Related
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
I want get value 1 in all line for get path string, and programmatically add cover to flowlayoutpanel.
in Resource/Game List.ini (from drag n drop)
Apex Legends,Resource/Cover/Apex Legends.jpg,Resource/Game Info/Apex Legends.txt
Fortnite,Resource/Cover/Fortnite.jpg,Resource/Game Info/Fortnite.txt
PUBG,Resource/Cover/PUBG.jpg,Resource/Game Info/PUBG.txt
here my code :
Private Sub LabelSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelSetting.Click
FlpAddItem.Controls.Clear()
'I am confused in this part to get value 1 in all line for get path string
'Directory.GetFiles(Path) will be replace with streamreader from lines(i) value 1
Dim Path = '???
Dim ImageX As Image = Nothing
Dim x As Int32 = Directory.GetFiles(Path).Count - 1
Dim Img(x) As PictureBox
Dim ImgText(x) As Label
Dim ImgPanel As Panel
For i = 0 To Directory.GetFiles(Path).Count - 1
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = Directory.GetFiles(Path)(i).Replace(Path, "").Replace(".jpg", "").Replace(".png", "")
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(Directory.GetFiles(Path)(i), True)
Img(i).Image = Image.FromFile(Directory.GetFiles(Path)(i))
ImgText(i).Text = Directory.GetFiles(Path)(i)
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
I suggest creating a class for the game name and path information
Public Class GamePath
Public Property GameName As String
Property Path As String
Public Overrides Function ToString() As String
Return GameName
End Function
End Class
I have overridden ToString, so that the game name will automatically be displayed in a ListBox.
When loading the form, I read this information from the INI-file and set it as data source of a listbox, where you will be able to select a game.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim games =
From line In File.ReadLines(IniFilePath)
Let parts = line.Split(","c)
Select New GamePath With {.GameName = parts(0), .Path = parts(1)}
GameListBox.DataSource = games.ToList()
GameListBox.SelectedIndex = 0 'Select first game
End Sub
Note that it is easier to use File.ReadLines than a StreamReader. You will have to add Imports System.IO to the top of the code. Then we use the LINQ syntax to split each line at the comma and to create the game path information.
The user selects a game in the ListBox and then clicks a button. You can get the file path information from the ListBox like this:
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Then read the files only once and assign the result to a variable
Dim files As String() = Directory.GetFiles(gamePath.Path)
Get the file count
Dim fileCount As Integer = files.Count
The whole Click method:
Private Sub StartGameButton_Click(sender As Object, e As EventArgs) Handles StartGameButton.Click
FlpAddItem.Controls.Clear()
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Dim files As String() = Directory.GetFiles(gamePath.Path)
Dim fileCount As Integer = files.Count
Dim ImageX As Image = Nothing
Dim Img(fileCount) As PictureBox
Dim ImgText(fileCount) As Label
Dim ImgPanel As Panel
For i = 0 To fileCount - 1
Dim filePath = files(i)
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
Some details:
In the For-loop you can get the path of an image file with
Dim filePath = files(i)
You can get the name of the image with
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
This automatically removes the directory name and the extension.
Later on, you don't call Directory.GetFiles again:
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
If you only want to read the file paths into a list, you could write
Dim games =
(From line In File.ReadLines(IniFilePath)
Select line.Split(","c)(1)).ToList()
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
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
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.