Solved

Clear the Runtime-Created Controls from a Panel vb.net

Posted on 2012-03-13
11
430 Views
Last Modified: 2012-03-14
I have a form.  On that form is a Panel called pnlData.
I dynamicly create controls on the panel data during runtime.

When a textbox gets focus, I have a "Popup" panel display a numberpad so the user can enter numbers into the field. (no keyboard)  

Here is a sample of the code that creates the 2nd panel...

   Private Sub crtNumPad(ByVal sender As TextBox)

        Dim pnlData() As Control = Me.Controls.Find("pnlData", True)
        'pnlData(0).SendToBack()

        Dim pnlNumPad As New Panel
        With pnlNumPad
            .Name = "plnNumPad"
            .Location = New System.Drawing.Point(sender.Location.X + 65, sender.Location.Y)
            .Size = New System.Drawing.Size(150, 200)
            .BackColor = Color.AliceBlue
            .Visible = True
            .BringToFront()
        End With

        pnlData(0).Controls.Add(pnlNumPad)
        pnlData(0).Controls.SetChildIndex(pnlNumPad, 0)

        Dim btnTemp1 As New Button
        With btnTemp1
            .Name = "btnTemp1"
            .Location = New System.Drawing.Point(1, 1)
            .Size = New System.Drawing.Size(50, 50)
            .AutoSize = False
            .Text = "1"
            .BringToFront()
        End With
        pnlNumPad.Controls.Add(btnTemp1)
        .....  more code....

End sub

Open in new window


When the user moves off of the field, I have the following subroutine..

    Private Sub txtItemQty_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pnlData() As Control = Me.Controls.Find("pnlData", True)
        For Each ctrl As Control In pnlData(0).Controls
            If ctrl.Name = "pnlNumPad" Then
                pnlData(0).Controls.Remove(ctrl)
                ctrl.Dispose()
            End If
        Next
        pnlData(0).Update()
    End Sub

Open in new window


the pnlNumPad is not removed.  

Any ideas?
0
Comment
Question by:ScottParker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37716904
call refresh() after removing the control
pnlData(0).Refresh()
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37717102
Why are you creating the "number pad" dynamically every time?

Just create it ONCE as a UserControl and store an instance of it in the Forms code.  When you need it add it to your Panel at run-time and remove it as necessary.  No need to dispose of it.

Is the panel added at design-time or run-time?

It appears that you are jumping thru hoops for no reason....
0
 
LVL 3

Author Comment

by:ScottParker
ID: 37717181
nepaluz,
    I tried that and it didn't work.
 

Idle_Mind,
I dont know how to do user controls.  

The main panel pnlData is created at design-time but the controls on it are created during run-time.
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37717512
"I dont know how to do user controls."

Click on Project --> Add User Control, then add your Buttons and Code at DESIGN-Time.  Now re-Build the project and your new UserControl should be at the TOP of your ToolBox where you can drag one onto your form.

"The main panel pnlData is created at design-time ..."

Then you do NOT need to Find() the panel with Controls.Find("pnlData", True).  Just use its name like:

    pnlData.Controls.Clear()
0
 
LVL 3

Author Comment

by:ScottParker
ID: 37719715
Ok I built the User Control...

I am assuming that in the code for my original text boxes, in the Subroutine that I have hooked up to the "Enter" event, I will then set the location and visible property of this ucNumPad that I have already dropped on the screen (design time) and have hidden.  

But how would you suggest that ucNumPad know which textbox it should be updateing?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37719786
You can set a "current" TextBox variable in your form before you display the ucNumPad.  Next, make your UserControl raise a custom event with the entered value.  Finally, declare your ucNumPad as "WithEvents" so the custom event can be trapped with the "Handles" clause.  When the custom event is received use the current TextBox variable to update the correct box.
0
 
LVL 3

Author Comment

by:ScottParker
ID: 37719799
Seem to be having a couple of other issues with the user control.

When I drag the user control from the toolbox and drop it on the form, it looks normal.
Then when i drag it over to pnlData, the user control increases in size.

Also,
 I can not get it to show up.  I suspect that the dynimic control that I put on the plnData at run-time are covering it up.  I thought I moved it on to the plnData and I can see it there at design time, but when I do the following line of code,
pnlData.Controls.SetChildIndex(UcNumPad1, 0)

Open in new window

I get an error saying

ex.Message = "'child' is not a child control of this parent."
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 37719821
You could explicitly add it just to make sure:

    pnlData.Controls.Add(UcNumPad1)
    UcNumPad1.BringToFront()
0
 
LVL 3

Author Comment

by:ScottParker
ID: 37720279
Ok, I got everything to work...  Just 1 issue remaining...

The user control seems to have scaled up when it is on the pnlData and I do not understand why.  

If I drop it on the main form, then it looks like I designed it... But once it moves to the pnlData, it scales larger.  

Any ideas why?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 37720495
It might be a FONT issue.  Make sure the Font is the same for your form/panel/usercontrol.
0
 
LVL 3

Author Closing Comment

by:ScottParker
ID: 37721023
Thank you very much.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question