• Martes 30 de Abril de 2024, 05:02

Autor Tema:  Resumir Código  (Leído 970 veces)

Sonia Rodri

  • Nuevo Miembro
  • *
  • Mensajes: 10
    • Ver Perfil
Resumir Código
« en: Viernes 9 de Enero de 2004, 13:55 »
0
Hola, tengo el siguiente código, y me parece excesivamente largo.Se que se puede resumir con matrices, funciones..., pero el caso es que no soy muy experta, y me cuesta mucho, cada vez que genero código.Podrian darme algun consejo, o bien recomendarme algún manual, o tutorial por internet?

Gracias ;-)

Dim A As String
Dim B As String
Dim C As String
Dim D As String
Dim E As String
Dim F As String
Dim G As String
Dim H As String
Dim I As String
Dim J As String
Private Sub text17_lostfocus()
Text17.Text = FormatNumber(Val(Text17.Text), 2, 0, 0, 0)
End Sub
Private Sub text18_lostfocus()
Text18.Text = FormatNumber(Val(Text18.Text), 2, 0, 0, 0)
End Sub
Private Sub text19_lostfocus()
Text19.Text = FormatNumber(Val(Text19.Text), 2, 0, 0, 0)
End Sub
Private Sub text20_lostfocus()
Text20.Text = FormatNumber(Val(Text20.Text), 2, 0, 0, 0)
End Sub
Private Sub text21_lostfocus()
Text21.Text = FormatNumber(Val(Text21.Text), 2, 0, 0, 0)
End Sub
Private Sub text22_lostfocus()
Text22.Text = FormatNumber(Val(Text22.Text), 2, 0, 0, 0)
End Sub
Private Sub text49_Gotfocus()
A = Val(Text17.Text)
B = Val(Text33.Text)
C = A * B
Text49.Text = FormatNumber(C, 2, 0, 0, 0)
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub
Private Sub text50_Gotfocus()
A = Val(Text18.Text)
B = Val(Text34.Text)
C = A * B
Text50.Text = FormatNumber(C, 2, 0, 0, 0)
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub
Private Sub text51_Gotfocus()
A = Val(Text19.Text)
B = Val(Text35.Text)
C = A * B
Text51.Text = FormatNumber(C, 2, 0, 0, 0)
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub
Private Sub text52_Gotfocus()
A = Val(Text20.Text)
B = Val(Text36.Text)
C = A * B
Text52.Text = FormatNumber(C, 2, 0, 0, 0)
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub
Private Sub text53_Gotfocus()
A = Val(Text21.Text)
B = Val(Text37.Text)
C = A * B
Text53.Text = FormatNumber(C, 2, 0, 0, 0)
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub
Private Sub text54_Gotfocus()
A = Val(Text22.Text)
B = Val(Text38.Text)
C = A * B
Text54.Text = FormatNumber(C, 2, 0, 0, 0)
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub

Private Sub text107_Gotfocus()
D = Val(Text49.Text)
E = Val(Text50.Text)
F = Val(Text51.Text)
G = Val(Text52.Text)
H = Val(Text53.Text)
I = Val(Text54.Text)
J = D + E + F + G + H + I
Text107.Text = FormatNumber(J, 2, 0, 0, 0)
If Val(Text107.Text) > 0 Then
Check7.Value = 1
Else
Check7.Value = 0
End If
End Sub

Juanolo

  • Miembro MUY activo
  • ***
  • Mensajes: 202
    • Ver Perfil
Re: Resumir Código
« Respuesta #1 en: Viernes 9 de Enero de 2004, 20:54 »
0
Hola Sonia:
La verdad es que no tengo tiempo de resumir el código que enviaste, pero te puedo ayudar un poco en lo que me parece es el problema principal. Específicamente, veo que has creado muchos controles text, por lo que debes manejar toneladas de código. Lo ideal es que tengas una matriz.
Esto consiste en una serie de controles con el mismo nombre, pero con una propiedad INDEX distinta para cada uno. La forma más fácil de hacerlo es crear el primero, luego eliges copiar y pegar; automáticamente Visual te preguntará si quieres crear una matriz de controles... le dices que sí, y ya está.
Por ejemplo, si tienes un control text, te creará text(0) (el original), text(1), text(2)... etc.
Luego, debes acostumbrarte a que cambia un poco el código, pues ya no basta dirigir el código al nombre del control, sino que además deberás indicarle el índice. Ejemplo:

Antes:
text.setfocus
Ahora:
text(2).setfocus

La ventaja, te puedes ahorrar toneladas de código. Usando tu ejemplo:

Private Sub text_lostfocus(Index as integer)
    if index>=17 and index <=30 then
       Text(Index).Text = FormatNumber(Val(Text(Index).Text), 2, 0, 0, 0)
    endif
End Sub

Estas lineas de código cambian el formato para todos los cuadros de texto cuyos indices estén entre 17 y 30.

Salu2.
La luz que sólo se ilumina a sí misma, es oscuridad.

Sonia Rodri

  • Nuevo Miembro
  • *
  • Mensajes: 10
    • Ver Perfil
Re: Resumir Código
« Respuesta #2 en: Martes 13 de Enero de 2004, 12:22 »
0
muchisimas gracias juanolo


he estado trabajando con ello y me ha resultado, perfecto.me has ahorrado horas, y de las aburridas.

;-)