User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 403,519 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,780 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 245 | Replies: 2 | Solved
Reply
Join Date: Jul 2008
Posts: 2
Reputation: 010Nick is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
010Nick 010Nick is offline Offline
Newbie Poster

Removing User Controls

  #1  
Jul 7th, 2008
Hi I have been writing VB.Net for a few weeks,
it has been going well but I am now stuck.

I have multiple user controls on a form and I need a way to remove a selected one. I was hoping that something like this would do it

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Controls.RemoveByKey("UserControl")
refresh_Form()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Controls.RemoveByKey("UserControl1")
refresh_Form()
End Sub
 

But this only works for Button1, I am guessing this is because each instance is not called. “UserControl” and “UserControl1”. I have added the user controls using code and was wondering if there is anything I could do at the point of adding them, to idenfy them for later on. (i.e give them a name I could use later on)

Sorry if this is a daft question, it’s all fairly new to me.
Many thanks in advance
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2002
Location: West Virginia
Posts: 375
Reputation: waynespangler is on a distinguished road 
Rep Power: 6
Solved Threads: 38
waynespangler waynespangler is offline Offline
Posting Whiz

Re: Removing User Controls

  #2  
Jul 7th, 2008
Try this:
        If Me.Controls.Contains(UserControl1) Then
            Me.Controls.Remove(UserControl1)
        End If
Last edited by waynespangler : Jul 7th, 2008 at 10:09 pm.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: 010Nick is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
010Nick 010Nick is offline Offline
Newbie Poster

Re: Removing User Controls

  #3  
Jul 8th, 2008
Thanks for the reply,

This is what I ended up using.
Live and learn


Public Class Form1
    Public Mycontrol As New UserControl
    Public Mycontrol1 As New UserControl

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Add_controls()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Remove_controls(Me.Mycontrol)
    End Sub
End Class

Module Module1
    Sub Add_controls()
        'add two user controls'
        Form1.Mycontrol.Location = New Point(0, 0)
        Form1.Controls.Add(Form1.Mycontrol)

        Form1.Mycontrol1.Location = New Point(60, 60)
        Form1.Controls.Add(Form1.Mycontrol1)
    End Sub
    Sub Remove_controls(ByVal _Countrol As Control)
        'remove a control'
        If Form1.Controls.Contains(_Countrol) Then
            Form1.Controls.Remove(_Countrol)
        End If
    End Sub
End Module
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 1:10 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC