Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Visual Basic 6 - Utilizzo di variabili nella gestione grafica
Forum - Visual Basic 6 - Utilizzo di variabili nella gestione grafica

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
Berto (Normal User)
Newbie


Messaggi: 14
Iscritto: 30/05/2010

Segnala al moderatore
Postato alle 21:41
Domenica, 30/05/2010
Questo è il primo algoritmo che metto su questo sito quindi vi prego di perdonare eventuali errori e di non arrabbiarvi troppo, sappiate che accetterò le vostre critiche purchè siano costruttive...
Io ho creato un programma in vb6 molto simile a Frogger(un vecchio gioco per pc) che funziona in questo modo:
Codice sorgente - presumibilmente VB.NET

  1. Option Explicit
  2. Dim a As Byte
  3. Dim v As Variant
  4. Private Sub haiperso()
  5. MsgBox "HAHA"
  6. MsgBox "Hai perso..."
  7. Unload Form1
  8.  
  9. End Sub
  10.  
  11.  
  12. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  13. If KeyCode = vbKeyRight Then
  14.     shape1.X1 = shape1.X1 + 50
  15.     shape1.X2 = shape1.X2 + 50
  16.     If shape1.X1 >= 7920 Then
  17.         shape1.X1 = shape1.X1 - 100
  18.         shape1.X2 = shape1.X2 - 100
  19.     End If
  20. End If
  21. If KeyCode = vbKeyDown Then
  22.     shape1.Y1 = shape1.Y1 + 50
  23.     shape1.Y2 = shape1.Y2 + 50
  24.     If shape1.Y1 >= 3360 Then
  25.         shape1.Y1 = shape1.Y1 - 100
  26.         shape1.Y2 = shape1.Y2 - 100
  27.     End If
  28. End If
  29. If KeyCode = vbKeyLeft Then
  30.     shape1.X1 = shape1.X1 - 50
  31.     shape1.X2 = shape1.X2 - 50
  32.     If shape1.X1 <= 600 Then
  33.         MsgBox "Hai vinto!"
  34.         Unload Form1
  35.     End If
  36. End If
  37. If KeyCode = vbKeyUp Then
  38.     shape1.Y1 = shape1.Y1 - 50
  39.     shape1.Y2 = shape1.Y2 - 50
  40.     If shape1.Y1 <= 300 Then
  41.         shape1.Y1 = shape1.Y1 + 100
  42.         shape1.Y2 = shape1.Y2 + 100
  43.     End If
  44. End If
  45. End Sub
  46.  
  47. Private Sub Form_Load()
  48. MsgBox "Buon divertimento!"
  49. End Sub
  50.  
  51. Private Sub Timer3_Timer()
  52. Dim s As Byte
  53. Randomize
  54. s = Val(Int(Rnd * 4))
  55. Select Case s
  56. Case Is = 0
  57.     Line10.Y1 = Line10.Y1 + 170
  58.     Line10.Y2 = Line10.Y2 + 170
  59. Case Is = 1
  60.     Line10.Y1 = Line10.Y1 - 170
  61.     Line10.Y2 = Line10.Y2 - 170
  62. Case Is = 2
  63.     Line10.X1 = Line10.X1 + 170
  64.     Line10.X2 = Line10.X2 + 170
  65. Case Is = 3
  66.     Line10.X1 = Line10.X1 - 170
  67.     Line10.X2 = Line10.X2 - 170
  68. End Select
  69.  
  70. If Line10.Y1 <= 500 Then
  71.     Line10.Y1 = Line10.Y1 + 200
  72.     Line10.Y2 = Line10.Y2 + 200
  73. End If
  74. If Line10.Y1 >= 3360 Then
  75.     Line10.Y1 = Line10.Y1 - 200
  76.     Line10.Y2 = Line10.Y2 - 200
  77. End If
  78. If Line10.X1 <= 2040 Then
  79.     Line10.X1 = Line10.X1 + 200
  80.     Line10.X2 = Line10.X2 + 200
  81. End If
  82. If Line10.X1 >= 3480 Then
  83.     Line10.X1 = Line10.X1 - 200
  84.     Line10.X2 = Line10.X2 - 200
  85. End If
  86.  
  87.  
  88. s = Val(Int(Rnd * 4))
  89. Select Case s
  90. Case Is = 0
  91.     Line9.Y1 = Line9.Y1 + 150
  92.     Line9.Y2 = Line9.Y2 + 150
  93. Case Is = 1
  94.     Line9.Y1 = Line9.Y1 - 150
  95.     Line9.Y2 = Line9.Y2 - 150
  96. Case Is = 2
  97.     Line9.X1 = Line9.X1 + 150
  98.     Line9.X2 = Line9.X2 + 150
  99. Case Is = 3
  100.     Line9.X1 = Line9.X1 - 150
  101.     Line9.X2 = Line9.X2 - 150
  102. End Select
  103.  
  104. If Line9.Y1 <= 500 Then
  105.     Line9.Y1 = Line9.Y1 + 300
  106.     Line9.Y2 = Line9.Y2 + 300
  107. End If
  108. If Line9.Y1 >= 3360 Then
  109.     Line9.Y1 = Line9.Y1 - 300
  110.     Line9.Y2 = Line9.Y2 - 300
  111. End If
  112. If Line9.X1 <= 2040 Then
  113.     Line9.X1 = Line9.X1 + 300
  114.     Line9.X2 = Line9.X2 + 300
  115. End If
  116. If Line9.X1 >= 3480 Then
  117.     Line9.X1 = Line9.X1 - 300
  118.     Line9.X2 = Line9.X2 - 300
  119. End If
  120.  
  121. End Sub
  122.  
  123.  
  124.  
  125. Private Sub Timer1_Timer()
  126. If a = 1 Then
  127. Line6.Y1 = Line6.Y1 + 10
  128. Line6.Y2 = Line6.Y2 + 10
  129.  
  130. Line7.Y1 = Line7.Y1 + 10
  131. Line7.Y2 = Line7.Y2 + 10
  132.  
  133. Line8.Y1 = Line8.Y1 + 10
  134. Line8.Y2 = Line8.Y2 + 10
  135.  
  136. Line16.Y1 = Line16.Y1 + 10
  137. Line16.Y2 = Line16.Y2 + 10
  138.  
  139. Line17.Y1 = Line17.Y1 + 10
  140. Line17.Y2 = Line17.Y2 + 10
  141. If Line6.Y2 >= 2880 Then
  142. a = 2
  143.  
  144. End If
  145. Else
  146. If a = 2 Then
  147. Line6.Y1 = Line6.Y1 - 10
  148. Line6.Y2 = Line6.Y2 - 10
  149.  
  150. Line7.Y2 = Line7.Y2 - 10
  151. Line7.Y1 = Line7.Y1 - 10
  152.  
  153. Line8.Y1 = Line8.Y1 - 10
  154. Line8.Y2 = Line8.Y2 - 10
  155.  
  156. Line16.Y1 = Line16.Y1 - 10
  157. Line16.Y2 = Line16.Y2 - 10
  158.  
  159. Line17.Y1 = Line17.Y1 - 10
  160. Line17.Y2 = Line17.Y2 - 10
  161. If Line6.Y2 <= 960 Then
  162. a = 1
  163. End If
  164. Else
  165. Line6.Y1 = Line6.Y1 + 10
  166. Line6.Y2 = Line6.Y2 + 10
  167.  
  168. Line7.Y1 = Line7.Y1 + 10
  169. Line7.Y2 = Line7.Y2 + 10
  170.  
  171. Line8.Y1 = Line8.Y1 + 10
  172. Line8.Y2 = Line8.Y2 + 10
  173.  
  174. Line16.Y1 = Line16.Y1 + 10
  175. Line16.Y2 = Line16.Y2 + 10
  176.  
  177. Line17.Y1 = Line17.Y1 + 10
  178. Line17.Y2 = Line17.Y2 + 10
  179. a = 2
  180.  
  181. End If
  182. End If
  183.  
  184. End Sub
  185.  
  186. Private Sub Timer2_Timer()
  187. If Line6.X1 > shape1.X2 And Line6.X1 < shape1.X1 Then
  188.     If shape1.Y1 > Line6.Y2 And shape1.Y1 < Line6.Y1 Then
  189. haiperso
  190.     End If
  191. End If
  192. If Line8.X1 > shape1.X2 And Line8.X1 < shape1.X1 Then
  193.     If shape1.Y1 > Line8.Y2 And shape1.Y1 < Line8.Y1 Then
  194. haiperso
  195.     End If
  196. End If
  197. If Line7.X1 > shape1.X2 And Line7.X1 < shape1.X1 Then
  198.     If shape1.Y1 > Line7.Y2 And shape1.Y1 < Line7.Y1 Then
  199. haiperso
  200.     End If
  201. End If
  202.  
  203. If Line9.X1 > shape1.X2 And Line9.X1 < shape1.X1 Then
  204.     If shape1.Y1 > Line9.Y2 And shape1.Y1 < Line9.Y1 Then
  205. haiperso
  206.     End If
  207. End If
  208. If Line10.X1 > shape1.X2 And Line10.X1 < shape1.X1 Then
  209.     If shape1.Y1 > Line10.Y2 And shape1.Y1 < Line10.Y1 Then
  210. haiperso
  211.     End If
  212. End If
  213. If Line11.X1 > shape1.X2 And Line11.X1 < shape1.X1 Then
  214.     If shape1.Y1 > Line11.Y2 And shape1.Y1 < Line11.Y1 Then
  215. haiperso
  216.     End If
  217. End If
  218. If Line12.X1 > shape1.X2 And Line12.X1 < shape1.X1 Then
  219.     If shape1.Y1 > Line12.Y2 And shape1.Y1 < Line12.Y1 Then
  220. haiperso
  221.     End If
  222. End If
  223. If Line13.X1 > shape1.X2 And Line13.X1 < shape1.X1 Then
  224.     If shape1.Y1 > Line13.Y2 And shape1.Y1 < Line13.Y1 Then
  225. haiperso
  226.     End If
  227. End If
  228. If Line14.X1 > shape1.X2 And Line14.X1 < shape1.X1 Then
  229.     If shape1.Y1 > Line14.Y2 And shape1.Y1 < Line14.Y1 Then
  230. haiperso
  231.     End If
  232. End If
  233. If Line15.X1 > shape1.X2 And Line15.X1 < shape1.X1 Then
  234.     If shape1.Y1 > Line15.Y2 And shape1.Y1 < Line15.Y1 Then
  235. haiperso
  236.     End If
  237. End If
  238. If Line16.X1 > shape1.X2 And Line16.X1 < shape1.X1 Then
  239.     If shape1.Y1 > Line16.Y2 And shape1.Y1 < Line16.Y1 Then
  240. haiperso
  241.     End If
  242. End If
  243. If Line17.X1 > shape1.X2 And Line17.X1 < shape1.X1 Then
  244.     If shape1.Y1 > Line17.Y2 And shape1.Y1 < Line17.Y1 Then
  245. haiperso
  246.     End If
  247. End If
  248. End Sub
  249.  
  250. Private Sub Timer4_Timer()
  251. Dim s As Byte
  252. Randomize
  253. s = Val(Int(Rnd * 4))
  254. Select Case s
  255. Case Is = 0
  256.     Line11.Y1 = Line11.Y1 + 50
  257.     Line11.Y2 = Line11.Y2 + 50
  258. Case Is = 1
  259.     Line11.Y1 = Line11.Y1 - 50
  260.     Line11.Y2 = Line11.Y2 - 50
  261. Case Is = 2
  262.     Line11.X1 = Line11.X1 + 50
  263.     Line11.X2 = Line11.X2 + 50
  264. Case Is = 3
  265.     Line11.X1 = Line11.X1 - 50
  266.     Line11.X2 = Line11.X2 - 50
  267. End Select
  268.  
  269. If Line11.Y1 <= 240 Then
  270.     Line11.Y1 = Line11.Y1 + 100
  271.     Line11.Y2 = Line11.Y2 + 100
  272. End If
  273. If Line11.Y2 >= 3200 Then
  274.     Line11.Y1 = Line11.Y1 - 100
  275.     Line11.Y2 = Line11.Y2 - 100
  276. End If
  277. If Line11.X1 <= 600 Then
  278.     Line11.X1 = Line11.X1 + 100
  279.     Line11.X2 = Line11.X2 + 100
  280. End If
  281. If Line11.X2 >= 1920 Then
  282.     Line11.X1 = Line11.X1 - 100
  283.     Line11.X2 = Line11.X2 - 100
  284. End If
  285.  
  286.  
  287. s = Val(Int(Rnd * 4))
  288. Select Case s
  289. Case Is = 0
  290.     Line12.Y1 = Line12.Y1 + 50
  291.     Line12.Y2 = Line12.Y2 + 50
  292. Case Is = 1
  293.     Line12.Y1 = Line12.Y1 - 50
  294.     Line12.Y2 = Line12.Y2 - 50
  295. Case Is = 2
  296.     Line12.X1 = Line12.X1 + 50
  297.     Line12.X2 = Line12.X2 + 50
  298. Case Is = 3
  299.     Line12.X1 = Line12.X1 - 50
  300.     Line12.X2 = Line12.X2 - 50
  301. End Select
  302.  
  303. If Line12.Y1 <= 240 Then
  304.     Line12.Y1 = Line12.Y1 + 100
  305.     Line12.Y2 = Line12.Y2 + 100
  306. End If
  307. If Line12.Y2 >= 3200 Then
  308.     Line12.Y1 = Line12.Y1 - 100
  309.     Line12.Y2 = Line12.Y2 - 100
  310. End If
  311. If Line12.X1 <= 600 Then
  312.     Line12.X1 = Line12.X1 + 100
  313.     Line12.X2 = Line12.X2 + 100
  314. End If
  315. If Line12.X2 >= 1920 Then
  316.     Line12.X1 = Line12.X1 - 100
  317.     Line12.X2 = Line12.X2 - 100
  318. End If
  319. s = Val(Int(Rnd * 4))
  320. Select Case s
  321. Case Is = 0
  322.     Line13.Y1 = Line13.Y1 + 50
  323.     Line13.Y2 = Line13.Y2 + 50
  324. Case Is = 1
  325.     Line13.Y1 = Line13.Y1 - 50
  326.     Line13.Y2 = Line13.Y2 - 50
  327. Case Is = 2
  328.     Line13.X1 = Line13.X1 + 50
  329.     Line13.X2 = Line13.X2 + 50
  330. Case Is = 3
  331.     Line13.X1 = Line13.X1 - 50
  332.     Line13.X2 = Line13.X2 - 50
  333. End Select
  334.  
  335. If Line13.Y1 <= 240 Then
  336.     Line13.Y1 = Line13.Y1 + 100
  337.     Line13.Y2 = Line13.Y2 + 100
  338. End If
  339. If Line13.Y2 >= 3200 Then
  340.     Line13.Y1 = Line13.Y1 - 100
  341.     Line13.Y2 = Line13.Y2 - 100
  342. End If
  343. If Line13.X1 <= 600 Then
  344.     Line13.X1 = Line13.X1 + 100
  345.     Line13.X2 = Line13.X2 + 100
  346. End If
  347. If Line13.X2 >= 1920 Then
  348.     Line13.X1 = Line13.X1 - 100
  349.     Line13.X2 = Line13.X2 - 100
  350. End If
  351. s = Val(Int(Rnd * 4))
  352. Select Case s
  353. Case Is = 0
  354.     Line14.Y1 = Line14.Y1 + 50
  355.     Line14.Y2 = Line14.Y2 + 50
  356. Case Is = 1
  357.     Line14.Y1 = Line14.Y1 - 50
  358.     Line14.Y2 = Line14.Y2 - 50
  359. Case Is = 2
  360.     Line14.X1 = Line14.X1 + 50
  361.     Line14.X2 = Line14.X2 + 50
  362. Case Is = 3
  363.     Line14.X1 = Line14.X1 - 50
  364.     Line14.X2 = Line14.X2 - 50
  365. End Select
  366.  
  367. If Line14.Y1 <= 240 Then
  368.     Line14.Y1 = Line14.Y1 + 100
  369.     Line14.Y2 = Line14.Y2 + 100
  370. End If
  371. If Line14.Y2 >= 3200 Then
  372.     Line14.Y1 = Line14.Y1 - 100
  373.     Line14.Y2 = Line14.Y2 - 100
  374. End If
  375. If Line14.X1 <= 600 Then
  376.     Line14.X1 = Line14.X1 + 100
  377.     Line14.X2 = Line14.X2 + 100
  378. End If
  379. If Line14.X2 >= 1920 Then
  380.     Line14.X1 = Line14.X1 - 100
  381.     Line14.X2 = Line14.X2 - 100
  382. End If
  383. s = Val(Int(Rnd * 4))
  384. Select Case s
  385. Case Is = 0
  386.     Line15.Y1 = Line15.Y1 + 50
  387.     Line15.Y2 = Line15.Y2 + 50
  388. Case Is = 1
  389.     Line15.Y1 = Line15.Y1 - 50
  390.     Line15.Y2 = Line15.Y2 - 50
  391. Case Is = 2
  392.     Line15.X1 = Line15.X1 + 50
  393.     Line15.X2 = Line15.X2 + 50
  394. Case Is = 3
  395.     Line15.X1 = Line15.X1 - 50
  396.     Line15.X2 = Line15.X2 - 50
  397. End Select
  398.  
  399. If Line15.Y1 <= 240 Then
  400.     Line15.Y1 = Line15.Y1 + 100
  401.     Line15.Y2 = Line15.Y2 + 100
  402. End If
  403. If Line15.Y2 >= 3200 Then
  404.     Line15.Y1 = Line15.Y1 - 100
  405.     Line15.Y2 = Line15.Y2 - 100
  406. End If
  407. If Line15.X1 <= 600 Then
  408.     Line15.X1 = Line15.X1 + 100
  409.     Line15.X2 = Line15.X2 + 100
  410. End If
  411. If Line15.X2 >= 1920 Then
  412.     Line15.X1 = Line15.X1 - 100
  413.     Line15.X2 = Line15.X2 - 100
  414. End If
  415. End Sub


Il programma funziona ma non è molto elegante...
Ho provato a migliorarlo con l'utilizzo di variabili e sottoprogrammi e questo è il risultato:
Codice sorgente - presumibilmente VB.NET

  1. Option Explicit
  2. Dim sn As String
  3. Dim lima As Integer
  4. Dim limb As Integer
  5. Dim lims As Integer
  6. Dim limd As Integer
  7. Dim a As Byte
  8. Dim v As Variant
  9. Dim tn As Variant
  10. Private Sub haiperso()
  11. MsgBox "HAHA"
  12. MsgBox "Hai perso..."
  13. Unload Form1
  14.  
  15. End Sub
  16.  
  17. Private Sub Command1_Click()
  18. shape1.X1 = shape1.X1 + 50
  19. shape1.X2 = shape1.X2 + 50
  20. End Sub
  21.  
  22. Private Sub Command2_Click()
  23. shape1.Y1 = shape1.Y1 + 50
  24. shape1.Y2 = shape1.Y2 + 50
  25. End Sub
  26.  
  27. Private Sub Command3_Click()
  28. shape1.X1 = shape1.X1 - 50
  29. shape1.X2 = shape1.X2 - 50
  30. End Sub
  31.  
  32. Private Sub Command4_Click()
  33. shape1.Y1 = shape1.Y1 - 50
  34. shape1.Y2 = shape1.Y2 - 50
  35. End Sub
  36.  
  37. Private Sub Timer1_Timer()
  38. If a = 1 Then
  39. Line6.Y1 = Line6.Y1 + 10
  40. Line6.Y2 = Line6.Y2 + 10
  41.  
  42. Line7.Y1 = Line7.Y1 + 10
  43. Line7.Y2 = Line7.Y2 + 10
  44.  
  45. Line8.Y1 = Line8.Y1 + 10
  46. Line8.Y2 = Line8.Y2 + 10
  47. If Line6.Y2 >= 2880 Then
  48. a = 2
  49.  
  50. End If
  51. Else
  52. If a = 2 Then
  53. Line6.Y1 = Line6.Y1 - 10
  54. Line6.Y2 = Line6.Y2 - 10
  55.  
  56. Line7.Y2 = Line7.Y2 - 10
  57. Line7.Y1 = Line7.Y1 - 10
  58.  
  59. Line8.Y1 = Line8.Y1 - 10
  60. Line8.Y2 = Line8.Y2 - 10
  61.  
  62. If Line6.Y2 <= 960 Then
  63. a = 1
  64. End If
  65. Else
  66. Line6.Y1 = Line6.Y1 + 10
  67. Line6.Y2 = Line6.Y2 + 10
  68.  
  69. Line7.Y1 = Line7.Y1 + 10
  70. Line7.Y2 = Line7.Y2 + 10
  71.  
  72. Line8.Y1 = Line8.Y1 + 10
  73. Line8.Y2 = Line8.Y2 + 10
  74.  
  75. a = 2
  76.  
  77. End If
  78. End If
  79.  
  80. End Sub
  81. Private Sub controllo()
  82. If tn.X1 > shape1.X2 And tn.X1 < shape1.X1 Then
  83.     If shape1.Y1 > tn & ".Y2" And shape1.Y1 < tn & ".Y1" Then
  84.         haiperso
  85.     End If
  86. End If
  87. End Sub
  88. Private Sub Timer2_Timer()
  89. Dim f As Byte
  90. For f = 6 To 15
  91.     tn = "Line" & f
  92.     controllo
  93. Next
  94.  
  95. End Sub
  96.  
  97. Private Sub Timer3_Timer()
  98. lima = 240
  99. limb = 3200
  100. lims = 2100
  101. limd = 3400
  102. sn = Line9
  103. shapemov
  104. sn = Line10
  105. shapemov
  106. End Sub
  107.  
  108.  
  109. Private Sub Timer4_Timer()
  110. Dim f As Byte
  111. lima = 240
  112. limb = 3200
  113. lims = 240
  114. limd = 1920
  115. For f = 11 To 15
  116.     sn = "Line" & f
  117.     shapemov
  118. Next
  119. End Sub
  120. Private Sub shapemov()
  121. Dim s As Byte
  122. Randomize
  123. s = Val(Int(Rnd * 4))
  124. Select Case s
  125. Case Is = 0
  126.     sn.Y1 = sn.Y1 + 50
  127.     sn.Y2 = sn.Y2 + 50
  128. Case Is = 1
  129.     sn.Y1 = sn.Y1 - 50
  130.     sn.Y2 = sn.Y2 - 50
  131. Case Is = 2
  132.     sn.X1 = sn.X1 + 50
  133.     sn.X2 = sn.X2 + 50
  134. Case Is = 3
  135.     sn.X1 = sn.X1 - 50
  136.     sn.X2 = sn.X2 - 50
  137. End Select
  138.  
  139. If sn.Y1 <= lima Then
  140.     sn.Y1 = sn.Y1 + 100
  141.     sn.Y2 = sn.Y2 + 100
  142. End If
  143. If sn.Y1 >= limb Then
  144.     sn.Y1 = sn.Y1 - 100
  145.     sn.Y2 = sn.Y2 - 100
  146. End If
  147. If sn.X1 <= lims Then
  148.     sn.X1 = sn.X1 + 100
  149.     sn.X2 = sn.X2 + 100
  150. End If
  151. If sn.X1 >= limd Then
  152.     sn.X1 = sn.X1 - 100
  153.     sn.X2 = sn.X2 - 100
  154. End If
  155.  
  156. End Sub



