I am creating a grid of textboxes inside a groupbox in vb.net. they are all of a uniform width,height, font etc.. the code I currently have is this:
Dim new_cell As New TextBox
With new_cell
.Multiline = True
.Width = cell_width
.Height = cell_height
.Font = ("arial", 12)
End With
For j = 0 To N
For i = 0 To M
new_cell.Left = (i * cell_width) + i
new_cell.Top = (j * cell_width) + j
space.Controls.Add(new_cell)
Next
Next
where M,N are the grid size and space is the groupbox.
this was supposed to create the whole grid however it only creates one textbox at the bottom corner. This is because the changes to new_cell on the next iteration of the loop affect the previous control and so a new control is never added. I could replace the single textbox with an array and then loop through and add each element of the textbox array to the groupbox. this however creates ugly code and seems inefficient( what with the repeated properties for each cell) and so I was wondering whether there was a way to add a control to a groupbox and then disassociate (or something) the textbox variable and the textbox added to the groupbox.
You create only one TextBox. You need to move Dim new_cell As New TextBox inside the i loop.
For j = 0 To N
For i = 0 To M
Dim new_cell As New TextBox
With new_cell
.Multiline = True
.Width = cell_width
.Height = cell_height
.Font = ("arial", 12)
End With
new_cell.Left = (i * cell_width) + i
new_cell.Top = (j * cell_width) + j
space.Controls.Add(new_cell)
Next
Next
Or even better:
Dim cell_x As Integer = space.DisplayRectangle.Left
Dim cell_y As Integer = space.DisplayRectangle.Top
Dim cell_i As Integer = 0 ': Cell `i` spacing
Dim cell_j As Integer = 0 ': Cell `j` spacing
For j = 0 To N
For i = 0 To M
space.Controls.Add(New TextBox() With {
.Name = String.Format("N{0}M{1}", j, i),
.Multiline = True,
.Width = cell_width,
.Height = cell_height,
.Font = New Font("arial", 12),
.Left = (cell_x + (i * (cell_width + cell_i))),
.Top = (cell_y + (j * (cell_height + cell_j)))
})
Next
Next
Related
I have a row of PictureBoxes created at run time which occupy more of the form's visible width. I want them to scroll at certain intervals so the user sees all of them if he waits.
I believe I must code to a Timer.
But what is the code for that? (The Form is set scrollable, but I do not want the user to interact with it. Just click the PB he likes)
Code for PB creation
'' In Form_Load:
Dim allSeries As IEnumerable(Of String) =
Directory.EnumerateFiles(root, "*.jpg", SearchOption.AllDirectories)
For i = 0 To allSeries.Count - 1
pb(i) = New PictureBox With {
.Name = "pb" + i.ToString,
.BackColor = Color.Transparent,
.Size = New Point(250, 300),
.BorderStyle = BorderStyle.None,
.SizeMode = PictureBoxSizeMode.Zoom,
.Top = 10,
.Left = pbLeft,
.Cursor = Cursors.Hand,
.Image = Image.FromFile(allSeries(i).ToString), 'Get the Image from the Directory
.Tag = Path.GetDirectoryName(allSeries(i)) 'Store Direcyory path
}
Controls.Add(pb(i))
pbLeft = pbLeft + 300 'position next to previous
'Next
Next
Thanks!
I have a from which contains some PictureBoxes. They can be from one to many.
I create them at run-time depending on the existence of specific folders.
I create them and place them the one next to each other. This means that with a scrollable form I can view all of them easy.
My question is this: How do I position them in "rows"? For a specific form size, there can be 5 labels next to each other and infinite rows of them
How do I achieve this?
My (working) code:
Public allSeries As IEnumerable(Of String) = System.IO.Directory.EnumerateDirectories(root)
For i As Integer = 1 To allSeries.Count
Dim pb As New Windows.Forms.PictureBox With {
.Name = "pb" & i.ToString,
.Size = New Drawing.Size(500, 500),
.Location = New Point(5, 5),
.BorderStyle = BorderStyle.FixedSingle,
.SizeMode = PictureBoxSizeMode.Zoom,
.Image = Image.FromFile(allSeries(i - 1).ToString + "\sImage.jpg"),
.Tag = traveldestination, 'Store Directory path
.Cursor = Cursors.Hand}
Me.Controls.Add(pb)
For i As Integer = 2 To allSeries.Count
With Me
.Controls.Item("pb" + i.ToString).Left = .Controls.Item("pb" + (i - 1).ToString).Left + 520
End With
Next
My (bad) and (not workng) code:
Dim pbsOnForm As Integer = 13 'total PictureBoxes on Form /for this instance
Dim pbsOnRow As Integer = 5 'PictureBoxes that "fit" in a row /for this intance)
For i As Integer = 1 To pbsOnForm
If i <= pbsOnRow Then
Me.Controls.Item("pb" + i.ToString).Top = Me.Controls.Item("pb" + i.ToString).Top
End If
If i > pbsOnRow And i <= 10 Then
Me.Controls.Item("pb" + i.ToString).Top = Me.Controls.Item("pb" + (i - pbsOnRow).ToString).Top
End If
Works, but when the PcrureBoxes will be more than 10, I do not know......
While using the TableLayoutPanel would fulfill most cases for this and is probably the best way to achieve this, here is some code to align the PictureBox's in row / column.
First we want to setup a method to handle the positioning. We need some variables scoped to the Form.
Dim counter As Integer = 0
Dim xPos As Integer = 5
Dim yPos As Integer = 5
Now we use these variables in a method that sets the location.
Private Sub PositionPictureBox(pb As PictureBox, Optional imgPerRow As Integer = 5)
pb.Location = New Point(xPos, yPos)
counter += 1
If counter = imgPerRow Then
counter = 0
xPos = 5
yPos = pb.Location.Y + pb.Height + 5
Else
xPos = pb.Location.X + pb.Width + 5
End If
End Sub
Finally we call the method when the PictureBox is instantiated.
For i As Integer = 1 To allSeries.Count
Dim pb As New Windows.Forms.PictureBox
With pb
.Name = "pb" & i.ToString()
.Size = New Drawing.Size(50, 50)
.Location = New Point(5, 5)
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.Zoom
.Image = Image.FromFile("...")
.Tag = allSeries(i)
.Cursor = Cursors.Hand
End With
PositionPictureBox(pb)
Me.Controls.Add(pb)
Next
I have written VBA code for Dynamically creating textboxes and buttons. The following is the code when I hit an "Add" button on the userform.
Dim oTxtBox As Control
Dim oBrwsBtn As Control
Dim oCaption As Control
Dim oTxtLen As Integer
oTxtLen1 = TextBox1.Width
oTxtBrth1 = TextBox1.Height
oTxtPos1 = TextBox1.Left
oButLen = CommandButton1.Width
oButBrth = CommandButton1.Height
oTxtLen2 = TextBox2.Width
oTxtBrth2 = TextBox2.Height
If i = Empty Then
i = 1
End If
Set oTxtBox = Me.Controls.Add("Forms.TextBox.1")
Set oBrwsBtn = Me.Controls.Add("Forms.CommandButton.1")
Set oCaption = Me.Controls.Add("Forms.TextBox.1")
With oTxtBox
.Left = oTxtPos1
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oTxtLen1
.Height = oTxtBrth1
End With
With oBrwsBtn
.Left = oTxtPos1 + oTxtLen1 + 18
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oButLen
.Height = oButBrth
End With
With oCaption
.Left = oTxtPos1 + oTxtLen1 + 18 + oButLen + 18
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oTxtLen2
.Height = oTxtBrth2
End With
i = i + 1
Q1 Now How to Edit the caption of the browse Button which I create dynamically No method .Caption with oBrWsBtn
And Q2: How to get the value when the focus is changed
For example When I click on 'TextBox1' Object. A variable should assign itself with the name (i. e. var(str) = focus object name)
Thanks in advance
Q1: Why don't you try defining the control type more specifically and see if that permits you to use the .Caption method. Don't understand why it's not working now, but maybe the Control object type doesn't have the .Caption method because not all controls have captions.
Try:
'at the top of the sub:
Dim oBrwsBtn as CommandButton
'then later
Set oBrwsBtn = Me.Controls.Add("Forms.CommandButton.1")
Another option to consider would be to create these buttons and just have them be invisible until needed. But that assumes you don't actually have a good reason to create them dynamically, which you may have.
Q2: Use a control_AfterUpdate sub:
'Dim this wherever appropriate for scope. Could be in the afterupdate sub if that works.
dim TextBox1_Value as string
Private Sub TextBox1_AfterUpdate()
TextBox1_Value = TextBox1.value
End Sub
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
Hie everyone
I have a procedure which puts buttons on a .net windows form using vb.net. It works ok but because I do not know the number of buttons I will be programming because they come from the database I would like a way to lay them out in rows of 10. I have programmed up to 50 which ios 5 rows but the method I am using will not work if there is more than 50. Is there a way do this. I have tried using mod of the number of boxes and it does not work.
Here is the code.
Private Sub AddButtons()
Dim xPos As Integer = 0
Dim yPos As Integer = 0
Dim n As Integer = 1
Dim numberOfBoxes As Integer
numberOfBoxes = txtNumberOfBoxes.Text
numberOfBoxes = numberOfBoxes + 1
' Declare and Initialize one variable
Dim btnArray(numberOfBoxes) As System.Windows.Forms.Button
For i As Integer = 0 To numberOfBoxes
btnArray(i) = New System.Windows.Forms.Button
Next i
While (n < numberOfBoxes)
With (btnArray(n))
.Tag = n + 1 ' Tag of button
.Width = 100 ' Width of button
.Height = 100 ' Height of button
If (n = 11) Then ' Location of second line of buttons:
xPos = 0
yPos = 120
ElseIf (n = 21) Then
xPos = 0
yPos = 240
ElseIf (n = 31) Then
xPos = 0
yPos = 360
ElseIf (n = 41) Then
xPos = 0
yPos = 480
ElseIf (n = 51) Then
xPos = 0
yPos = 600
End If
'If n Mod 10 = 0 Then
' xPos = xPos
' yPos = yPos + 50
'End If
' Location of button:
.Left = xPos
.Top = yPos
' Add buttons to a Panel:
pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
xPos = xPos + .Width ' Left of next button
.Text = (n)
' for Event of click Button
AddHandler .Click, AddressOf Me.ClickButton
n += 1
End With
End While
btnAddButton.Enabled = False ' not need now to this button now
Label1.Visible = True
End Sub
I would suggest using a FlowLayoutPanel or, more likely, a TableLayoutPanel. You configure it to grow in the manner required and then all you have to do in code is Add the Button to its Controls collection. It handles all the layout so there's no calculation required.
Private Sub AddButtons()
Dim numberOfBoxes As Integer = txtNumberOfBoxes.Text
For i As Integer = 0 To numberOfBoxes - 1
'since row is an integer it won't have decimal places
'also \ divides without decimales-
Dim row As Integer = i \ 10
'The modulo operator is your friend. it gives you
'the rest of the devision.
' http://en.wikipedia.org/wiki/Modulo_operation
Dim col As Integer = i Mod 10
Dim newButton = New System.Windows.Forms.Button
With newButton
.Tag = i + 1 ' Tag of button
.Width = 100 ' Width of button
.Height = 100 ' Height of button
.Left = col * .Width
.Top = row * (.Height + 20)
.Text = i + 1
AddHandler .Click, AddressOf Me.ClickButton
End With
pnlButtons.Controls.Add(newButton)
Next i
btnAddButton.Enabled = False ' not need now to this button now
Label1.Visible = True
End Sub