|
-
Jul 3rd, 2023, 04:54 PM
#1
Thread Starter
Fanatic Member
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
-
Jul 3rd, 2023, 05:18 PM
#2
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)
-
Jul 3rd, 2023, 05:32 PM
#3
Re: Two Controls, One Event
Suddenly hungry for chocolate ice cream.
-
Jul 3rd, 2023, 05:33 PM
#4
Thread Starter
Fanatic Member
Re: Two Controls, One Event
uuh, The amount of buttons gonna be created isnt fix since user sets it
-
Jul 4th, 2023, 04:10 AM
#5
Thread Starter
Fanatic Member
Re: Two Controls, One Event
how I managed to get 2 replys and 131 views
-
Jul 6th, 2023, 02:13 PM
#6
Hyperactive Member
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.
-
Jul 6th, 2023, 03:51 PM
#7
Re: Two Controls, One Event
 Originally Posted by gaouser
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
 
-
Jul 8th, 2023, 04:17 PM
#8
Thread Starter
Fanatic Member
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
-
Jul 8th, 2023, 04:52 PM
#9
Re: Two Controls, One Event
You have a firm grasp of the obvious.
Last edited by VanGoghGaming; Oct 13th, 2023 at 04:43 PM.
-
Jul 9th, 2023, 07:40 PM
#10
Re: Two Controls, One Event
 Originally Posted by gaouser
 Originally Posted by Shaggy Hiker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|