Le differenze tra i due codici sono il movimento di shape1(che in realtà è una line) tramite command button e l'assenza del controllo sulla posizione di shape1...
Il problema è che le variabili così dichiarate non funzionano! :pat:
In più quando uso l'eseguibile e faccio riferimento a unload form1 non viene bloccata l'esecuzione del programma ma viene solo chiuso il form...
Qualcuno mi può aiutare?
Vi ringrazio in anticipo e vi chiedo ancora scusa per eventuali errori... :-|

PM Quote
Avatar
GrG (Member)
Guru^2


Messaggi: 3430
Iscritto: 21/08/2007

Segnala al moderatore
Postato alle 22:51
Domenica, 30/05/2010
non capisco...

L'istruzione Unload serve per chiudere il form indicato, quindi funziona correttamente... tu che vorresti fare?

PM Quote
Avatar
Alfonso (Ex-Member)
Guru


Messaggi: 688
Iscritto: 30/09/2009

Segnala al moderatore
Postato alle 0:53
Lunedì, 31/05/2010
Prima di tutto è impossibile fare una verifica sul campo del tuo listato. Con così tante linee, shape, command, timer è troppo faticoso approntare il progetto come l'hai fatto tu.
La prossima volta allega uno zip della form completa.

Comunque che non si chiuda con unload è l'ultimo dei tuoi problemi.
Guardando il listato si nota:

