Looping through dynamically created controls - vb.net

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 :/
;)

Related

Conditional visibility on MS Access Form - how to write in VBA or Macro

I have some very (very) basic MS Access knowledge. I'm trying to expand a bit into either VBA or macros as I'd like to put in some conditional visibility for my form. Basically, I have a checkbox. If it's checked, I want three or four more fields to pop up. Someone was able to point me to a basic VBA formula of if (this checkbox) = true then, (fieldx).visible = true, else, (fieldx).visibility = false, end if.
But I'm so new to this that I need more help and explanation. I tried putting it in but couldn't get it to work (no error message, just nothing changed at all).
Specific questions:
-Does this formula seem right?
-If I want multiple fields to be visible, can I combine them into one formula or should I create a new "if" statement for all?
-Where do I enter this code? I'm running the Office 365 version. For all I know, I'm not even putting it in the right place.
-How do I determine the field names to replace the (this checkbox) and (fieldx) in the formula? I tried entering the name I title the fields as, but with the spaces in the name I got an error message, and without the spaces nothing happened. Is there a specific naming convention to turn the field names into formula-appropriate titles? Is the name listed somewhere?
-Once I get the formula entered, is there something I have to do to get it to run/take effect? I tried saving, closing and reopening with no changes.
-Is this the best way to go about this?
If there's anything else you think I should know, I would love to hear it - but please keep in mind I'm very new to this so if you could keep it at "dummy" or ELI5 levels of explanation, I'd appreciate it!
after creating a form with 4 textboxes and a checkbox put the form in design mode (lower right corner has design mode selected, select a textbox and hit property sheet on the ribbon (or f4).
On the property sheet note the visible property. set the visible property to false. Now the textbox will be invisible when the form starts.
Tip you can select all the textboxes at the same time and set their properties all at once.
Every control on the form and even the various parts of the form have properties you can set and play with. For instance you can give any name you want to any control. On the property sheet go to the other tab and set the name property.
Tip: choose a name you you will remember without having to look it up and describes the controls function.
Next select the checkbox (not the checkbox's label). On the property sheet go to the event tab and select the on click event. hit the ellipsis and choose code builder. Access is Event Driven. We want the textboxes to appear when the checkbox is selected so we put that code in the checkbox click event.
after choosing code builder we get the code window where we can browse among all the events for all our forms. for now all you should see is something like:
Private Sub mycheckbox_Click()
End Sub
So insert some code to handle the checkboxes like:
Private Sub mycheckbox_Click()
If mycheckbox = True Then
txtbox1.Visible = True
txtbox2.Visible = True
txtbox3.Visible = True
txtbox4.Visible = True
Else
txtbox1.Visible = False
txtbox2.Visible = False
txtbox3.Visible = False
txtbox4.Visible = False
End If
End Sub
now when the checkbox is not checked no textboxes are visible.
but when the checkbox is checked they appear

move items to the another listbox which is searched by textbox

I am beginner at programming. I'm trying to build a form based app using visual basic according to a example in youtube.
In the form I cant move a item from listbox1 to listbox2 which was searched in textbox2
When I write the first item which is placed in first row of listbox1 it moves item to listbox2 but if I try with another item it cant move to listbox2.
I would be happy if someone can help me about this case.
Here are a picture of my form and the code I use :
a Lot depends on what you are putting into the listboxes. If it is simple stuff like a, b, c, d etc it will add everything to listbox 2 as you have your code under text_changed event. Try and use a button when user stopped typing to search for the entire word.
Also add your text after the sc Call to your sc sub before the return statement as well -
Sub sc()
''Current code
''If Textbox2<text - remove, already called...
Listbox2.Items.Add(Listbox1.Text)
Listbox1.Items.Remove(Listbox1.SelectedIndex)
Return
End Sub

Issue with a vb.net panel control loop

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

Visual Basic: How to replace selected NumericUpDowns with ComboBoxes

Is it possible to replace the selected NumericUpDown controls with ComboBoxes?
I understand that not all properties can be conserved but the only properties I need are the location and the size.
The workflow I have in mind is as follows:
Select certain NumericUpDowns
Click replace with... and then select ComboBoxes (or any other approach)
Where the NumericUpDowns were, there are now ComboBoxes of the same size
The reason I want to do this is that I have to put together a GUI with multiple tabs. Each tab page has a list of Labels with either NumericUpDowns or ControlBoxes next to it. The order of the controls changes per tab. I just want to copy the items on the first tab and paste them on the other tabs. Then per tab I only have to change certain NumericUpDowns into ComboBoxes.
I started with VB yesterday so I might be overlooking something.
The quickest is doing it manually, we cant change your GUI remotely - since you are at an entry level with a language you do not know well, RAD is the best recommendation - this way you can study what it does - just like learning HTML with DreamWeaver's RAD tools.
Since your interested, (I know your new to VB so I'dd make it heaps clear) you do these steps:
a) Open Winforms VS 2008 solution
b) Click the File > Create New Project > WinForms
c) Double click the form and it will show you the forms code
d) Then in the constructor method you will see the line InitializeComponent
e) Right Click on this method call and chose Goto Definition
f) This will show you the code that populates the form with controls
g) Then for each form I'm suggestion you replace all the NumericUpDown's with ComboBoxes in the xyz.Designer.vb file
However I'd really recommend doing it with Visual Studio IDE. Don't be frightened.
Private sub Replace_By_ComboBox(ByVal nud As NumericUpDowns)
'Create new combo box
Dim cbx As New ComboBox
cbx.Left = nud.Left
cbx.To = nud.Top
cbx.Width = nud.Width
cbx.Height = nud.Height
cbx.Visible = True
cbx.Enabled = True
'Add the combo box
nud.Parent.Controls.Add(cbx)
'Remove the NumericUpDowns
nud.Parent.Controls.Remove(nud)
End Sub

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)