Results 1 to 10 of 10

Thread: Two Controls, One Event

  1. #1

    Thread Starter
    Fanatic Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    Turkey, down the old sides of Fatih
    Posts
    607

    Two Controls, One Event

    Hi guys! its gaouser again, I was experimenting with the ScriptControl and managed a way to use dim withevents and .move () The scriptcontrol is at form1, the code i posted under is at form2, I .addobject the form2 to script control with members true, But after creating 2 buttons and 2 events, Only last button has event, I cant fix
    I created this code:
    Code:
    Dim WithEvents button As VB.CommandButton
    
    Private Sub button_Click()
    Form1.ScriptControl1.Run (button.name & "_click")
    End Sub
    Sub moveobject(ByVal controlname As String, ByVal x As Integer, ByVal y As Integer)
    
      Dim targetControl As Control
        Set targetControl = Me.Controls(controlname)
        
        ' Check if the control with the given name exists
        If Not targetControl Is Nothing Then
            ' Set the position of the control
            targetControl.Left = x
            targetControl.Top = y
        Else
            ' Control with the given name does not exist
            MsgBox "Control with name " & controlname & " does not exist."
        End If
    End Sub
    Sub addbutton(ByVal name As String)
    
    Set button = Controls.Add("VB.CommandButton", name, Form2)
    button.Visible = True
    End Sub
    
    Private Sub Form_Load()
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,538

    Re: Two Controls, One Event

    Code:
    Dim WithEvents button1 As VB.CommandButton
    Dim WithEvents button2 As VB.CommandButton
    Set button1 = Controls.Add("VB.CommandButton", "btn1", Form2)
    Set button2 = Controls.Add("VB.CommandButton", "btn2", Form2)

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Two Controls, One Event

    Suddenly hungry for chocolate ice cream.

  4. #4

    Thread Starter
    Fanatic Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    Turkey, down the old sides of Fatih
    Posts
    607

    Re: Two Controls, One Event

    uuh, The amount of buttons gonna be created isnt fix since user sets it

  5. #5

    Thread Starter
    Fanatic Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    Turkey, down the old sides of Fatih
    Posts
    607

    Re: Two Controls, One Event

    how I managed to get 2 replys and 131 views

  6. #6
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    338

    Re: Two Controls, One Event

    A WithEvents variable is no different from any other VB variable - if you assign to it twice the old value is erased! Why would you expect it to trigger an event from a button that it isn't pointing to?

    If you define your button control as a control array by setting the Index property, then you can add or remove your buttons as needed dynamically, and they will all share the same event handlers. This is probably what you want.

    Otherwise if you want a single event handler routine for multiple buttons that aren't in a control array, you'd need a collection of classes that each have their own WithEvents variable.
    Last edited by ahenry; Jul 6th, 2023 at 02:50 PM.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Two Controls, One Event

    Quote Originally Posted by gaouser View Post
    how I managed to get 2 replys and 131 views
    Most of the 'views' are bots crawling the forum. It means nothing. Some of the others are people who looked at the question and then got called away by an urgent phone call.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Fanatic Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    Turkey, down the old sides of Fatih
    Posts
    607

    Re: Two Controls, One Event

    "ugrent phone call" is a bit sus?

    got nuthin' to do but to make a new project, hype, then give up

    Check out all my cool stuff btw
    VB: EveryDiscord, a Discord client made fully in VB6 | MSPaint Modifier, a VB6-based MSPaint hooking engine, experimental | OpenIM, a fully VB6 instant messaging service based on TCP/IP connections | Kadooki (Overall the AltWWW project), the WWW re-imagined by me with continuations of HTML3.2's parts. | ClaFeed, A little feed algorithm I have been developing for a Twitter-style system. | CfmOS PC, my project CfmOS ported to VB6 in order to prototype faster, often lags on updates though!

    C: LegacyResource, a little ResHacker ripoff | CfmOS, A mobile OS designed to mimic AOSP (Stock Android) on an ESP32-S3, featuring a full bytecode architecture

  9. #9

  10. #10
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    581

    Re: Two Controls, One Event

    Quote Originally Posted by gaouser View Post
    Quote Originally Posted by Shaggy Hiker View Post
    Most of the 'views' are bots crawling the forum. It means nothing. Some of the others are people who looked at the question and then got called away by an urgent phone call.
    "ugrent phone call" is a bit sus?
    Not gonna ask Shaggy how he knows this?

    The man is GOD!

Tags for this Thread

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