Dim tn As Variant  
a tn viene assegnato un valore in Timer2_Timer
tn="Line" & F  
il risultato è una stringa non certo un controllo line come penso tu supponga. Infatti chiamando la sub controllo si ha un errore "Necessario oggetto" quando scrivi tn.x1
Come l'hai impostato tu, tn potrebbe indicare il nome del controllo (che è una stringa) ma allora nella sub controllo dovresti scrivere una routine di scansione dei controlli lines in cerca di quello con il nome tn e procedere su quello per i confronti.

Più semplicemente ti consiglio di creare un array di lines1 da 6 a 17 e poi nel ciclo for passare f (l'indice dell'array di linee) alla sub controllo
Codice sorgente - presumibilmente VB.NET

  1. Private Sub Timer2_Timer()
  2.   Dim f As Byte
  3.   For f = 6 To 15
  4.     controllo f
  5.   Next
  6. End Sub
  7.  
  8. Private Sub controllo(f as Byte)
  9.   If line1(f).X1 > shape1.X2 And line1(f).X1 < shape1.X1 Then
  10.     If shape1.Y1 > line1(f).Y2 And shape1.Y1 < line1(f).Y1 Then
  11.         haiperso
  12.     End If
  13.   End If
  14. End Sub



Così devi fare per sn.

