Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Optimizacion de Codigo

Estas en el tema de Optimizacion de Codigo en el foro de Visual Basic clásico en Foros del Web. Hola a todos, tengo un formulario bastante grande, donde hay varios checkbox y optionbutton, y dependiendo de los chequeados muestra unos y/o oculta otros este ...
  #1 (permalink)  
Antiguo 22/09/2011, 15:04
 
Fecha de Ingreso: septiembre-2009
Mensajes: 108
Antigüedad: 14 años, 7 meses
Puntos: 1
Optimizacion de Codigo

Hola a todos, tengo un formulario bastante grande, donde hay varios checkbox y optionbutton, y dependiendo de los chequeados muestra unos y/o oculta otros

este es el primero de 5 que hay, pero el codigo lo veo muy largo

Código:
Private Sub Agregar_Intervencion1_Click()
    If Agregar_Intervencion1 = 1 Then
        CheckMultiple1.Visible = True
        CheckBilateral1.Visible = True
        Agregar_Intervencion2.Enabled = True
    Else
        Agregar_Intervencion2.Enabled = False
        Agregar_Intervencion3.Enabled = False
        Agregar_Intervencion4.Enabled = False
        Agregar_Intervencion5.Enabled = False
        Agregar_Intervencion2.Value = 0
        Agregar_Intervencion3.Value = 0
        Agregar_Intervencion4.Value = 0
        Agregar_Intervencion5.Value = 0
        CheckMultiple1.Value = 0
        CheckMultiple2.Value = 0
        CheckMultiple3.Value = 0
        CheckMultiple4.Value = 0
        CheckMultiple5.Value = 0
        CheckBilateral1.Value = 0
        CheckBilateral2.Value = 0
        CheckBilateral3.Value = 0
        CheckBilateral4.Value = 0
        CheckBilateral5.Value = 0
        MismaVia1.Value = 0
        DistVia1.Value = 0
        MismaVia2.Value = 0
        DistVia2.Value = 0
        MismaVia3.Value = 0
        DistVia3.Value = 0
        MismaVia4.Value = 0
        DistVia4.Value = 0
        MismaVia5.Value = 0
        DistVia5.Value = 0
        CheckMultiple1.Visible = False
        CheckBilateral1.Visible = False
        MismaVia1.Visible = False
        DistVia1.Visible = False
        
        TextBilateral1.Text = 0
        TextMultiple1.Text = 0
        TextDistinta1.Text = 0
        TextMisma1.Text = 0
        
        TextBilateral2.Text = 0
        TextMultiple2.Text = 0
        TextDistinta2.Text = 0
        TextMisma2.Text = 0
        
        TextBilateral3.Text = 0
        TextMultiple3.Text = 0
        TextDistinta3.Text = 0
        TextMisma3.Text = 0
        
        TextBilateral4.Text = 0
        TextMultiple4.Text = 0
        TextDistinta4.Text = 0
        TextMisma4.Text = 0
        
        TextBilateral5.Text = 0
        TextMultiple5.Text = 0
        TextDistinta5.Text = 0
        TextMisma5.Text = 0
        
    End If
End Sub

Private Sub CheckBilateral1_Click()
     If CheckBilateral1 = 1 Then
        CheckMultiple1.Visible = False
        CheckMultiple1.Value = 0
        MismaVia1.Visible = False
        DistVia1.Visible = False
        TextBilateral1.Text = 1
    Else
         CheckMultiple1.Visible = True
         CheckBilateral1.Value = 0
         TextBilateral1.Text = 0
     End If
End Sub

Private Sub CheckMultiple1_Click()
    If CheckMultiple1 = 1 Then
       CheckBilateral1.Visible = False
       CheckBilateral1.Value = 0
       TextMultiple1.Text = 1
       MismaVia1.Visible = True
       DistVia1.Visible = True
    Else
       CheckBilateral1.Visible = True
       CheckBilateral1.Value = 0
       MismaVia1.Visible = False
       DistVia1.Visible = False
       TextMultiple1.Text = 0
       TextDistinta1.Text = 0
       TextMisma1.Text = 0
       MismaVia1.Value = 0
       DistVia1.Value = 0
    End If
End Sub

Private Sub MismaVia1_click()
If MismaVia1.Value = True Then
TextMisma1.Text = 1
TextDistinta1.Text = 0
End If
End Sub
Private Sub DistVia1_click()
If DistVia1.Value = True Then
TextDistinta1.Text = 1
TextMisma1.Text = 0
End If
End Sub
Aqui la leyenda de cada uno
Agregar_Intervencion1, 2, 3, 4 y 5 (CheckBox)
CheckBilateral1, 2, 3, 4 y 5 (CheckBox)
CheckMultiple1, 2, 3, 4 y 5 (CheckBox)
MismaVia1, 2, 3, 4, y 5 (OptionButton)
DistVia1, 2, 3, 4, y 5 (OptionButton)

de estos tengo 5, este es el primero, en el segundo saco los controles 1, en el tercero los controles 2 y asi sucesivamente

alguna idea o sugerencia

Muchas Gracias
  #2 (permalink)  
Antiguo 22/09/2011, 22:12
Avatar de culd  
Fecha de Ingreso: noviembre-2003
Mensajes: 959
Antigüedad: 20 años, 6 meses
Puntos: 19
Respuesta: Optimizacion de Codigo

En ves de crear muchos controles con el mismo nombre y le cambias el numero, mejor crea una matris de controles... Del mismo control... Asi te quedaria asi...

Agregar_Intervencion(numero)
etc
etc
etc

Y usas un FOR para poner todo en 0, o en FALSE, o TRUE, o lo que fuese, siempre comparando que no sea el numero clickeado...

Y asi sucesivamente, con eso resumis el codigo a unas cuantas lineas.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:10.