Panel Problem

  • Thread starter Thread starter scorpion53061
  • Start date Start date
S

scorpion53061

I have several dynamically created controls which exist on a form.

When it comes to time to use the panel I add the controls to the panel

e.g. Panel1.Controls.Add(Button1)

and so on and so forth.

Well now I no longer need the panel but I need the controls in the
panel. I tell the panel to clear all controls.

Now my dynamically created controls are not visible on the form
especially noticeable the comboboxes and textboxes. I think they are
still there though. I just cannot see them although I have asked htem to
become visible.

Does anyone have any ideas of how to fix this?
 
scorpion53061 said:
When it comes to time to use the panel I add the controls to the panel

e.g. Panel1.Controls.Add(Button1)

and so on and so forth.

Well now I no longer need the panel but I need the controls in the
panel. I tell the panel to clear all controls.

Now my dynamically created controls are not visible on the form
especially noticeable the comboboxes and textboxes. I think they are
still there though. I just cannot see them although I have asked htem to
become visible.

You will have to remove the controls from the panel's 'Controls' collection
and add them to the form's 'Controls' collection prior to removing the
panel.
 
Thank you for responding Herfried. I almost am thinking this is a bug.

These controls are members of hash tables which I cast as controls.

I am looping through removing the controls from the panel and adding
them to the form collection. In this case I get an index out of range.
DefInstance1 is the form instance I am addressing. If I comment out the
remove from panel it works and the controls are in the form collection
but they are not visible though I ask them to be.

First the code then the error that follows:

Sub HideControls()

For Each de As DictionaryEntry In ClassPanel.PanelIn
For int As Integer = (DirectCast(de.Value,
Panel).Controls.Count - 1) To 0 Step -1
DirectCast(de.Value,
Panel).Controls.Remove(DirectCast(de.Value, Panel).Controls.Item(int))
Definstance1.Controls.Add(DirectCast(de.Value,
Panel).Controls(int))
DirectCast(de.Value, Panel).Visible = False

Next
DirectCast(de.Value, Panel).Visible = False

Next

End Sub


System.ArgumentOutOfRangeException: Specified argument was out of the
range of valid values.
Parameter name: Index 33 is out of range.
at System.Windows.Forms.ControlCollection.get_Item(Int32 index)
at Myapp.Module1.HideControls() in Mypath\Module1.vb:line 190
at Myapp.Form2.LinkFavoritesSearch_LinkClicked(Object sender,
LinkLabelLinkClickedEventArgs e) in Mypath\Form2.vb:line 781
at
System.Windows.Forms.LinkLabel.OnLinkClicked(LinkLabelLinkClickedEventArgs
e)
at System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Label.WndProc(Message& m)
at System.Windows.Forms.LinkLabel.WndProc(Message& msg)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at Myapp.Module1.CustomerLoginProcess() in Mypath\Module1.vb:line 482
at Myapp.ClassButton.GenericButton_Click(Object sender, EventArgs e)
in Mypath\ClassButton.vb:line 83
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Myapp.Module1.Main() in Mypath\Module1.vb:line 118
 
Lets take a closer look at your loop to see what you're asking.

we'll simplify the code and then substitute for known values to make it
clearer.
We'll assume your ControlCollection has 4 controls(although judging by
you're error it has 34).

'Simplified version of your loop
For Index As Integer = 3 to 0 Step -1
ControlCollection.Remove.Item(Index)
OtherCollection.Add(ControlCollection.Item(Index))
Next

now we'll step through and see what happens.

first iteration
ControlCollection.Remove.Item(3)
'So far so good.
OtherCollection.Add(ControlCollection.Item(3))
'Whoops! What happened there? [Index out of range.]
'ControlCollection no longer contains Item(3) because
'we removed it in the previous step.
never reach next iteration

How do we fix it?

For Index As Integer = 0 To 3
OtherCollection.Add(ControlCollection.Item(0))

'note we always remove the first item, all other items will
'be reindexed so the next control will be at index 0.
Next


No need to remove the controls from the Panel since they cannot be in two
places at once so were automatically removed from the Panel before being
added to the form.
 
Hi Mick,

This is what I have. The controls are in fact added to the form I need
them to be. However, when I call them to be visible, they do not appear.
I added a debug.Writeline showing position, and name of the control. All
were present and accounted for.

Thank you for your help!!

For Each de As DictionaryEntry In ClassPanel.PanelIn
For Index As Integer = 0 To DirectCast(de.Value,
Panel).Controls.Count - 1
Definstance1.Controls.Add(DirectCast(de.Value,
Panel).Controls.Item(0))
Next
DirectCast(de.Value, Panel).Visible = False


Next


'making it visible
If databasepath = cd.FavoritesPath Then

DirectCast(ccombo.ComboIn.Item("combo1"), ComboBox).Size = New

System.Drawing.Size(288, 21)

DirectCast(ccombo.ComboIn.Item("combo1"), ComboBox).Location =

New System.Drawing.Point(384, 129)

