Finding control in a Panel within a panel - vb.net

I have a form in vb.net like so...
There is an outside panel named "pnlResults", Within that panel i have a further 10 panels.
As shown there is label with the text as "name" in each of these panels.
I would like to access these labels through a loop however I have tried the following without success.
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
If ctrl.Name.StartsWith("lblName") Then
'Found the labels
End If
End If
Next
The names of all the labels I want to find start with "lblName", they are then identified individually buy a number from 1 to 10 following "lblname" i.e. "lblName1" etc all the way to 10
I believe this is due to the fact that a panel is its own container thus excluded from the loop of 'me.controls'. How do I get around this problem?

Using recursion
Private Sub findingAcontrol(ByRef panelx As Panel)
For Each Control As Control In panelx.Controls
If TypeOf Control Is Panel Then
Me.findingAcontrol(Control)
Exit Sub
End If
If Control.Name = "Button3" Then
MessageBox.Show(Control.Text)
End If
Next
End Sub
Add a button and call your function. For example:
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
For Each Control As Control In Me.Controls
If TypeOf Control Is Panel Then
Me.findingAcontrol(Control)
Exit Sub
End If
Next
End Sub

Here is another solution. You are correct in thinking that since the inner panels are also containers that your code isn't finding controls within them.
This solution just simply goes one level and once it finds an inner panel, finds the label based on how many inner panels you've gone through.
Update: I had assumed this was a web application. Here is an example of something you could do with a windows form.
Dim i As New Integer
i = 0
For Each ctr As Control In pnlResults.Controls
If TypeOf ctr Is Panel Then
i += 1
Dim lblName As New Label
lblName = ctr.Controls.Find("lblName" + i.ToString(), False)(0)
'Do something
End If
Next
Leaving the code for a web application just in case:
Dim i As New Integer
i = 0
For Each ctr As Control In pnlResults.Controls
If TypeOf ctr Is Panel Then
i += 1
Dim lblName As New Label
lblName = ctr.FindControl("lblName" + i.ToString())
'Do something
End If
Next

Search the panel's control collection for panels only.
For Each pl As Panel In pnlResults.Controls.OfType(Of Panel)()
For i As Integer = 1 To 10
Dim lb As Label = pl.Controls("lblName" & i.ToString) 'is the label
Next
Next

Related

Get the names of the datagridview present in the form

How can I check the number of DataGridViews on a form, and then display present their names in VB.NET?
I have tried this:
For Each dgv As DataGridView In Me.Controls
MsgBox(dgv.Name)
Next
But my guess is that Me.Controls consists of every other form controls except DataGridView?
For Each _control In Me.Controls
If TypeOf _control Is DataGridView Then
MsgBox(_control.Name)
End If
Next
I know this question is posted long time ago. In answer posted by #AadityaDengle You'll get DataGridView controls placed in form but, if DataGridView is nested in some other control (for example Panel, TableLayoutPanel, GroupBox, ...) it will not find it.
You have to search in all controls using "recursive search" way. Below is example :
'there will be stored names(id) of all DataGridView controls
Private allGrids As String = ""
Private Sub getAllGrids()
'loop through all controls on a form
For Each c As Control In Me.Controls
If TypeOf c Is DataGridView Then
'if control is DataGridView, then collect her name(id)
allGrids += c.Name.ToString + ","
Else
'if control isn't type of DataGridView and have child control(s), loop through that control
'(for example Panel, TableLayoutPanel,GroupBox, ...)
If c.HasChildren = True Then getAllGrids(c)
End If
Next
End Sub
Private Sub getAllGrids(cnt As Control)
'loop through all controls on a "container" control
'the search principle is the same like in getAllGrids on a form
For Each c As Control In cnt.Controls
If TypeOf c Is DataGridView Then
'collect DataGridView name(id)
allGrids += c.Name.ToString + ","
Else
'subroutine call hisself again with new control
If c.HasChildren = True Then getAllGrids(c)
End If
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(allGrids)
End Sub
Even though both answers by #AadityaDengle and #nelek are right I will leave my code here that I think it's a little more structured.
Note that this code does work with or without your DataGridView is placed inside a Panel or a GroupBox.
'Loop throught all DataGridViews in your Form
Private Sub FormDGV(ByVal Controlo As Control)
If Controlo.HasChildren Then
If TypeOf Controlo Is DataGridView Then
MessageBox.Show(Controlo.Name)
End If
For Each Control As Control In Controlo.Controls
FormDGV(Control)
Next
End If
End SubĀ“
If your DataGridViews are nested in a Panel or something like that you need to send the specifc Panel through parameter like this -
FormDGV(YourPanel)
But if they are not, you just need to send the form itselt.

