Semplificare codice in classi



  • Semplificare codice in classi
    di IngElettro (utente non iscritto) data: 24/08/2016 14:43:43

    Buongiorno a tutti,

    vi porgo il mio problema,
    mediante degli UserForm1 e dei moduli creo un sistema che mi rende automatico l'inserimento di nuovi oggetti che simulano un segnale binario (48 input e 48 output) e mi genera il file di testo con la sequenza bianaria che si desidera ottenere.

    Il codice attuale funziona ma vorrei sapere come posso semplificare tutto e renderlo automatico, mi spiego meglio, al posto che scrivere 48 volto lo stesso codice cambiando la variabile , il peso specific del bit e il nome del Option Button, vorrei creare una classe che mi permette di automatizzare il tutto con Array e strutture... Però non saprei bene come fare...

    grazie per l'aiuto
     
    Dim nome As String
    Dim lettura, miaVarForm2, teleruttore, out9, out10, out11, out12, out13, out14, out15, out16, out17, out18, out19, out20, out21, out22, out23, out24, in9, in10, in11, in12, in13, in14, in15, in16, in17, in18, in19, in20, in21, in22, in23, in24, out8, out7, out6, out5, out4, out3, out2, out1, in8, in7, in6, in5, in4, in3, in2, in1 As Double
    
    
    
    
    
    
    
    
    
    
    Private Sub CheckBox36_Click()
    
    
    If CheckBox36.Value = True Then
    lettura = "InOut"
    
    
    Else:
    lettura = "OutIn"
    
    End If
    
    End Sub
    
    
    
    
    
    
    
    
    
    Private Sub CheckBox38_Click()
    If CheckBox38.Value = True Then
    out14 = 2 ^ 10
    
    
    Else: out14 = 0
    End If
    End Sub
    
    Private Sub CheckBox39_Click()
    If CheckBox39.Value = True Then
    out13 = 2 ^ 10
    Else: out13 = 0
    End If
    End Sub
    
    Private Sub CheckBox40_Click()
    If CheckBox40.Value = True Then
    out15 = 2 ^ 10
    Else: out15 = 0
    End If
    End Sub
    
    Private Sub CheckBox41_Click()
    If CheckBox41.Value = True Then
    out16 = 2 ^ 10
    Else: out16 = 0
    End If
    
    
    End Sub
    
    
    Private Sub CheckBox42_Click()
    If CheckBox42.Value = True Then
    out9 = 2 ^ 9
    Else: out9 = 0
    End If
    End Sub
    
    Private Sub CheckBox43_Click()
    If CheckBox43.Value = True Then
    out10 = 2 ^ 10
    MsgBox (out10)
    
    Else: out10 = 0
    End If
    End Sub
    
    Private Sub CheckBox44_Click()
    If CheckBox44.Value = True Then
    out11 = 2 ^ 11
    Else: out11 = 0
    End If
    End Sub
    
    Private Sub CheckBox45_Click()
    If CheckBox45.Value = True Then
    out12 = 2 ^ 12
    Else: out12 = 0
    End If
    End Sub
    
    
    
    Private Sub CommandButton1_Click()  '// Rountine che assegna numero e valore e lo restituisce su una nuova riga
    ultimariga = Cells(Rows.Count, "A").End(xlUp).Row
    Cells(ultimariga + 1, 1).EntireRow.Insert
    
    
    
    Worksheets("Configurazione Home").Cells(ultimariga + 1, 1).Value = TextBox1.Value
    Worksheets("Configurazione Home").Cells(ultimariga + 1, 2).Value = out24 + out23 + out22 + out21 + out20 + out19 + out18 + out17 + out16 + out15 + out14 + out13 + out12 + out11 + out10 + out9 + out8 + out7 + out6 + out5 + out4 + out3 + out2 + out1
    Worksheets("Configurazione Home").Cells(ultimariga + 1, 3).Value = in8 + in7 + in6 + in5 + in4 + in3 + in2 + in1
    Worksheets("Configurazione Home").Cells(ultimariga + 1, 4).Value = teleruttore
    Worksheets("Configurazione Home").Cells(ultimariga + 1, 50).Value = Cells(1, 100).Value
    ActiveCell.EntireRow.Insert
    
    
    Unload Me
    
    
    
    
    End Sub
    
    
    
    Private Sub TextBox2_Change()
    TextBox2.Value = Cells(1, 100).Value
    
    End Sub
    
    Private Sub Label4_Click()
    
    End Sub
    
    Private Sub TextBox1_Change()
    nome = TextBox1.Value
    
    
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    
    
    Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
    teleruttore = 1
    Else: teleruttore = 0
    End If
    End Sub
    
    Private Sub CheckBox10_Click()
    If CheckBox10.Value = True Then
    in1 = 1
    Else: in1 = 0
    End If
    End Sub
    
    Private Sub CheckBox11_Click()
    If CheckBox11.Value = True Then
    in6 = 32
    Else: in6 = 0
    End If
    End Sub
    
    Private Sub CheckBox12_Click()
    If CheckBox12.Value = True Then
    in5 = 16
    Else: in5 = 0
    End If
    End Sub
    
    Private Sub CheckBox13_Click()
    If CheckBox13.Value = True Then
    in7 = 64
    Else: in7 = 0
    End If
    End Sub
    
    Private Sub CheckBox14_Click()
    If CheckBox14.Value = True Then
    in8 = 128
    Else: in8 = 0
    End If
    End Sub
    
    Private Sub CheckBox15_Click()
    If CheckBox15.Value = True Then
    in2 = 2
    Else: in2 = 0
    End If
    End Sub
    
    Private Sub CheckBox16_Click()
    If CheckBox16.Value = True Then
    in3 = 4
    Else: in3 = 0
    End If
    End Sub
    
    Private Sub CheckBox17_Click()
    If CheckBox17.Value = True Then
    in4 = 8
    Else: in4 = 0
    End If
    End Sub
    
    Private Sub CheckBox2_Click()
    If CheckBox2.Value = True Then
    out6 = 32
    Else: out6 = 0
    End If
    End Sub
    
    Private Sub CheckBox3_Click()
    If CheckBox3.Value = True Then
    out5 = 16
    Else: out5 = 0
    End If
    End Sub
    
    Private Sub CheckBox4_Click()
    If CheckBox4.Value = True Then
    out7 = 64
    Else: out7 = 0
    End If
    End Sub
    
    Private Sub CheckBox5_Click()
    If CheckBox5.Value = True Then
    out8 = 128
    Else: out8 = 0
    End If
    End Sub
    
    Private Sub CheckBox6_Click()
    If CheckBox6.Value = True Then
    out1 = 1
    Else: out1 = 0
    End If
    End Sub
    
    Private Sub CheckBox7_Click()
    If CheckBox7.Value = True Then
    out2 = 2
    Else: out2 = 0
    End If
    End Sub
    
    Private Sub CheckBox8_Click()
    If CheckBox8.Value = True Then
    out3 = 4
    Else: out3 = 0
    End If
    End Sub
    
    Private Sub CheckBox9_Click()
    If CheckBox9.Value = True Then
    out4 = 8
    Else: out4 = 0
    End If
    End Sub
    
    
    
    
    
    



  • di patel data: 24/08/2016 17:44:00

    probabilmente si può fare anche senza classi con gli array di controlli, ma allega un file di esempio da testare





  • di Albatros54 data: 24/08/2016 18:08:04

    Cit...." e il nome del Option Button "
    nel codice che hai postato non vedo Option Button ma Checkbox.

    Ti allego un file, potrebbe essere una base di partenza.
    allego sotto il codice.
    Ciao  
     
     Codice  da incollare in un Modulo di Classe 
    Option Explicit
    
    Public WithEvents chk As MSForms.CheckBox
    Public frm As UserForm
    Dim out14 As Double
    Dim out13 As Double
    
    Private Sub chk_Click()
              Select Case chk.Name
           Case "CheckBox1"
           If chk.Value = True Then
    out14 = 2 ^ 10
    MsgBox out14
    End If
     
     Case "CheckBox2"
     If chk.Value = True Then
    If out13 = 2 ^ 10 Then
    Else: out13 = 0
    MsgBox out13
    End If
    End If
    End Select
    End Sub
    
    
     Codice  da incollare userform 
    
    Option Explicit
    
    Dim ColCheckBox As Collection
    Dim mychk As ClsCheckBox
    
    Private Sub UserForm_Initialize()
    
        
        Dim ctl As MSForms.Control
        
        
        Set ColCheckBox = New Collection
        
        
        
        For Each ctl In Me.Controls
           
            If TypeOf ctl Is MSForms.CheckBox Then
                Set mychk = New ClsCheckBox
                Set mychk.chk = ctl
                Set mychk.frm = Me
                ColCheckBox.Add mychk
                End If
                Next
    
    Set ctl = Nothing
        Set mychk= Nothing
    End Sub
    
    






  • di IngElettro (utente non iscritto) data: 25/08/2016 07:55:18

    Ciao.. Ti ringrazio per la risposta , é esattamente ciò che cercavo... Ora lo provo con calma e ti faccio sapere

    Si Checkbox e non Option Button scusami, ma resta applicabile il modello su ogni tipo quindi é quanto cercavo

    Grazie mille



  • di scossa data: 25/08/2016 15:30:01

    Ciao IngElettro,

    purtroppo non alleghi un file su cui lavorare e quindi non mi è chiaro cosa vuoi ottenere.
    Comunque il codice proposto da Albatros54 (ciao) mi sembra che non possa funzionare correttamente, per via di come gestisce le variabili Out14, Out13 etc.. avendo visibilità a livello di classe e non a livello di progetto.

    Sempre come semplice punto di partenza, allego il file ProvaCheckBox.
    Qui sotto i vari codici (nella UserForm1 ci sono 3 checkbox ChkB1, ChkB2, ChkB3).



    scossa's web site
    Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee.
    (George Bernard Shaw)



     
    '---------------------------------------------
    ' nel modulo di classe di ThisWorkbook
    '---------------------------------------------
    
    Private Sub Workbook_Open()
      UserForm1.Show
    End Sub
    
    
    
    '---------------------------------------------
    ' in un modulo standard
    '---------------------------------------------
    
    Public Bit1 As Byte
    Public Bit2 As Byte
    Public Bit3 As Byte
    
    
    
    
    
    '----------------------------------------------
    ' nel modulo di classe della userform UserForm1
    '----------------------------------------------
    
    Dim oColEvents As Collection
    
    Private Sub UserForm_Initialize()
      Dim ChkbEvent As CheckBoxClass
      Dim oCtrl As Control
      
      Set oColEvents = New Collection
      
      For Each oCtrl In Me.Controls
          If TypeName(oCtrl.Object) = "IMdcCheckBox" Then
              Set ChkbEvent = New CheckBoxClass
              Set ChkbEvent.mCheckboxes = oCtrl
              oColEvents.Add ChkbEvent, oCtrl.Name
              Set ChkbEvent = Nothing
          End If
      Next
    End Sub
    
    
    
    
    '----------------------------------------------
    ' nel modulo di classe CheckBoxClass
    '----------------------------------------------
    
    Public WithEvents mCheckboxes As MSForms.CheckBox
    
    Private Sub mCheckboxes_Click()
      Bit1 = -UserForm1.ChkB1.Value
      Bit2 = -UserForm1.ChkB2.Value
      Bit3 = -UserForm1.ChkB3.Value
      MsgBox " Bit 1" & vbTab & "Bit 2" & vbTab & "Bit 3" & vbCrLf & _
             "    " & Bit1 & vbTab & "   " & Bit2 & vbTab & "   " & Bit3
      Debug.Print 1 * Bit1 + 10 * Bit2 + 100 * Bit3
    End Sub
    



  • di alfrimpa data: 25/08/2016 15:49:10

    @ scossa

    Scusa Marco mi spieghi cosa sta ad indicare il trattino posto prima di userform1?

    Grazie

    Alfredo





  • di scossa data: 25/08/2016 15:55:16

    Ciao Alfredo,

    serve a convertire il valore logico True nel numero 1 (Bit1 e le altre sono dichiarate Byte), ma al contrario del VERO lato celle, che convertito in numero varrebbe 1 (ricordi i vari -- usati lato celle?), in VBA il valore True convertito varrebbe -1 e non 1, quindi con il - davanti diventa 1

    scossa's web site
    Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee.
    (George Bernard Shaw)



  • di alfrimpa data: 25/08/2016 16:00:53

    Grazie mille per la fulminea risposta Marco

    A oresto

    Alfredo





  • di IngElettro (utente non iscritto) data: 25/08/2016 16:15:08

    Ciao Scossa

    Grazie per la risposta domani provo a riscrivere il codice usando le classi

    in realtà non voglio ottenere nulla di diverso a livello di codice perché il programma funziona correttamente ma vorrei solo riscriverlo usando le classi per evitare di fare la stessa operazione 96 volte, in quanto simulo 48 Input e 48 output, attualmente ho sviuluppato un modello funzionale a 8 bit, invece il definitive sarà a 48 bit /Input e 48 output. Detto questo ti ringrazio per il tempo dedicato




  • di Albatros54 data: 25/08/2016 16:44:18