DirectCast(clabel.LabelIn.Item("lbltwo"), Label).Text =

"Select Category"

DirectCast(ccombo.ComboIn.Item("combo1"), ComboBox).Visible = True


End If


Debug.writeline

lblUpdate
632 32
Customer_Software.Form2, Text:
lblMessageCenter
256 45
Customer_Software.Form2, Text:
Label1
256 0
Customer_Software.Form2, Text:
TaskPane1
0 0
Customer_Software.Form2, Text:
Label2
256 40
Customer_Software.Form2, Text:
Label19
656 0
Customer_Software.Form2, Text:
Label20
656 0
Customer_Software.Form2, Text:
combo1
384 129
Customer_Software.Form2, Text:
combo2
264 88
Customer_Software.Form2, Text:
combo3
264 88
Customer_Software.Form2, Text:
combo4
264 88
Customer_Software.Form2, Text:
TextBox1
8 144
Customer_Software.Form2, Text:
TextBox2
8 200
Customer_Software.Form2, Text:
TextBox3
8 256
Customer_Software.Form2, Text:
TextBox4
168 313
Customer_Software.Form2, Text:
TextBox5
168 369
Customer_Software.Form2, Text:
TextBox6
168 425
Customer_Software.Form2, Text:
TextBox7
8 32
Customer_Software.Form2, Text:
TextBox8
8 480
Customer_Software.Form2, Text:
TextBox9
8 528
Customer_Software.Form2, Text:
TextBox10
8 584
Customer_Software.Form2, Text:
TextBox11
8 640
Customer_Software.Form2, Text:
TextBox12
8 696
Customer_Software.Form2, Text:
TextBox13
8 752
Customer_Software.Form2, Text:
lbltwo
280 131
Customer_Software.Form2, Text:
lblthree
8 128
Customer_Software.Form2, Text:
lblfour
8 184
Customer_Software.Form2, Text:
lblfive
8 240
Customer_Software.Form2, Text:
lblsix
8 296
Customer_Software.Form2, Text:
lblseven
168 296
Customer_Software.Form2, Text:
lbleight
8 408
Customer_Software.Form2, Text:
lblnine
168 352
Customer_Software.Form2, Text:
lblten
8 352
Customer_Software.Form2, Text:
lbleleven
168 408
Customer_Software.Form2, Text:
lbltwelve
8 16
Customer_Software.Form2, Text:
lblthirteen
8 464
Customer_Software.Form2, Text:
lblfourteen
8 512
Customer_Software.Form2, Text:
lblfifteen
8 568
Customer_Software.Form2, Text:
lblsixteen
8 624
Customer_Software.Form2, Text:
lblseventeen
8 680
Customer_Software.Form2, Text:
lbleighteen
8 736
Customer_Software.Form2, Text:
"Mick Doherty"
Lets take a closer look at your loop to see what you're asking.

we'll simplify the code and then substitute for known values to make it
clearer.
We'll assume your ControlCollection has 4 controls(although judging by
you're error it has 34).

'Simplified version of your loop
For Index As Integer = 3 to 0 Step -1
ControlCollection.Remove.Item(Index)
OtherCollection.Add(ControlCollection.Item(Index))
Next

now we'll step through and see what happens.

first iteration
ControlCollection.Remove.Item(3)
'So far so good.
OtherCollection.Add(ControlCollection.Item(3))
'Whoops! What happened there? [Index out of range.]
'ControlCollection no longer contains Item(3) because
'we removed it in the previous step.
never reach next iteration

How do we fix it?

For Index As Integer = 0 To 3
OtherCollection.Add(ControlCollection.Item(0))

'note we always remove the first item, all other items will
'be reindexed so the next control will be at index 0.
Next


No need to remove the controls from the Panel since they cannot be in two

places at once so were automatically removed from the Panel before being

added to the form.
 
For Each de As DictionaryEntry In ClassPanel.PanelIn
For Index As Integer = 0 To DirectCast(de.Value,
Panel).Controls.Count - 1
Definstance1.Controls.Add(DirectCast(de.Value,
Panel).Controls.Item(0))
Next
DirectCast(de.Value, Panel).Visible = False


Next

Tidier version of the above code
\\\
For Each de As DictionaryEntry In ClassPanel.PanelIn
Dim PanelControl As Panel = DirectCast(de.Value, Panel)
For Index As Integer = 0 To PanelControl.Controls.Count-1
DefInstance1.Controls.Add(PanelControl.Controls(0))
Next
PanelControl.Visible = False
Next
///

This looks fine to me.
If the controls were sited on the Panel and were Visible then they will be
sited on the Form and be Visible without the need for extra code to make
them Visible.
I suspect there is an error somewhere else or you are not giving us the
information needed to help you.

If you can recreate your problem in a small project, you can mail it to me
and I'll check it out for you.
 
Nak,

I will prepare a small project and send it to you.
Perhaps during the process I will find where the error is occurring as
well.

"Mick Doherty"
 
I sent you an email about an hour and a half ago. Let me know if you
don't get it ok?

"Mick Doherty"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top