On button click fill next picture box

I'm writing a little program in VB and I'm stuck at a point where I want to add a specific image on a button click to a picturebox. The tricky part for me is, that each time I click the button, I want the image (from same location, e.g "C:\Test.jpg") to appear in the next picture box.
I tried to use a variable in the picturebox name and increase it on each click but it kept giving errors (must have used it wrong, obviously).
So to make it more clear:
I click Button1
image from location "C:\Test.jpg" appears in PictureBox1
I click Button1 again
image from location "C:\Test.jpg" appears in PictureBox2
etc.
As you can imagine, I'm not an expert in VB.NET so if you good people have any suggestions, thank you in advance :D
Vahur
Here's another approach using Controls.Find():
Public Class Form1
Private counter As Integer = 0
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
counter = counter + 1
Dim matches() As Control = Me.Controls.Find("PictureBox" & counter, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
Dim pb As PictureBox = DirectCast(matches(0), PictureBox)
Using fs As New System.IO.FileStream("C:\Test.jpg", IO.FileMode.Open)
pb.Image = New Bitmap(Image.FromStream(fs))
End Using
End If
End Sub
End Class
Form level:
Private mPList As New List(of PictureBox)
Private pIndex as Integer = 0
Form Shown:
With mPlist
.Add(PictureBox1)
.Add(PictureBox2)
... continue as needed
Ens With
Button Click
' remove image from last picbox (????)
mPlist(pIndex).Image = Nothing
pIndex += 1
if pIndex > mPlist.Count-1 then pIndex = 0
mPlist(pIndex).Image.FromFile(filename)
The issue, I'm sure, is that you are trying to figure out how to increment a number and get a handle of the picture box to set the picture on. I am going to make a couple assumptions here, in that you are making a card game and you will have a set number of players. You have a couple options:
For the number, you can either set a PRIVATE variable in your form for holding the next number and on each button click, increment it by 1.
Private Player1NextImage as byte = 1
Private Player2NextImage as byte = 1
Private Player3NextImage as byte = 1
Private Player4NextImage as byte = 1
In the button click, for the specific player, you would want to get their picturebox that is next:
You can either keep the list of pictureboxes in a list, as show by #Plutonix, or you can just use a SELECT CASE statement and hardcode the pictureboxes.
Select Case Player1NextImage
Case 1
Player1Card1PictureBox.Image = ...
Case 2
Player1Card2PictureBox.Image = ...
Etc...
'Then increment the number
Player1NextImage += 1
There are multiple ways to do this. Let's say your variable is called Counter, and (assuming) that the parent of your pictureboxes is the main form / control, add this to your button click event:
Counter += 1
Dim Pic As PictureBox = CType(Me.Controls("PictureBox" & Counter.ToString), PictureBox)
If Pic Is Nothing Then
MsgBox("Could not find PictureBox")
Else
Pic.Image = Image.FromFile("C:\Test.jpg")
End If

Retrieving data on dynamic controls

I am using dynamically created controls and need to retrieve information about the control at runtime.
If IsLoaded <> "free" Then
flow_display.Controls.Clear()
For x As Integer = 0 To populate.Count - 1
If populate(x).parentID = 2 Then
Dim NewPicBox As PictureBox = New PictureBox
NewPicBox.Size = New System.Drawing.Size(697, 50)
NewPicBox.ImageLocation = pw_imgLink & populate(x).imageID
AddHandler NewPicBox.Click, AddressOf catWindow
flow_display.Controls.Add(NewPicBox)
End If
Next
IsLoaded = "free"
End If
End Sub
Here I create the control when the user clicks on the appropriate label. Right now the catWindow sub is empty. I need to figure out which button is clicked and figure out its location on the populate list. I have tried a few things and from what I've read from other questions can't seem to find anything the helps. Thanks :)
For finding out which PictureBox is pressed, your catWindow Sub should look like this:
Public Sub catWindow(ByVal sender As Object, ByVal e As EventArgs)
Dim box As PictureBox = TryCast(sender, PictureBox)
If box Is Nothing Then Exit Sub
'Now "box" refers to the PictureBox that was pressed
'...
End Sub
If you want to find it's location in the populate list, you will need to iterate through the list until you find the matching box. You could also pre-empt a property on your PictureBox that isn't doing anything else and use it to store the index. Older forms tools used to have a .Tag property especially for this kind of thing. But really, the need to do this smells like a design flaw to me.
FWIW, I'd rewrite your original sample like this:
If IsLoaded <> "free" Then
flow_display.SuspendLayout()
flow_display.Controls.Clear()
For Each box As PictureBox In populate
.Where(Function(p) p.parentID = 2)
.Select(Function(p) New PictureBox() With {
.Size = New System.Drawing.Size(697, 50),
.ImageLocation pw_imgLink & p.imageID })
AddHandler box.Click, AddressOf catWindow
flow_display.Controls.Add(box)
Next box
flow_display.ResumeLayout()
IsLoaded = "free"
End If

