Is it possible to select labels with integers? - vb.net

Is it possible to mark Label with an integer?
Dim nr As Integer = 1
Do
Label(nr).text = "something"
nr += 1
Loop until nr = 4
And then it would fill up the labels
EDIT:
I wanted to know if it was possible to create Label arrays.
I found this article nad put together my own method.
label1.Text = "test1"
label2.Text = "test2"
label1.Location = New Point(120, 80)
label2.Location = New Point(140, 20)
Me.Controls.Add(label1)
Me.Controls.Add(label2)
Dim labels() As Label = {label1, label2}
For Each label As Label In labels
label.Text = "new test"
Next

You could use the label name in the control collection of the form the labels are displayed on.
For i as integer = 0 to 49 Step 1
Me.Controls("Label" & i.toString).Text = "Something"
Next

Something like this maybe?
Dim lbl As Label() = New Label(9) {}
Dim nr As Integer = 0
Do
lbl(nr) = New Label()
lbl(nr).Text = "somthing " & nr
nr += 1
Loop While nr <> 4

Related

How to dynamicallty create multiple controls at runtime

I am trying to add multiple labels to a userform at runtime
It's for the player names of a board game; and until the game starts the number of players are not known. I have managed to figure out for myself how to use the dynamic array function to create the list of players. I used a For.....Next loop to add the player names. I thought I could do that to add the labels to the form, but it only adds one. Depending on where the new control type is declared, it either adds the first player only, or the last player
This code produces one label only within the groupbox, the last player
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Players_Num As Integer = InputBox("Enter the number of players")
Dim Players(Players_Num) As String
Dim newText As New Label
For i = 0 To Players_Num - 1
Players(i) = InputBox("Enter player name")
Next
'This piece of code was jsut for me to test that I was successfully using a For...Loop
'to add the players names, and will be deleted later on
For x = 0 To Players_Num - 1
MessageBox.Show(Players(x))
Next
For z = 0 To Players_Num - 1
newText.Name = "txt" & Players(z)
newText.Text = Players(z)
newText.Size = New Size(170, 20)
newText.Location = New Point(12 + 5, 12 + 5)
GroupBox1.Controls.Add(newText)
Next
End Sub
End Class
This one places only the first player
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Players_Num As Integer = InputBox("Enter the number of players")
Dim Players(Players_Num) As String
For i = 0 To Players_Num - 1
Players(i) = InputBox("Enter player name")
Next
'This piece of code was jsut for me to test that I was successfully using a For...Loop
'to add the players names, and will be deleted later on
For x = 0 To Players_Num - 1
MessageBox.Show(Players(x))
Next
For z = 0 To Players_Num - 1
Dim newText As New Label
newText.Name = "txt" & Players(z)
newText.Text = Players(z)
newText.Size = New Size(170, 20)
newText.Location = New Point(12 + 5, 12 + 5)
GroupBox1.Controls.Add(newText)
Next
End Sub
End Class
I've tried this in vs 2015 and 2019 Community
Where is it going wrong?
From the looks of the code, you are correctly creating the controls but their location is the same for all of them, essentially, they are being place one of top of the other, the first is hidden with the second, which is hidden with the third.
The line
newText.Location = New Point(12 + 5, 12 + 5)
needs to be modified to place the labels at different locations.
Perhaps, something like:
newText.Location = New Point(12 + 5, 12 + (z * 25))
This will vertically align the labels with a gap of 25 between them
You are placing them all in the same location
newText.Location = New Point(12 + 5, 12 + 5)
Use your 'z' index to place them at different locations in order to be able to see them
For me it is easier to contain controls in a TableLayoutPanel then add the TLP to what ever control collection, such as a GroupBox This way you can couple a Label with TextBox, for example. Here's an example how you can create controls from a DataTable. In your case you would only need 1 ColumnStyle for labels, I just thought I would show you a good practice for future shortcuts. (I rarely use the designer to place controls)
'Start test data
Dim DtTable As New DataTable
With DtTable
Dim NewDtRow As DataRow = .NewRow
For i As Integer = 0 To 25
Dim DtCol As New DataColumn With {.ColumnName = "Col" & i, .DataType = GetType(String)}
.Columns.Add(DtCol)
NewDtRow(DtCol.ColumnName) = "Test" & i
Next
.Rows.Add(NewDtRow)
End With
'End test data
Dim TLP1 As New TableLayoutPanel With {.Name = "TlpFields"}
With TLP1
.BorderStyle = BorderStyle.Fixed3D
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
.AutoScroll = True
.AutoSize = True
.RowStyles.Clear()
.ColumnStyles.Clear()
.Dock = DockStyle.Fill
.ColumnCount = 2
.ColumnStyles.Add(New ColumnStyle With {.SizeType = SizeType.AutoSize})
End With
For Each DtCol As DataColumn In DtTable.Columns
With TLP1
.RowCount += 1
.RowStyles.Add(New RowStyle With {.SizeType = SizeType.AutoSize})
'create labels
.Controls.Add(New Label With {
.Text = DtCol.ColumnName,
.Anchor = AnchorStyles.Right}, 0, .RowCount)
'create textboxs
Dim TxtBox As New TextBox With {
.Name = "TextBox" & DtCol.ColumnName,
.Size = New Size(170, 20),
.Anchor = AnchorStyles.Left}
'add binding
TxtBox.DataBindings.Add("Text", DtTable, DtCol.ColumnName)
.Controls.Add(TxtBox, 1, .RowCount)
End With
Next
Controls.Add(TLP1)