PM Quote
Avatar
Berto (Normal User)
Newbie


Messaggi: 14
Iscritto: 30/05/2010

Segnala al moderatore
Postato alle 14:45
Lunedì, 31/05/2010
Vi ringrazio per la pazienza, se sapessi come allegare direttamente il form e il progetto sul vostro sito lo avrei fatto...
L'idea di Alfonso è ottima, usare un vettore risolverebbe il problema ma io gli array e le matrici le so fare solo di numeri o stringhe... :_doubt:
Mi chiedevo inoltre se ci fosse un modo di dichiarare le variabili tale da permettere al mio programma di funzionare.
Per quanto riguarda l'unload mi scuso GrG per la poca chiarezza nella domanda posta, provo a riformularla:
Io vorrei che tutto il processo riguardante il programma fosse chiuso non solo il form1, infatti, dopo aver usato l'unload, il processo è ancora visibile da task manager.
In pratica: COME CHIUDO IL PROGRAMMA? :hail:

PM Quote
Avatar
Alfonso (Ex-Member)
Guru


Messaggi: 688
Iscritto: 30/09/2009

Segnala al moderatore
Postato alle 16:31
Lunedì, 31/05/2010
Creare un array di linee è molto semplice:
Dalla Casella degli strumenti selezioni il controllo linea e tracci la tua linea sulla form. Se è la prima linea che tracci sarà la line1. Ti posizioni con il mouse su uno degli estremi della linea selezionata, premi il tasto destro del mouse, scegli nel menu contestuale Copia. Ti posizioni con il mouse su un punto vuoto del form e cliccando con il tasto destro, dal menu che appare scegli Incolla.
Ti sarà chiesto se vuoi creare una matrice di controlli. Se rispondi  Sì,  ti ritroverai con una linea Line1(0) e una Line1(1).
Continuando ad incollare arrivi fino a Line1(16).
Cancelli le linee con indice da 0 a 5  ed hai pronto un array con indice da 6 a 16.