Removing a collection of controls

I have a form (Form1) and it has 30 controls.
When I hit a button, I want to remove those 30 buttons and put other controls on the form.
Now, my problem is that this is to slow.
I have this list with controls I want to delete and I run through them with a For Each.
Private Sub ClearControls()
'removing the controls from Me.Controls
For Each Control As Control In ListToDelete
Me.Controls.Remove(Control)
Next
ListToDelete = New List(Of Control)
End Sub
Now, if you watch the form, you see the controls getting deleted 1 by 1. This action takes about 0.4 seconds (timed with the build-in stopwatch) and that's too long.
Are there any solutions to delete the controls in a faster way or is it only possible to delete the controls 1 by 1?
Maybe an important fact is that everything is connected with a database.
The controls are created by a class I defined myself (TableDrawer) and it creates a rectangle or circle (depends on info from the database).
I add the selfmade controls to the form and when I want to delete them, it takes 0.4 seconds to get other controls on the form - also with information out of my database.
Hopefully this clears some things up and I hope you can help me out... It really has to go a bit faster (I hope to get 0.1s or lower)
Hiding the Panel first seems to make the controls disappear quicker than just clearing the Panel. See this code:
Option Strict On
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Panel1.Visible = False
If Not Panel1.Controls.OfType(Of Button).Any() Then
For x As Integer = 1 To 10
For y As Integer = 1 To 10
Dim btn As New Button()
btn.Size = New Size(45, 45)
btn.Location = New Point((x - 1) * 45, (y - 1) * 45)
btn.Text = (x * y).ToString()
Panel1.Controls.Add(btn)
btn.Visible = True
Next
Next
End If
Panel1.Visible = True
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Panel1.Visible = False
Panel1.Controls.Clear()
Panel1.Visible = True
End Sub
End Class
This code has 2 buttons, and a Panel. Button1 generates 100 buttons, places them on a Panel. Button2 hides the panel before removing them. Perhaps you can experiment with this idea.
It's not the deletion that tends to take the time - it's redrawing the form each time. Try surrounding your deletion code with calls to SuspendLayout and ResumeLayout
Private Sub ClearControls()
'removing the controls from Me.Controls
Me.SuspendLayout()
For Each Control As Control In ListToDelete
Me.Controls.Remove(Control)
Next
Me.ResumeLayout()
ListToDelete = New List(Of Control)
End Sub
Put the controls in a panel container control. Removing the panel container removes all child controls.
kindly never used remove and panel.removeat to remove any controls. It won't be able to delete last control in panel layout.Especially for panel.removeat will return out of index error once delete the last end control in panels. I'm also wondering need to know why this is problem appears?
Stored all the control name in string array, find those controls in panel and delete them, Try below code will help you delete all control in panel for one short. Try using with find and removeBykey function will make your task easier.
Dim ctrllist() as string
Dim counts = 0
For each control in Me.panel1.controls
redim Preserve ctrllist(0 to counts)
ctrllist(counts)=control.name
counts+=1
Next
For counts=Lbound(ctrllist) to Ubound(ctrlllist)
If me.panel1.controls.find(ctrllist(counts),True).Length>0 then
me.panel1.controls.removeBykey(ctrllist(counts))
End If
Next
Hope it will help.
Thanks user1884888! The technique helps me.
If I use Me.ScrollPanelControl.Controls.Clear() then the application goes unresponsive and there's no choice to terminate it from Task Manager but using this technique helps me.
This code is to help some one having the same problem.
While (True)
Dim count = Me.ScrollPanelControl.Controls.Count
If count <= 0 Then
Exit While
End If
Dim firstCtrl = CType(Me.ScrollPanelControl.Controls(0), MyControl)
If Not firstCtrl.IsMoving Then
If Me.ScrollPanelControl.Controls.Find(firstCtrl.Name, True).Length > 0 Then
Me.ScrollPanelControl.Controls.RemoveByKey(firstCtrl.Name)
End If
ElseIf count > 1 Then
firstCtrl = CType(Me.ScrollPanelControl.Controls(1), MyControl)
If Me.ScrollPanelControl.Controls.Find(firstCtrl.Name, True).Length > 0 Then
Me.ScrollPanelControl.Controls.RemoveByKey(firstCtrl.Name)
End If
Else
Exit While
End If
End While