How to stop an 'Object reference not set to an instance of an object' when creating textboxes at run time in VB

For a project I require the creation of textboxes at run time, to this end I have used this code;
Dim AOP As Integer = GlobalVariables.AOP
Dim tb(11, 11) As TextBox
Dim LocationX As Integer
Dim LocationY As Integer
Dim count As Integer = 2
LocationX = 10
LocationY = 10
tb(1, 0).Name = "txtRoot"
tb(1, 0).Size = New Size(170, 20)
tb(1, 0).Location = New Point(LocationX, LocationY)
tb(1, 0).Visible = False
I am then able to loop it using this For loop;
For i = 1 To AOP
If count > AOP Then
Else
If i = AOP Then
LocationY = LocationY + 10
LocationX = 10
tb(count, 0).Name = "txtXValue" & 0 & "YValue" & count
tb(count, 0).Size = New Size(170, 20)
tb(count, 0).Location = New Point(LocationX, LocationY)
Controls.Add(tb(count, 0))
count = count + 1
i = 1
Else
LocationX = LocationX + 10
tb(count, i).Name = "txtXValue" & i & "YValue" & count
tb(count, i).Size = New Size(170, 20)
tb(count, i).Location = New Point(LocationX, LocationY)
Controls.Add(tb(count, i))
End If
End If
Next
This works in theory, however, when the code reaches the line;
tb(1, 0).Name = "txtRoot"
It returns the error 'Object reference not set to an instance of an object'
I am wondering if there is anyway around this? or if this way of creating textboxes isn't possible? Any help would be appreciated.
You have initialized the array but not added initialized TextBoxes, the array contains currently only Nothing. Also note that arrays are zero based, so the first TextBox is in tb(0, 0).
For i As Int32 = 0 To tb.GetLength(0) - 1
For ii As Int32 = 0 To tb.GetLength(1) - 1
tb(i, ii) = New TextBox()
tb(i, ii).Visible = False
' .... '
Next
Next
Now all are initialized.

Drawing an array of PictureBoxes in vb.net

I'm trying to draw an array of PictureBoxes, for testing I use the same picture for each picturebox.
But instead of showing the picture, it shows the color blue.
I would show you a picture, but I dont have 10 reputation..
Dim teren(120) As PictureBox
Dim x_locatie As Integer = 1, y_locatie As Integer = 0
For i = 0 To 10
x_locatie = 210
For j = 0 To 12
teren(i * j) = New PictureBox()
teren(i * j).Size = New Size(61, 61)
teren(i * j).Name = "x" + i.ToString + "y" + j.ToString
teren(i * j).Location = New Point(x_locatie, y_locatie)
Dim locatie As String = folder + "\harta\test.png"
teren(i * j).ImageLocation = locatie
teren(i * j).Show()
Next
y_locatie += 61
Next
I also tried another method , but same result.
Sub PictureBox1_Paint(sender1 As Object, er As PaintEventArgs)
If myImage IsNot Nothing Then
Dim r As New Rectangle(x, y, xlatime, ylungime)
er.Graphics.DrawImage(myImage, r)
End If
End Sub
Sub deseneaza(ByVal poza As String, ByRef x_perm As Integer, ByRef y_perm As Integer, ByRef lungime As Integer, ByRef latime As Integer)
myImage = Image.FromFile(poza)
x = x_perm
y = y_perm
xlatime = latime
ylungime = lungime
Refresh()
End Sub
'this part of code is in body of another function
Dim x_locatie As Integer = 1, y_locatie As Integer = 0
For i = 0 To 10
x_locatie = 210
For j = 0 To 12
Dim locatie As String = folder + "\harta\test.png"
deseneaza(locatie, x_locatie, y_locatie, 61, 61)
Next
y_locatie += 61
Next
I saw in other threads that their problem solution was something like that Dim teren() As PictureBox {teren1, teren2 , ... , teren n} But the problem in my case is that I need 120 PictureBoxes, and I think that it must be a way to do this without writing 120 pictureboxes.
Please try this...it will generate 16 picture box, size 20x20, in a row. I put it under "FormLoading" event.
Dim Shapes(16) As PictureBox
For i = 1 To 16
Shapes(i) = New PictureBox
With Shapes(i)
.BackColor = SystemColors.Control 'Color.Green
.BackgroundImage = New Bitmap(My.Resources.led_blk)
.BackgroundImageLayout = ImageLayout.Zoom
.Size = New Size(20, 20)
.Visible = True
.Location = New Point( 23 * i, 50)
End With
Me.Controls.Add(Shapes(i))
Next
I think GDI+ will be the way to go. I think you need a custom class that has a rectangle structure as a member with other properties that help you with further logic with the character intersecting with them. Paint should be done in the Paint event of the surface control you are using - PictureBox has the best rendering - IMO.
Public Class Tile
Public Property Bounds As New Rectangle
Public Property IsImpassable As Boolean
'others you think of
End Class
Dim iTop = 325
Dim pBox(48) As PictureBox
Dim pinColor = Color.SkyBlue
Dim leftStart = 50
For j = 0 To 3
For i = 0 To 11
pBox(i) = New PictureBox
'pBox(i).Image = Image.FromFile("\NoTest.bmp")
pBox(i).Visible = True
pBox(i).BackColor = pinColor
pBox(i).Top = iTop + (j * 40)
pBox(i).Width = 20
pBox(i).Height = 20
pBox(i).Left = leftStart + (35 * i)
If i > 9 Then
pBox(i).Left = leftStart + (35 * i) + 15
pBox(i).Width = 25
End If
pBox(i).BringToFront()
pBox(i).SizeMode = PictureBoxSizeMode.StretchImage
Controls.Add(pBox(i))
Next
Next

