Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Public Class Form1
Dim I, cont, it, FilaActual As Integer
Dim n1, n2, n3, nr, Res, Total As Double
Dim txt3 As String
Private Sub btlentra_Click(sender As Object, e As EventArgs) Handles btlentra.Click
n1 = Txt4.Text
n2 = Txt5.Text
n3 = n1 * n2
Const val As String = "##,####.##"
nr = Format(n3, val)
Res = n3 + Res
txt3 = ComboTipo.Text
If txt3 = "" Then MsgBox("Falta introducir Tipo")
If txt3 <> "" Then DgvDatos.Rows.Add(txt1.Text, txt2.Text, txt3, Txt4.Text, Txt5.Text, nr)
Total = 0
For I = 0 To ((DgvDatos.RowCount.ToString()) - 1) ' cuenta el número de filas del Datagrid
Total = (DgvDatos.Item(5, I).Value) + Total
Next
Txtresult.Text = Format(Total, val)
End Sub
Private Sub btllimp_Click(sender As Object, e As EventArgs) Handles btllimp.Click
txt1.Text = ""
txt2.Text = ""
REM txt3.Text = ""
ComboTipo.DropDownStyle = ComboBoxStyle.DropDownList
ComboTipo.SelectedIndex = 0
Txt4.Text = 1
Txt5.Text = ""
txt1.Text = DateTimePicker1.Value.Date
End Sub
Private Sub btlsalir_Click(sender As Object, e As EventArgs) Handles btlsalir.Click
Guardardatos()
Application.Exit()
End Sub
Private Sub Btlmodificar_Click(sender As Object, e As EventArgs) Handles Btlmodificar.Click
Dim txt3 As String
txt3 = ComboTipo.Text
If txt3 = "" Then MsgBox("Falta introducir Tipo")
If txt3 <> "" Then DgvDatos.Item(0, DgvDatos.CurrentRow.Index).Value = txt1.Text
If txt3 <> "" Then DgvDatos.Item(1, DgvDatos.CurrentRow.Index).Value = txt2.Text
If txt3 <> "" Then DgvDatos.Item(2, DgvDatos.CurrentRow.Index).Value = txt3
If txt3 <> "" Then DgvDatos.Item(3, DgvDatos.CurrentRow.Index).Value = Txt4.Text
If txt3 <> "" Then DgvDatos.Item(4, DgvDatos.CurrentRow.Index).Value = Txt5.Text
n1 = Txt4.Text
n2 = Txt5.Text
n3 = n1 * n2
Const val As String = "##,####.##"
nr = Format(n3, val)
If txt3 <> "" Then DgvDatos.Item(5, DgvDatos.CurrentRow.Index).Value = nr
' MsgBox(DataGridView1.Item(5, 0).Value) ' valor columna 5 en la fila 0
' MsgBox(DataGridView1.Item(5, DataGridView1.CurrentRow.Index).Value) ' valor columna 5 en la fila donde se esta modificando
Total = 0
For I = 0 To ((DgvDatos.RowCount.ToString()) - 1) ' cuenta el número de filas del Datagrid
Total = (DgvDatos.Item(5, I).Value) + Total
Next
Txtresult.Text = Format(Total, val)
REM txt1.Text = DateTimePicker1.Value.Date
End Sub
Private Sub Datagridview1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvDatos.CellClick
Dim i As Integer = DgvDatos.CurrentRow.Index
txt1.Text = DgvDatos(0, i).Value
txt2.Text = DgvDatos(1, i).Value
txt3 = DgvDatos(2, i).Value
ComboTipo.Text = txt3 REM asigna el valor del tipo actual al combobox, al pulsar en una fila
Txt4.Text = DgvDatos(3, i).Value
Txt5.Text = DgvDatos(4, i).Value
End Sub
Private Sub Btleliminar_Click(sender As Object, e As EventArgs) Handles Btleliminar.Click
DgvDatos.Rows.Remove(DgvDatos.CurrentRow)
Total = 0
For I = 0 To ((DgvDatos.RowCount.ToString()) - 1)
Total = (DgvDatos.Item(5, I).Value) + Total
Next
Const val As String = "##,####.##"
Txtresult.Text = Format(Total, val)
txt1.Text = DateTimePicker1.Value.Date
End Sub
Private Sub DgvDatos_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvDatos.CellDoubleClick
FilaActual = DgvDatos.CurrentRow.Index
txt1.Text = DgvDatos.Rows(FilaActual).Cells("Column1").Value
txt2.Text = DgvDatos.Rows(FilaActual).Cells("Column2").Value
txt3 = DgvDatos.Rows(FilaActual).Cells("Column3").Value
Txt4.Text = DgvDatos.Rows(FilaActual).Cells("Column4").Value
Txt5.Text = DgvDatos.Rows(FilaActual).Cells("Column5").Value
n3 = DgvDatos.Rows(FilaActual).Cells("Column6").Value
End Sub
Private Sub leerdatos()
Dim archivo_leer As StreamReader
archivo_leer = New StreamReader("./datos.txt")
While Not archivo_leer.EndOfStream
Dim cadena As String = archivo_leer.ReadLine
Dim leer As String() = cadena.Split(New Char() {";"})
DgvDatos.Rows.Add(leer)
End While
archivo_leer.Close()
Total = 0
For I = 0 To ((DgvDatos.RowCount.ToString()) - 1)
Total = (DgvDatos.Item(5, I).Value) + Total
Next
Const val As String = "##,####.##"
Txtresult.Text = Format(Total, val)
End Sub
Private Sub Guardardatos() ' subrutina para guardar el archivo
Dim archivo_escritura As StreamWriter
Dim linea As String
archivo_escritura = New StreamWriter("./datos.txt")
With DgvDatos
For I = 0 To .RowCount - 2
linea = .Rows(I).Cells("Column1").Value & ";" &
.Rows(I).Cells("Column2").Value & ";" &
.Rows(I).Cells("Column3").Value & ";" &
.Rows(I).Cells("Column4").Value & ";" &
.Rows(I).Cells("Column5").Value & ";" &
.Rows(I).Cells("Column6").Value & ";"
archivo_escritura.WriteLine(linea)
Next
MsgBox("Ok Archivo Guardado")
End With
archivo_escritura.Close()
End Sub
Private Sub AnadirDatos()
Dim archivo_escritura = ("./datos.txt")
Dim sw As StreamWriter
Dim linea As String
Dim fs As FileStream = Nothing
sw = File.AppendText(archivo_escritura)
With DgvDatos
For I = 0 To .RowCount - 2
linea = .Rows(I).Cells("Column1").Value & ";" &
.Rows(I).Cells("Column2").Value & ";" &
.Rows(I).Cells("Column3").Value & ";" &
.Rows(I).Cells("Column4").Value & ";" &
.Rows(I).Cells("Column5").Value & ";" &
.Rows(I).Cells("Column6").Value & ";"
sw.WriteLine(linea)
Next
MsgBox("Guardado Ok")
End With
sw.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
leerdatos()
txt1.Text = DateTimePicker1.Value.Date
Txt4.Text = 1
End Sub
Private Sub btlexe_Click(sender As Object, e As EventArgs) Handles btlexe.Click
Dim obj As New Process
obj.Start("C:\Users\jlsch\source\repos\WinFormsSumar\WinFormsSumar\bin\Debug\net6.0-windows\WinFormsSumar.exe", AppWinStyle.MaximizedFocus)
End Sub
Private Sub btlAcumula_Click(sender As Object, e As EventArgs) Handles btlAcumula.Click
Dim tipo As String
Dim nrAl As Double = 0
Dim nrPe As Double = 0
Dim nrGa As Double = 0
Dim nrFa As Double = 0
Dim nrTe As Double = 0
Dim nrOc As Double = 0
Dim nrVa As Double = 0 REM añadido nuevo acumulado
Dim nrRe As Double = 0
Dim filas As Integer
DGView.Rows.Clear() REM borra la fila del DGView para que deje entrar nuevos datos al recalcular
filas = ((DgvDatos.RowCount.ToString()) - 1)
REM MsgBox("Nr. filas=" & filas)
For I = 0 To filas
tipo = (DgvDatos.Item(2, I).Value)
If tipo = "Al" Then nrAl = (DgvDatos.Item(4, I).Value) + nrAl
If tipo = "Pe" Then nrPe = (DgvDatos.Item(4, I).Value) + nrPe
If tipo = "Ga" Then nrGa = (DgvDatos.Item(4, I).Value) + nrGa
If tipo = "Fa" Then nrFa = (DgvDatos.Item(4, I).Value) + nrFa
If tipo = "Te" Then nrTe = (DgvDatos.Item(4, I).Value) + nrTe
If tipo = "Oc" Then nrOc = (DgvDatos.Item(4, I).Value) + nrOc
If tipo = "Va" Then nrVa = (DgvDatos.Item(4, I).Value) + nrVa
If tipo = "Re" Then nrRe = (DgvDatos.Item(4, I).Value) + nrRe REM añadido nuevo acumulado, revisa el tipo
Next
Const val As String = "##,####.##"
DGView.Rows.Add(Format(nrAl, val), Format(nrPe, val), Format(nrGa, val), Format(nrFa, val), Format(nrTe, val), Format(nrOc, val), Format(nrVa, val), Format(nrRe, val)) REM añadido nuevo acumulado, tambien en el DGView
End Sub
Private Sub DGView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGView.CellClick
Dim nrAl, nrPe, nrGa, nrFa, nrTe, nrOc, nrVa, nrRe As Double REM añadido nuevo acumulado
FilaActual = DGView.CurrentRow.Index
nrAl = DGView.Rows(FilaActual).Cells("Column7").Value
nrPe = DGView.Rows(FilaActual).Cells("Column8").Value
nrGa = DGView.Rows(FilaActual).Cells("Column9").Value
nrFa = DGView.Rows(FilaActual).Cells("Column10").Value
nrTe = DGView.Rows(FilaActual).Cells("Column11").Value
nrOc = DGView.Rows(FilaActual).Cells("Column12").Value
nrVa = DGView.Rows(FilaActual).Cells("Column13").Value
nrRe = DGView.Rows(FilaActual).Cells("Column14").Value REM añadido nuevo acumulado, cambiar tambien nr. Columna (ver su nºen el Grid)
End Sub
Private Sub BtlNote_Click(sender As Object, e As EventArgs) Handles BtlNote.Click
Dim obj As New Process
obj.Start("C:\Users\jlsch\source\repos\WinFormsBlockNota\WinFormsBlockNota\bin\Debug\net6.0-windows\WinFormsBlockNota.exe", AppWinStyle.MaximizedFocus)
End Sub
End Class