I'm trying to add pictureboxs to an array so I can reference them later as they are being created programmatically - vb.net

So far all I have is this code...
Dim lineb As New PictureBox
lineb.Size = New Size(24, 24)
lineb.BackColor = Color.FromArgb(0, 192, 192)
lineb.Location = New Drawing.Point(xb, yb)
Controls.Add(lineb)
lineb.Name = "lineb" + CStr(creatorb)
creatorb += 1
This generates a infinite line of pictureboxs as it is inside a timer.tick event. Xb and Yb continuously move and this works. I need to figure out how to add each picturebox to an array or another way to reference them later. Each one gets created and renamed to lineb + 1...2...3...4... etc.

Here is an example of a way to keep track of dynamically created controls. It will be easier than adding every control to an array.
Dim pbox as New PictureBox
With pbox
.location = New Point(xb, yb)
.size = New Size(24, 24)
.Tag = "box1" 'choose a tag that will help identify it later
' and so on
Addhandler pbox.click, AddressOf pbox_click '' to add a click event handler
End With
Me.Controls.add(pbox)
Private Sub pbox_click(sender As Object, e As EventArgs)
Dim the_sender As PictureBox = DirectCast(sender, PictureBox)
Dim reference As String = DirectCast(the_sender.tag, String)
''' do whatever you need to do now that you know which picturebox was clicked.
End Sub
Alternatively you can always loop through your controls to find the one you are looking for:
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is PictureBox Then
If ctrl.tag = "box1" Then '''or whetever you are looking for
''' do your stuff here
End If
End If
Next

Related

How to add infinite components when a button is clicked

I have a social media WinForm. I have a function that basically makes a new picture box when a button is clicked
Public Sub NewPost()
picture as new picturebox
picture.Width = 208
picture.Height = 264
picture.Image = Form2.PictureBox1.Image
picture.Location = New Point(258, 60)
End Sub
The thing is it only generates 1 new picture box because I have to make a new variable each time I want to add a picturebox, and eachtime I have to have a new name. I know my question Is a bit confusing but help would be nice thanks
If you want to trap events for your dynamic PictureBoxes, then you'll have to abandon the WithEvents model and move to using AddHandler.
Here's a quick example where the name of the PictureBox is displayed when it is clicked. Note that I am not setting a Location since they are being added to a FlowLayoutPanel which takes care of the placement for you:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NewPost()
End Sub
Public Sub NewPost()
Dim picture As New PictureBox
picture.Width = 208
picture.Height = 264
picture.BorderStyle = BorderStyle.FixedSingle
' ...etc...
Dim index As Integer = FlowLayoutPanel1.Controls.Count + 1
picture.Name = "pb" & index
AddHandler picture.Click, AddressOf picture_Click
FlowLayoutPanel1.Controls.Add(picture)
End Sub
Private Sub picture_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Debug.Print(pb.Name)
End Sub
End Class
because I have to make a new variable each time
Not necessarily. You just want to keep a reference to the object. That reference doesn't need to be its own variable, it can just as easily be an element in a list. For example, suppose on your form you have a list of PictureBox objects as a class-level member:
Dim pictureBoxes As New List(Of PictureBox)()
Then in your method you can just add to that list:
Public Sub NewPost()
Dim pictureBox As New PictureBox
pictureBox.Width = 208
pictureBox.Height = 264
pictureBox.Image = Form2.PictureBox1.Image
pictureBox.Location = New Point(258, 60)
Me.pictureBoxes.Add(pictureBox)
End Sub
In this case the pictureBox variable is local to the NewPost method and gets re-created each time. But pictureBoxes is a class-level member and keeps track of the growing list of PictureBox objects that you're creating.
You can use a for while loop to create n number of objects
You can use the existing ControlCollection
Public Function NewPost() As String
Dim picture As New PictureBox
'your code
picture.Name = "Pb" & Form2.Controls.OfType(Of PictureBox).Count
Form2.Controls.Add(picture)
Return picture.Name
End Function
then you can retrive it
DirectCast(Form2.Controls(NewPost), PictureBox).Image = Form2.PictureBox1.Image
'OR
DirectCast(Form2.Controls("Pb12"), PictureBox).Image = Form2.PictureBox1.Image

How to manage dynamically created controls in VB.NET?

I just understand how to make controls dynamically in VB.NET (I mean, only part of adding a new one)
But, unlike VB6, it seems hard to handle those dynamic things.
When I click the DONE button, I want to make an array filled with the text of textboxes.
At the same time, I want to make a Delete button that removes the button itself and the textbox in the same line.
Is there any simple method or an sample code for this?
Thank you!
Drop a TableLayoutPanel on your form, called pnlLayout, and also the Add button called btnAdd. Configure TableLayoutPanel to have two columns, adjust column width as needed.
Paste below code into your form:
Public Class Form1
Dim deleteButtons As List(Of Button)
Dim textBoxes As List(Of TextBox)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
deleteButtons = New List(Of Button)
textBoxes = New List(Of TextBox)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim elementCount As Integer = deleteButtons.Count
Dim txt As New TextBox
txt.Width = 100
txt.Height = 20
textBoxes.Add(txt)
Dim btn As New Button
btn.Width = 100
btn.Height = 20
btn.Text = "Delete " & elementCount.ToString
AddHandler btn.Click, AddressOf btnDelete
deleteButtons.Add(btn)
pnlLayout.SetCellPosition(txt, New TableLayoutPanelCellPosition(0, elementCount))
pnlLayout.SetCellPosition(btn, New TableLayoutPanelCellPosition(1, elementCount))
pnlLayout.Controls.Add(btn)
pnlLayout.Controls.Add(txt)
End Sub
Private Sub btnDelete(sender As Object, e As EventArgs)
Dim senderButton As Button = DirectCast(sender, Button)
Dim txt As TextBox = textBoxes(deleteButtons.IndexOf(senderButton))
pnlLayout.Controls.Remove(senderButton)
pnlLayout.Controls.Remove(txt)
End Sub
End Class
By default, it will have no textboxes and no Delete buttons, you can add as many rows of "Textbox + Delete button" as you want. When you press Delete, the row will be removed (and everything shifted to accommodate the empty space).
For the textbox'ex part:
Dim strcol() As String = {TextBox2.Text, TextBox3.Text}
For Each strtxt In strcol
MessageBox.Show(strtxt)
Next
It really depends on your code, but, if you have their name use this to delete the buttons &/ textbox'es:
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
If TypeOf Me.Controls(i) Is TextBox Then
If Me.Controls(i).Name = "TextBox2" Then
Me.Controls.RemoveAt(i)
End If
End If
If TypeOf Me.Controls(i) Is Button Then
If Me.Controls(i).Name = "Button3" Then
Me.Controls.RemoveAt(i)
End If
End If
Next
But it depends on your code...

Remove LineShape from Windows Form, LineShape and ShapeContainer Arrays

I am using the code below to add multiple LineShape controls to a Windows Form. Note the globally declared mLineShapes() and mShapeContainter() arrays (at bottom of code) which store each new LineShape object once it's created.
At present, I have been unsuccessful at removing a given LineShape control from the form (even if I know its array index), and also cannot removing an array element without causing a Nothing for the removed element. Obviously, once I remove the element from these arrays, it requires that all the remaining elements with greater indices are copied to lower values to fill in the Nothing voided element. Given these circumstances, can lists be used instead of the mLineShapes() and mShapeContainer() arrays?
enter code here' create new ShapeContainer
Dim sSCTemp As New ShapeContainer
' add ShapeContainer to Form
sSCTemp.Parent = Me
' create new LineShape
Dim sLSTemp As New LineShape
sLSTemp.BorderColor = Color.Black
sLSTemp.BorderWidth = 2
sLSTemp.Cursor = Cursors.Cross
' add LineShape to ShapeContainer
sLSTemp.Parent = sSCTemp
' set starting and ending coordinates for the line
sLSTemp.StartPoint = New System.Drawing.Point(siSCCount * 20, 60 + siSCCount * 60)
sLSTemp.EndPoint = New System.Drawing.Point(100 + siSCCount * 20, 110 + siSCCount * 60)
' set new LineShape to top of z-order
sLSTemp.BringToFront()
sSCTemp.BringToFront()
' connect ContextMenuStrip to LineShape
sLSTemp.ContextMenuStrip = mLsCtm1
' add new LineShape to arrays
ReDim Preserve mLineShapes(siSCCount)
ReDim Preserve mShapeContainer(siSCCount)
mLineShapes(siSCCount) = sLSTemp
mLineShapes(siSCCount).Name = "LineShape" & siSCCount
mShapeContainer(siSCCount) = sSCTemp
mShapeContainer(siSCCount).Name = "ShapeContainer" & siSCCount
In addition to the above, the endpoints of each
LineShape are selected from the arrays so that they can be moved. An example is below:
Dim siSCId As Integer
Dim myShapeContainer As ShapeContainer
myShapeContainer = CType(sender, ShapeContainer)
Dim myLineShape As LineShape
' get index of the actual ShapeContainer in ShapeContainer array
siSCId = Array.IndexOf(mShapeContainer, sender)
If siSCId > -1 Then
myLineShape = mLineShapes(siSCId)
If MouseIsNearBy(myLineShape.EndPoint) Then
myLineShape.BorderColor = Color.Red
NearLineEndPoint = True
End If
If MouseIsNearBy(myLineShape.EndPoint) = False Then
myLineShape.BorderColor = Color.Black
NearLineEndPoint = False
End If
If (dragStartPoint) Then
myLineShape.StartPoint = New Point(oldStartPoint.X + e.X - oldMouseX, oldStartPoint.Y + e.Y - oldMouseY)
End If
End If
Therefore, If I simply add a new LineShape to the form controls without using the mLineShapes() ans mShapeControl() arrays, how can I modify the above code (which finds the LineShape in the storage arrays) so that the line can be modified? I think that if I click on a LineShape, I can get its name using .sourcecontrol or .parent?
UPDATE 5/9/2019
After right clicking on a control on Form1 and selecting the "Link" command from a ContextMenuStrip, the following method (ctmsconnect) is fired to draw a new LineShape control that the user then drags and drops on to the next control in the workflow. Question is, is the list of LineShapes ("Lines") not needed?
(in Form1 class declarations):
Dim SC As New ShapeContainer
Dim Lines As New List(Of LineShape)
Private Sub ctmsconnect_Click(sender As System.Object, e As System.EventArgs) Handles ctmsconnect.Click
mLineWidth = 1
Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)
Dim x As Integer = cms.SourceControl.Right - 2
Dim y As Integer = cms.SourceControl.Top + (cms.SourceControl.Height / 2 - 12)
Dim LS As New LineShape
NumLineShapes += 1
LS.Name = "LineShape" & NumLineShapes
LS.BorderColor = Color.Black
LS.BorderWidth = 2
'Set starting and ending coordinates for the line
LS.StartPoint = New Point(x, y)
LS.EndPoint = New Point(x + 80, y - 5)
'Set new LineShape to top of z-order
LS.BringToFront()
Dim nxgContextMenuStrip As New ContextMenuStrip
LS.ContextMenuStrip = nxgContextMenuStrip
LS.Tag = "LineShape" & NumLineShapes & "_Delete"
'Attach an event handler for the ContextMenuStrip control's Opening event.
AddHandler nxgContextMenuStrip.Opening, AddressOf cms_Opening
numconnectedlineendpoints += 1
Dim myValues As New List(Of String)
myValues.Add(cms.SourceControl.Name)
DropLineOriginalObjectName = cms.SourceControl.Name
OrigDropControl = cms.SourceControl
myValues.Add(LS.Name)
myValues.Add("linestart")
dicGUIControls.Add(numconnectedlineendpoints, myValues)
Lines.Add(LS)
SC.Shapes.Add(LS)
Me.Refresh()
End Sub
You shouldn't need the arrays, just use the controls collection. Instead of setting the parent of the controls, you should probably add them to the collection:
'sSCTemp.Parent = Me
Me.Controls.Add(sSCTemp)
To remove them, you can reference them by the name property:
If Me.Controls.ContainsKey("ShapeContainer1") Then
Me.Controls.RemoveByKey("ShapeContainer1")
End If
The shape controls inside the ShapeContainer have to be accessed through the Shape collection:
If Me.Controls.ContainsKey("ShapeContainer1") Then
Dim sc As ShapeContainer = DirectCast(Me.Controls("ShapeContainer1"), ShapeContainer)
If sc.Shapes.ContainsKey("LineShape2") Then
sc.Shapes.RemoveAt(sc.Shapes.IndexOfKey("LineShape2"))
End If
End If
Example of reading the StartPoint and EndPoint properties:
Dim sb As New StringBuilder
For Each ls As LineShape In Me.ShapeContainer1.Shapes
sb.AppendLine(ls.StartPoint.ToString & " - " & ls.EndPoint.ToString)
Next
MessageBox.Show(sb.ToString)
Note: ShapeContainer Class makes a special note:
Be careful that you do not create more than one ShapeContainer for each form or container; doing this may introduce unexpected behavior. If you add a design-time line or shape control to a form or container after you write code to create one programmatically, you should modify that code to use the ShapeContainer created by the designer.
Public Class Form1
Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
' Set the form as the parent of the ShapeContainer.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
canvas.Parent = Me
' Set the ShapeContainer as the parent of the LineShape.
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If RadioButton1.Checked = True Then
Dim line1 As New Microsoft.VisualBasic.PowerPacks.LineShape
line1.Parent = canvas
' Set the starting and ending coordinates for the line.
line1.StartPoint = New System.Drawing.Point(Me.Width / 2, 0)
line1.EndPoint = New System.Drawing.Point(e.X, e.Y)
TextBox1.Text = canvas.Shapes.Count.ToString
line1.Name = "MyShape"
canvas.Shapes.Add(line1)
AddHandler line1.Click, AddressOf LineClick
End If
End Sub
Private Sub LineClick(sender As Object, e As EventArgs)
' Here is where we take the object that is sender from the arguments and cast it to its specific control
If RadioButton2.Checked = True Then
' I could just as easily use
CType(sender, PowerPacks.LineShape).Dispose()
TextBox1.Text = canvas.Shapes.Count
End If
End Sub
End Class

Simple VB, handling many buttons

In my VB solution I have a lot of buttons arranged in a grid. When the program is loaded one of the buttons is randomly set to be the right one (You have to click that one to win). Can anybody point my in the right direction? Because there must be a better way than manually coding every single button, and creating an event handler for every single button.
You don't have to give me a working example, just an overall idea of how this is done.
Thanks.
If you want to arrange the buttons in a grid, either use the TableLayoutPanel or add the buttons directly to the form and calculate their positions. The TableLayoutPanel is useful if you want to arrange the buttons automatically when the form resizes, otherwise adding the buttons directly seems easier to me.
Add the buttons to an array defined at form level to make them easily accessible
Public Const NColumns As Integer = 5, NRows As Integer = 4
Private buttons As Button(,) = New Button(NColumns - 1, NRows - 1) {}
You can add the buttons easily in loops
For ix As Integer = 0 To NColumns - 1
For iy As Integer = 0 To NRows - 1
Dim btn = New Button()
btn.Text = String.Format("{0:d2}{1:d2}", ix, iy)
btn.Location = New Point(leftMargin + ix * xDistance,
topMargin + iy * yDistance)
btn.Size = New Size(buttonWidth, buttonHeight)
AddHandler btn.Click, Addressof Button_Clicked
buttons(ix, iy) = btn
Controls.Add(btn)
Next
Next
You can determine the winning button with a random generator. Define it as form member, not as local variable.
Private randomGenrator As System.Random = New System.Random()
Determine the coordinates
Dim xWins = randomGenrator.Next(NColumns) 'Returns a number between 0 and NColumns-1
Dim yWins = randomGenrator.Next(NRows)
The click handler looks like this
Private Sub Button Button_Clicked(sender As Object, e As EventArgs)
If sender = buttons(xWins, yWins) Then
'You win
Else
'You loose
End
End Sub
First, you said you want a grid of buttons, so you have to have a FlowLayoutPanel control in your form in order to let the buttons you want to add, to be arranged automatically.
Second, you have to make use of a for loop, r any kind of look, in order to add the buttons to be added to previously added 'FlowLayoutPanel'.
class Answers
Dim strAnswerText as string
Dim AnswerFlag as Boolean
End Class
Sub LoadForm(byval a_Answers as Answer())
Dim i as Integer = 0
Dim b as Button
For(i=0;i<NUM_OF_BUTTONS;i++)
b = New Button()
b.Text = "Choice -" & i & "- " & a_Answers(i).strAnswerText
b.Tag = a_Answers(i).AnswerFlag
'Supposing that the FlowLayoutPanel control name is fl
AddHandler b.Click, Addressof Clicked
fl.controls.Add(b)
End For
End Sub
Sub Button Clicked(sender as object, e as EventArgs)
if sender.Tag = True
'True answer
else
'Wrong answer
end if
End Sub

How to add new tabs to a TabControl and be able to update controls in them?

I need to be able to programmatically create new tabs on a TabControl, add controls to them, and be able to update the controls in each tab from another function. I already have a function to add tabs to the control, and to add controls to those tabs when they are created, but I'm stuck as to update the controls after they have been created.
EDIT: This is what I have to make the tabs and add the controls:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
TabControl1.TabPages.Add(tabpage)
End Sub
Urgh. I can't seem to get back into the account I used to post this question, so I have to post my follow-up to Tim's question in the comments for the previous answer as a new answer.
Debug.WriteLine(TabControl1.TabPages.Item(2).Controls.Find("textbox1", True).Count) returns 0. The tab and the controls have been created prior.
Ok - I'll give it a shot, but I'm real rusty with WinForms, slightly less rusty with VB.NET. You'll need to locate the control you want to update, and you should be able to do that through the Controls collection of the appropriate container - in this case, most likely a TabPage:
TextBox tb1 = CType(tabpage.Controls.Find("textBox1", false), TextBox)
tb1.Text = "I set the text!"
Syntax might be slightly off, but hopefully this will at least point you in the right direction.
See Control.ControlCollection.Find Method
UPDATED
Hans Passant suggested that this isn't working because you didn't set the Name property (I'm assuming he means the name of the controls, not the tab page). I did a little more reading on the ControlsCollection.Find method, and MSDN says "Searches for controls by their Name property and builds an array of all the controls that match." You (and I) were trying to find the control by the instance name (textbox1, textbox2) - which were the instance names for the two controls, not the control names.
So try this instead:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "(empty)"
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Parent = tabpage
textbox2.Parent = tabpage
textbox1.Location = New Point(10, 10)
textbox2.Location = New Point(10, 30)
textbox1.Name = "textbox1"
textbox2.Name = "textbox2"
TabControl1.TabPages.Add(tabpage)
End Sub
Then you can find the control using:
TextBox tb1 = CType(TabControl1.TabPages.Item(TabControl1.TabPages.Count - 1).Controls.Find("textbox1", True)(0), TextBox)
tb1.text = "Test"
Give that a try and see if it works for you. The key (and what I missed looking at your code last night as it was past my bedtime for me) was there was now way to identify the control in the Find method.
I know this is old, but just an opinion, write a function that creates AND returns the control you want to add to the tabpage. In that case, you will have a reference to the control ready at hand.
For example:
Public Function CreateNewListBoxInsideNewPageTab() As ListBox
Dim newTab As New TabPage()
newTab.Text = "Tab " & TabControl1.TabPages.Count + 1
Dim newLst As New ListBox
newLst.Dock = DockStyle.Fill
newTab.Controls.Add(newLst)
TabControl1.TabPages.Add(newTab)
TabControl1.SelectedTab = newTab
Return newLst
End Function
Now when I call this function, I'll have the new listbox as an object:
Dim newListBox as ListBox = CreateNewListBoxInsideNewPageTab()
newListBox.Items.Add("This is a new listbox item!")
can i add tabcontroll elements after programm started?
For i = 0 To frmMain.cmbZielSpache.Items.Count - 1
Dim cBox = New CheckBox()
cBox.Name = "GEN_" & i
cBox.Location = New Point(offsetX, offsetY)
cBox.Text = frmMain.cmbZielSpache.Items(i)
If frmMain.cmbZielSpache.Items(i) = frmMain.cmbZielSpache.Text Or My.Settings.chkTranslate_normal_alleSprachen = True Then
cBox.Checked = True
End If
offsetX = offsetX + 120
Me.Controls.Add(cBox)
AddHandler cBox.CheckedChanged, AddressOf checkChangedHandler
Next i
Instead of using Me.Controls i wanna add the checkboxes after programm started dynamically.