.


:




:

































 

 

 

 


. . WithEvents.




.

. :

= , , , .

= , , .

= , , .

, , .

 

= , , , _ = , , , _  
, .
New   Dim as New ._ = New ._._ = New
. . . _. . _._.

 

VBA . Controls , , .. Control, . Image, , , Picture. , .

Image. , , , , , Image.

Dim s As Control, si As Image

Set s = Controls(Controls.Count - 1)

s.Height = s.Height + 10

Set si = s

' Set si=Controls(Controls.Count - 1)

si.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)

, . . .

Dim s As Control

Set s = Controls(Controls.Count - 1)

s.Height = s.Height + 10

s.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)

s.Picture = LoadPicture(ActiveDocument.Path & "\S.jpg")

Image, . Set si=s . BackColor , ( ).

.

. , , , . .

, , Event. , RaiseEvent _.

( ) WithEvents. .

calc .

Public Event vrong (i As Integer)

 

Public Function fact(i as integer) as Integer

Dim r as integer, j as Integer

r=1

For j=2 to i

r= r*j

If r>256 Then

RaiseEvent vrong (j)

fact = r

Exit Sub

Endif

Next

fact=r

End Function

 

: , , , .

Dim WithEvents cl as calc

 

Sub try()

Set cl = New calc

MsgBox cl.fact(5)

MsgBox cl.fact (555)

End Sub

 

Sub cl_vrong(c as Integer)

MsgBox , & c

End Sub

try 120, .

, (.. ). : Class1 Image . (click) image -, Class1. .

class1. im , move : click outform.

Public WithEvents im As Image

Public Event click(str)

Public Event outform(ob)

 

Private Sub Class_Initialize()

Set im = UserForm1.Controls.Add("Forms.Image.1")

im.Width = 10: im.Height = 10

im.Top = Rnd * 400: im.Left = Rnd * 400

End Sub

 

click , .. . 10 click, str, .

Private Sub im_Click()

im.Width = im.Width + 10

RaiseEvent click(str(im.Width))

End Sub

 

move , . , Top 5. outform. class1.

Public Sub move()

If im.Top > UserForm1.Height Then

RaiseEvent outform(Me)

Else

im.Top = im.Top + 5

End If

End Sub

 

, class1 ( ), , WithEvents, ob1. ob1, , . .

Private WithEvents ob1 As Class1

 

Private Sub UserForm_Initialize()

Set ob1 = New Class1

End Sub

 

Private Sub ob1_click(str As Variant)

MsgBox "ob1:" & str

ob1.move

End Sub

 

Private Sub ob1_outform(ob As Variant)

MsgBox ""

End Sub

 

, , , , ? . , , . . , , class1, .

class2, class1. class1. click () outform ( ). . Top ( !).

 

Public WithEvents cls As Class1

 

Private Sub Class_Initialize()

Set cls = New Class1

End Sub

 

Private Sub cls_click(str As Variant)

MsgBox str

End Sub

 

Private Sub cls_outform(ob As Variant)

MsgBox ob.im.Top

ob.im.Top = 0

End Sub

 

, class2, . move.

Public cl As New Collection

 

Private Sub Button1_Click()

For i = 1 To 5

cl.Add New Class2

Next

Do While True

For Each c1 In cl

c1.cls.move

Next

Start = Timer ' Set start time.

Do While Timer < Start + 1

DoEvents ' Yield to other processes.

Loop

Loop

End Sub

 

, .

! : , .. class2. , .





:


: 2016-11-18; !; : 413 |


:

:

, , . , .
==> ...

1680 - | 1511 -


© 2015-2024 lektsii.org - -

: 0.026 .