Overwrite a string from the middle

I am trying to make a function to create textboxes. I need to know if there is a way to replace the end of a string with the new number.
While x <= tbnumberofitems
Dim x As Integer = 0 ' looop count '
Dim y As Integer = 1 ' name count'
Dim label1name As String = "label"
Dim textbox1name As String = "textbox"
While x <= tbnumberofitems
y = y + 1
If x = 0 Then y = 1
Convert.ToString(y)
Dim label1 As New Label
label1.Name = label1name & y
'Code to create label box
Dim textbox1 As New TextBox
textbox1.Name = textbox1name & y
'code to create text box
x = x + 1
End While
This is what I currently have. What I need now is a way to make it where when the loop runs the next time, it changes the name to textbox2 and label2 on 3rd loop label 3 and textbox 3, etc. If that is not clear enough what I am trying to do is make it where the numberofitems, make it 5, creates 5 labels and 5 textboxes through the program.
Basicly Like this ..
For x as Integer = 1 to tbnumberofitems
'Code to create label
Dim lbl As New Label
lbl.Name = "label" & format(x)
lbl.Location = New Point(10, x*20)
Me.Controls.Add(lbl)
'Code to create textbox
Dim tb As New TextBox
tb.Name = "TextBox" & format(x)
tb.Location = New Point(100, x*20)
Me.Controls.Add(tb)
Next

Trying to create text boxes dynammically and remove them

I am using VB.NET vb 2008 . I am trying to create text boxes dynammically and remove them here is the code i have written so far
Private Sub setTextBox()
Dim num As Integer
Dim pos As Integer
num = Len(word)
temp = String.Copy(word)
Dim intcount As Integer
remove()
GuessBox.Visible = True
letters.Visible = True
pos = 0
'To create the dynamic text box and add the controls
For intcount = 0 To num - 1
Txtdynamic = New TextBox
Txtdynamic.Width = 20
Txtdynamic.Visible = True
Txtdynamic.MaxLength = 1
Txtdynamic.Location = New Point(pos + 5, 0)
pos = pos + 30
'set the font size
Txtdynamic.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Txtdynamic.Name = "txtdynamic_" & intcount & "_mycntrl"
Txtdynamic.Enabled = False
Txtdynamic.Text = ""
Panel1.Controls.Add(Txtdynamic)
Next
Panel1.Visible = True
Controls.Add(Panel1)
Controls.Add(GuessBox)
Controls.Add(letters)
letter = ""
letters.Text = ""
hang_lable.Text = ""
tries = 0
End Sub`enter code here`
Function remove()
For Each ctrl In Panel1.Controls
Panel1.Controls.Remove(ctrl)
Next
End Function
I am able to create the textboxes but only a few of them are removed. by using For Each ctrl In Panel1.Controls it doesn't retrieve all the controls and some ae duplicated as well.
Change your remove to
Sub remove()
For i As Integer = Panel1.Controls.Count - 1 To 0 Step -1
Panel1.Controls.Remove(Panel1.Controls(i))
Next i
End Sub
If I am not mistaken you should not change a collection in a loop that you are currently looping, using a for each. The safest ways would be to use the index, in reverse, so that the position is not affected.