Se preferisci continuare così come sei adesso:

Dim tn As String

Questo rimane come è adesso
Codice sorgente - presumibilmente VB.NET

  1. Private Sub Timer2_Timer()
  2.   Dim f As Byte
  3.   For f = 6 To 15
  4.      tn = "Line" & f
  5.      controllo
  6.   Next
  7. End Sub


Scansioni l'array dei controlli presenti nella form e quando trovi quello con il nome che cerchi lo utilizzi per i confronti
Codice sorgente - presumibilmente VB.NET

  1. Private Sub controllo()
  2.   Dim t As Integer
  3.   For t = 0 To Controls.Count - 1
  4.     If Controls(t).Name = tn Then
  5.         If Controls(t).X1 > shape1.X2 And Controls(t).X1 < shape1.X1 Then
  6.             If shape1.Y1 > Controls(t).Y2 And shape1.Y1 < Controls(t).Y1 Then
  7.                 haiperso
  8.             End If
  9.         End If
  10.     End If
  11.   Next
  12. End Sub



Stessa cosa per sn e la Sub shapemov()

PM Quote
Avatar
Berto (Normal User)
Newbie


Messaggi: 14
Iscritto: 30/05/2010

Segnala al moderatore
Postato alle 16:54
Lunedì, 31/05/2010
Grazie e mille!!! Io volevo fare funzionare un programma e ora so anche come creare una matrice di comandi! :rotfl::rotfl::rotfl:
Però resta il fatto che non riesco a chiudere il programma quando uno perde e che non ho la minima idea di come mettere il file .rar del programma completato sul sito...
Potete aiutarmi anche per questi problemi?