Why are controls missing from Me.Controls()

Hey guys. I must be missing something. I am trying to cycle throught the lables on my form but it would appear that I am missing quite a few labels... I have a total of 69 lables on my form and I only get 5 hits on the msgbox. All controls were placed on design time on the form and not on panels or tabs. Also upon inspecting the me.controls. the count is incorrect as it is missing exactly 64 controls. (The missing lables).
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Label Then
MsgBox(ctl.Name)
End If
Next ctl
Any ideas why they would not show up?
Brad Swindell
The Controls collection is a heirarchy. You are only getting top level controls. If you want to get all controls then you will need to recursively dig into each child controls Control collection.
All controls were placed on design
time on the form and not on panels or
tabs.
Remember that GroupBox is also a control, with it's own Controls property as well.
This function should give you what you want, but my VB.Net is very, very rusty so if it doesn't compile I apologize.
Private Sub PrintAllControlsRecursive(col As Control.ControlCollection, ctrlType As Type)
If col Is Nothing OrElse col.Count = 0 Then
Return
End If
For Each c As Control In col
If c.GetType() = ctrlType Then
MessageBox.Show(c.Name)
End If
If c.HasChildren Then
PrintAllControlsRecursive(c.Controls, ctrlType)
End If
Next
End Sub
Sub PrintAllControls(ByVal ParentCtl As Control)
Dim ctl As Control
MsgBox(ParentCtl.Name + " start", MsgBoxStyle.Exclamation)
For Each ctl In ParentCtl.Controls
MsgBox(ctl.Name)
If ctl.HasChildren = True Then
PrintAllControls(ctl)
End If
Next
MsgBox(ParentCtl.Name + " End", MsgBoxStyle.Information)
End Sub
Flatten.
Just use LINQ and a recursive lambda with selectmany to flatten the hierarchy:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim act As Func(Of Control, IEnumerable(Of Control)) =
Function(ctl) ctl.Controls.Cast(Of Control)().SelectMany(
Function(ctl2) ctl2.Controls.Cast(Of Control)().
Union(act(ctl2))).Union(ctl.Controls.Cast(Of Control))
MsgBox(Join((From c In act(Me).Distinct Order By c.Name
Select c.Name & "--" & c.GetType.ToString).ToArray, vbCrLf))
End Sub
Not barnyard programming at all nor chance of repeated items or obscure bugs...
be sure that you're finding controls AFTER the form has been completelly charged, otherwise if you try to list controls during load process the me.controls.count will be zero.