Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: corlo en 9 Noviembre 2021, 19:14 pm



Título: eliminar registro
Publicado por: corlo en 9 Noviembre 2021, 19:14 pm
Hola soy corlo
estoy haciendo una mini aplicacion en añadir datos a los textbox ,  para luego leer los datos con el combo y una opcion para eliminar los datos.

añadir los datos: lo hace bien
leer los datos: lo hace bien
eliminar los datos: elimina el dato en el combo pero no elimina los datos de los textbox
no se que hago mal
paso el codigo que tengo hasta ahora

en un formulario

Código:

Option Explicit


Private Sub Boton_añadir_Click()
totalregistros = totalregistros + 1
If totalregistros > 50 Then
MsgBox "lista completa", 16, "error"
Else
agenda(totalregistros).Nombre = Nom.Text
agenda(totalregistros).apellidos = Ape.Text
agenda(totalregistros).telefono = Tel.Text
agenda(totalregistros).Edad = Val(Edad.Text)

Combo2.AddItem Nom.Text
End If
Nom.SetFocus
End Sub

Private Sub Boton_eliminar_Click()
Dim b As String

b = MsgBox("Eliminar Registro:" + Nom.Text, 3 + 32, "Eliminar")
If b = vbYes Then



If Combo2.ListIndex <> -1 Then




Combo2.RemoveItem (Combo2.ListIndex)



End If

totalregistros = totalregistros - 1

agenda(totalregistros).Nombre = Nom.Text
agenda(totalregistros).apellidos = Ape.Text
agenda(totalregistros).telefono = Tel.Text
agenda(totalregistros).Edad = Val(Edad.Text)





End If
Nom.Text = ""
Ape.Text = ""
Tel.Text = ""
Edad.Text = ""
End Sub

Private Sub Boton_fin_Click()
End
End Sub





Private Sub Botonnuevo_Click()
Nom.Text = ""
Ape.Text = ""
Tel.Text = ""
Edad.Text = ""
Nom.SetFocus

End Sub





Private Sub Combo2_Click()
Dim n As Integer
n = Combo2.ListIndex + 1

Nom.Text = agenda(n).Nombre
Ape.Text = agenda(n).apellidos
Tel.Text = agenda(n).telefono
Edad.Text = Val(agenda(n).Edad)



End Sub

Private Sub Ape_GotFocus()
Ape.SelStart = 0
Ape.SelLength = Len(Ape.Text)
End Sub

Private Sub Edad_GotFocus()
Edad.SelStart = 0
Edad.SelLength = Len(Edad.Text)
End Sub

Private Sub Nom_GotFocus()
Nom.SelStart = 0
Nom.SelLength = Len(Nom.Text)
End Sub
Private Sub Tel_GotFocus()
Tel.SelStart = 0
Tel.SelLength = Len(Tel.Text)
End Sub



Private Sub Form_Load()
totalregistros = 0
End Sub







y en un modulo


Código:

Type registro
Nombre As String * 15
apellidos As String * 25
telefono As String * 15
Edad As String * 3
End Type
Global agenda(1 To 50) As registro
Global totalregistros As Integer









gracias


Título: Re: eliminar registro
Publicado por: Serapis en 10 Noviembre 2021, 23:23 pm
En realidad, lo peor no es que no se borren los datos de los textbox (que no solo no importa si no que interesa conservarlo temporalmente)... lo peor es que no se borran de tu array.

Hay una variedad de indicaciones que dar y corregir... pero no quiero extenderme en explicaciones... porque son elementales y uno debiera ceñirse a ellos.

Te pòngo un código que puedes remplazar por todo lo que tienes. nota que he renombrado los textbox y los botones a un valor más acorde y el boton 'nuevo', en realidad sobra, aún así su código todavía es válido.

Nota también, que si no tienes más módulos de código donde uses esos registros de la agenda, el código en el módulo puede ser pasado al formulario (pero con alcance privado).

Código
  1.  
  2. Private Type Registro
  3.    Nombre                  As String * 15
  4.    Apellidos               As String * 25
  5.    Telefono                As String * 15
  6.    Edad                    As String * 3
  7. End Type
  8.  
  9. Private Agenda(0 To 49)     As Registro
  10. Private totalregistros      As Integer
  11. Private NoUpdateTxts        As Boolean
  12.  
  13.  
  14. Private Sub BtnAñadir_Click()
  15.    If (totalregistros = 50) Then
  16.        MsgBox "lista completa", 16, "error"
  17.    Else
  18.        If (Esregistrable = True) Then
  19.            With Agenda(totalregistros)
  20.                .Nombre = TxtNombre.Text
  21.                .Apellidos = TxtApellidos.Text
  22.                .Telefono = TxtTelefono.Text
  23.                .Edad = Val(TxtEdad.Text)
  24.            End With
  25.  
  26.            Combo2.AddItem TxtNombre.Text
  27.            totalregistros = (totalregistros + 1)
  28.  
  29.            Call BtnNuevo_Click
  30.        Else
  31.            MsgBox "El registor está incompleto, no puede añadirse hasta que esté completo."
  32.        End If
  33.    End If
  34. End Sub
  35.  
  36. Private Sub BtnEliminar_Click()
  37.    Dim b As VbMsgBoxResult
  38.    Dim index As Integer, k As Integer, j As Integer
  39.  
  40.    j = (Combo2.ListCount - 1)
  41.    index = Combo2.ListIndex
  42.    If (index = -1) Then  ' exit sub
  43.        Combo2.ListIndex = j  ' si no hay ninguno seleccionado se supone el último.
  44.        index = j
  45.    End If
  46.  
  47.    b = MsgBox("Eliminar Registro: " + TxtNombre.Text, 3 + 32, "Eliminar")
  48.    If (b = vbYes) Then
  49.        ' Los datos a eliminar, se pasan a los textbox, por si se quiere volver a añadir o editar antes de añadir.
  50.        Call Combo2_Click
  51.        ' opcionalmente se puede simplemente borrar...
  52.        'Call BtnNuevo_Click
  53.  
  54.        ' Eliminar el item del combo.
  55.        Combo2.RemoveItem (index)
  56.  
  57.        ' Bajar todos los items en el array por encima del item eliminado una posición.
  58.        For k = index To j - 1
  59.            Agenda(k) = Agenda(k + 1)
  60.        Next
  61.  
  62.        ' ahora el último ítem queda libre, se puede vaciar o dejarlo...
  63.        With Agenda(k)
  64.            .Nombre = ""
  65.            .Apellidos = ""
  66.            .Telefono = ""
  67.            .Edad = ""
  68.        End With
  69.  
  70.        ' queremos que en el combo siempre se vea un item.
  71.        NoUpdateTxts = True
  72.        If (j > 0) Then
  73.            If (index < j) Then
  74.                Combo2.ListIndex = index
  75.            Else
  76.                Combo2.ListIndex = (j - 1)
  77.            End If
  78.        End If
  79.        NoUpdateTxts = False
  80.  
  81.        totalregistros = j ' descontar 1.
  82.  
  83.        ' Desactivar el botón eliminar si no quedan registros.
  84.        BtnEliminar.Enabled = (totalregistros > 0)
  85.    End If
  86. End Sub
  87.  
  88. Private Sub BtnTerminar_Click()
  89.    End
  90. End Sub
  91.  
  92. Private Sub BtnNuevo_Click()
  93.    TxtNombre.Text = ""
  94.    TxtApellidos.Text = ""
  95.    TxtTelefono.Text = ""
  96.    TxtEdad.Text = ""
  97.    TxtNombre.SetFocus
  98. End Sub
  99.  
  100. Private Sub Combo2_Click()
  101.    If (NoUpdateTxts = False) Then
  102.        With Agenda(Combo2.ListIndex)
  103.            TxtNombre.Text = .Nombre
  104.            TxtApellidos.Text = .Apellidos
  105.            TxtTelefono.Text = .Telefono
  106.            TxtEdad.Text = Val(.Edad)
  107.        End With
  108.    End If
  109. End Sub
  110.  
  111. Private Sub TxtEdad_GotFocus()
  112.    Call SelOnGotFocus(TxtEdad)
  113. End Sub
  114.  
  115. Private Sub TxtApellidos_GotFocus()
  116.    Call SelOnGotFocus(TxtApellidos)
  117. End Sub
  118.  
  119. Private Sub TxtNombre_GotFocus()
  120.    Call SelOnGotFocus(TxtNombre)
  121. End Sub
  122.  
  123. Private Sub TxtTelefono_GotFocus()
  124.    Call SelOnGotFocus(TxtTelefono)
  125. End Sub
  126.  
  127. Private Sub SelOnGotFocus(ByRef Txt As TextBox)
  128.    Txt.SelStart = 0
  129.    Txt.SelLength = Len(Txt.Text)
  130. End Sub
  131.  
  132. ' Verifica que los campos dle registor no quedan vacíos (sdon obligatorios)
  133. '  Si un campo no es obligatorio, debe retirarse de aquí.
  134. Private Function Esregistrable() As Boolean
  135.    If (Len(Me.TxtNombre.Text) > 0) Then
  136.        If (Len(Me.TxtApellidos.Text) > 0) Then
  137.            If (Len(Me.TxtTelefono.Text) > 0) Then
  138.                Esregistrable = IsNumeric(Me.TxtEdad.Text)
  139.            End If
  140.        End If
  141.    End If
  142. End Function
  143.  
  144. 'Private Sub Form_Load()
  145. '    totalregistros = 0
  146. 'End Sub
  147.  

Ejecuta el código paso a paso al menos el del botón eliminar, para ver qué va sucediendo a cada instante... (arranca la aplicación con la tecla F8, o bien pon un punto de interrupción (tecla F9) en la línea del boton eliminar y cuando llegue allí se parará y entonces ejecuta paso a paso con la tecla F8).
(http://imgfz.com/i/QHipF5x.png)

Por favor, como ya llevas tiempo en el foro, acostumbra a colocar tu código entre etiquetas GESHI (elige VB), que aparecen en el editor cuando redactas tu mensaje...


Título: Re: eliminar registro
Publicado por: corlo en 11 Noviembre 2021, 00:04 am
Hola  Serapis


Muchas gracias por el codigo

todo correcto lo he adaptado y funciona

 llevaba varios dias con el codigo y no me salia gracias.