Issue with a vb.net panel control loop - vb.net

I have a loop in vb.net where I am wanting to display 50 panels, all with the same 3 controls. Only the last control is populated with the 3 controls, why is this?
Dim PanelVerticalPoint As Integer = btDF.Height * 6
For counter = 1 To 50 Step +1
Dim ButtonPanel As Panel = New Panel
With ButtonPanel
ButtonPanel.Location = New Point(0, PanelVerticalPoint)
ButtonPanel.Size = New Size(btDF.Width, btDF.Height)
Me.Controls.Add(ButtonPanel)
ButtonPanel.Controls.Add(btCustomButtonMenu)
ButtonPanel.Controls.Add(btCustomTextBox)
ButtonPanel.Controls.Add(btCustomButton)
End With
PanelVerticalPoint = PanelVerticalPoint + btDF.Height
Next counter

You have to add a new instance of the buttons to each panel. You are adding the same button instance to the panels so each add is really moving the button.

It's not easy to clone a control. It looks like your case might be appropriate for a user control instead. Make the user control in the designer with the buttons and text box, then just create many instances of the user control instead of the panel.
Here's a very similar question with that kind of answer
Clone Winform control

Related

Adding a Control to Beginning of a FlowLayoutPanel

I have a program that grabs product data and adds a custom control for each record to a FlowLayoutPanel.
I would like to add a control to the beginning of the FlowLayoutPanel, as opposed to the end, to appear as the first item.
Does anyone have an idea how this is done? I would like to avoid having to repopulate it every time I add an item to the beginning.
You can use the SetChildIndex method of the FlowLayoutPanel's Control collection:
Dim newButton As New Button With {.Text = flp.Controls.Count.ToString}
flp.SuspendLayout()
flp.Controls.Add(newButton)
flp.Controls.SetChildIndex(newButton, 0)
flp.ResumeLayout()

Looping through dynamically created controls

I have a number of dynamically created buttons (buttons generated at run time), and also a number of dynamically created panels. I want to do something similar to the code below although i know that this code will not work.
For i = 1 to NumberOfButtons
button(i).top = panel(i).top
next
The buttons are named button1, button2 etc. And the panels are also named panel1, panel2 etc.
This is all being written in VB Express 2008.
As you generate your buttons and panels, you need to store them into an array. Then you can access it via index like your have in your code sample. Another option would be to do it via Me.Controls("button1") and similar, but I would consider this bad coding.
For i As Integer = 1 To theTopNumber
Me.Controls("button" & i.ToString).Top = Me.Controls("panel" & i.ToString).Top
Next
Would't this work?
For Each btn As Button In Me.Controls
For Each pnl In Me.Controls
btn.Top = pnl.Top
Next
Next
Please (if you are satisfied) up vote or mark as answer since my mysteriously got banned for no particular reason. It might help to re-enable it :/
;)

Add Icon to context Menu in VB.NET inside a windows forms appliction

I have searched many times here and on Google looking for a solution that did not envolve utilizing someone's class.
This context menu is pops up where the user right clicks inside a dataGridView
When adding the items the VB code is
Dim m As New ContextMenu()
m.MenuItems.Add(New MenuItem("Disassociate *A* Device"))
m.MenuItems.Add(New MenuItem("Purge Device Assosciations"))
Is there no simple way to reference a resource to add an icon to said menuItems?
Pseudo
m.MenuItem(0).Icon.Source = ....
?
Assuming that this is for a Windows Forms application.
Why not use the ContextMenuStrip?
Example:
Dim m As New ContextMenuStrip()
Dim item As New ToolStripMenuItem("Click Me!")
item.Image = My.Resources.image
m.Items.Add(item)
DataGridView1.ContextMenuStrip = m
You will need to set Owner Draw to true and actually draw the menu item yourself
Here is a good detailed example
I use the image propery and assign a system.drawing.image object to it.
You wont be able to do it in one line, you do the add once all the properties of the newmenu is set.

Re-Creating Dynamic Controls

I have a VB.Net WinForm Program.
I dynamically create panels with controls.
Each panel has:
2 Labels
1 DataGridView
1 Button
Everything works fine the first time I create the panels.
Everything gets created, and everything is functional.
If I have to re-create the form, I get rid of the existing panels (and their controls) with this code:
For P = 0 To Panels.Count - 1
For Each PControl In Panels(P).controls
Panels(P).controls.remove(PControl)
Next
Me.Controls.Remove(Panels(P))
Next
Panels.Clear()
DataGrids.Clear()
lblCounts.Clear()
Where:
Panels, DataGrids, & lblCounts are ArrayLists holding controls
When I re-create the panels, I get the panels and all of their controls except Buttons
When I step through the debugger, I see the buttons being removed, and I see them being created, but they don't appear in the panel
Any ideas?
Your question is regarding a button not appearing when you are adding the controls, but you are only showing the removal process, which is flawed.
Make a UserControl that holds your Labels, Grid and Button. Add that to your form. That's what UserControls are for.
Also, when you are done using it, just call:
MyControl.Dispose()
Otherwise, I suspect you are leaking memory. Remove does not destroy the object.
For Each PControl In Panels(P).controls
Panels(P).controls.remove(PControl)
Next
This part may kick you out of your code. The 'For Each' does not like it when its items change during execution. check it with Breakpoints. if is is really a problem , you could do..
lazy method, by just adding .ToList
For Each PControl In Panels(P).controls.ToList
Panels(P).controls.remove(PControl)
Next
similar to:
Dim AllControls as New List(Of control)
AllControls.AddRange(Panels(P).controls)
For Each PControl in AllControls
Panels(P).controls.remove(PControl)
Next
or:
For i as integer = Panels(P).controls.count -1 to 0 step -1
Dim PControl as control = Panels(P).controls(i)
PControl.parent.remove(PControl)
Next
Try this
WHILE Panels(P).controls.count > 0
Panels(P).controls.removeAt(1)

Creating control from 'control array?'

I have an array of picture boxes as so:
Dim pieces(500) As PictureBox
pieces(1) = New PictureBox
With pieces(1)
.CreateControl()
.Visible = True
.BackColor = Color.Red
.Width = 50
.Height = 50
.Left = 50
.Top = 50
End With
The program does not crash or anything, but the picturebox is no where to be seen on the form. How to I make this work correctly?
And is 'Control Array' the correct term for this? or something else?
It won't show up until you add those PictureBoxes to a form.
I suppose you already have a Windows Form, so all you have to do is:
Window.Controls.Add(PictureBox)
Supposing your form object is called "Window"
You need to add them one by one and they don't need to be on an array, that's why there's a Control collection inside the Windows Form
Control Array is a VB 6 term, is not used in .NET anymore. The programming model between .NET and VB 6 is very different, you should take the time to go through a good tutorial or good book.
You need to add it to the form or panel where you want it/them displayed.
CreateControl only creates children and forces the creation of the control's Handle, but it will not place it onto the a form or parent control (it wouldn't know what to add it too!).