Ultima modifica effettuata da Berto il 31/05/2010 alle 18:32
PM Quote
Avatar
Alfonso (Ex-Member)
Guru


Messaggi: 688
Iscritto: 30/09/2009

Segnala al moderatore
Postato alle 18:46
Lunedì, 31/05/2010
Il programma deve essere in formato .zip e lo puoi allegare quando stai rispondendo ad un messaggio. Alla sinistra del pulsante Send c'è il pulsante Sfoglia che ti serve per selezionare il file da allegare.

Non so cosa fa il tuo programma. Se è composto solo dal form1 e con unload form1 non si chiude completamente, vuol dire che potrebbe esserci in corso una qualche procedura o un loop e comunque, prima di chiudere, disabilita i timer.

Timer1.enabled = false

E in form_unload metti Set Form1 = Nothing

PM Quote
Avatar
Berto (Normal User)
Newbie


Messaggi: 14
Iscritto: 30/05/2010

Segnala al moderatore
Postato alle 18:50
Lunedì, 31/05/2010
Grazie, quando avrò apportato le modifiche pubblicherò il programma completo e potrai provarlo di persona... (Io sono per l'open source) :heehee:

PM Quote
Avatar
Berto (Normal User)
Newbie


Messaggi: 14
Iscritto: 30/05/2010

Segnala al moderatore
Postato alle 19:51
Lunedì, 31/05/2010
Alfonso, non so cosa ho sbagliato ma non funziona... :d
Allego il form e il progetto... Magari riesci a capire il problema...
Grazie per la pazienza.

Ultima modifica effettuata da Berto il 01/06/2010 alle 19:01
PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo