Results 1 to 13 of 13

Thread: How do i delete a control?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    I know how to create one. But deleting is harder. Can't find example code on it anywhere. Topic is little known probably.

    Dim MyCmd As Control
    Set MyCmd = Controls.Add("Forms.CommandButton.1")
    MyCmd.left = 18
    MyCmd.top = 50
    MyCmd.width = 175
    MyCmd.height = 20
    MyCmd.Caption = "This iz fun. " & MyCmd.Name

    I've tried
    MyCmd.Delete
    but only get an error 91.
    Anyone know the answer?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I hope i'm not talking bullcrap since i don't have vb6 and i can't create my controls in vb5 but you could try this:

    Set MyCmd = Nothing
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    I do belive that it is

    Code:
    Unload myCmd
    Only works for controls created at runtime.
    Iain, thats with an i by the way!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Still doesnt work. This is VBA in Office97. VB5 more or less.
    The controll is created at runtime as described:

    Dim MyCmd As Control

    Private Sub UserForm_Click()
    Set MyCmd = Controls.Add("Forms.CommandButton.1")
    MyCmd.left = Rnd * 50
    MyCmd.top = Rnd * 50
    MyCmd.width = 20
    MyCmd.height = 20
    MyCmd.Caption = "Fun" & MyCmd.Name
    End Sub

    Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 27 Then Unload MyCmd 'ESCAPE pressed
    End Sub

    When pressing Escape i get "Cannot load or remove object" runtime error 361.

  5. #5
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Sorry that is for unloading control array elements.

    Give this one a whirl

    Code:
    Me.Controls.Remove ("myCmd")
    Iain, thats with an i by the way!

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Originally posted by Thomas Halsvik
    know how to create one. But deleting is harder. Can't find example code on it anywhere. Topic is little known probably.

    Dim MyCmd As Control
    Set MyCmd = Controls.Add("Forms.CommandButton.1","<Control Name>")
    MyCmd.left = 18
    MyCmd.top = 50
    MyCmd.width = 175
    MyCmd.height = 20
    MyCmd.Caption = "This iz fun. " & MyCmd.Name

    I've tried
    MyCmd.Delete
    but only get an error 91.
    Anyone know the answer?
    I think you need to assign a name to the newly created CommandButton, else you can not remove the control with the Romove method.
    Code:
    Controls.Remove "<Control Name>"

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Thanks. That last one worked. Strange how little there is to be found on the net about this subject.

    Well. I'm happy. Now I have a new degree of freedom.

    ----------------------------------------------------
    Question: How do u get lines in the posts?

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb Just the UUB Code

    just read this & you'll know how we did it.

    http://forums.vb-world.net/index.php?action=bbcode

    [Edited by Chris on 06-06-2000 at 02:34 PM]

  9. #9
    Member
    Join Date
    Dec 1999
    Posts
    41
    I pasted the code...It works fine...But I am not able to figure out how to use that Command Button.If I want to handle the Click Event, how do I do that....
    thanks
    Neelesh

  10. #10
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    you have to dim myCmd as a command button and use the WithEvents Keyword.

    Now you can program all of the events.

    Code:
    Dim WithEvents myCmd as CommandButton
    Iain, thats with an i by the way!

  11. #11
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    Then you may need to change this one as well
    Code:
    Set MyCmd = Controls.Add("Forms.CommandButton.1") 
    
    To
    
    Set MyCmd = Controls.Add("vb.CommandButton",<Control Name>)

    [Edited by Chris on 06-06-2000 at 09:56 PM]

  12. #12
    Member
    Join Date
    Dec 1999
    Posts
    41
    Still I am confused...
    Could you please write code for just one event (with all the variables correctly defined)

    thanks
    neel

  13. #13
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Here we go then.

    Declaring and Setting + Code for the click event.

    [code]
    'General Declaractions
    Dim WithEvents myCmd As CommandButton

    Private Sub Form_Load()

    Set MyCmd = Controls.Add("Forms.CommandButton.1","myCmd")

    End Sub

    Private Sub myCmd_Click()

    msgbox "Hello World"

    End Sub
    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width