jueves, 25 de octubre de 2018

Nombre de las columnas DataRow

For Each dr As DataRow In ds.Tables(0).Rows
   MessageBox.Show(dr.Table.Columns(0).ColumnName)
Next


' Verificar si existen registros
If ds.Tables(0).Rows.Count > 0 Then
End If

martes, 23 de octubre de 2018

Asignar web service dinámico

ws.Url = "url...wsdl"

lunes, 22 de octubre de 2018

Crear archivo

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"10,4,3,33",
                                  "10,10,10,2018-12-12",
                                  "2,8,20,venta al x mayor",
                                  "5,4,13,otra línea",
                                  "7,1,5,razón social",
                                  "8,7,7, dirección",
                                  "1,9,12,correo electrónico"}

        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i)
                Next


                For j As Integer = 0 To columna - 1
                    linea = linea & " "
                Next

                linea = linea & tmp_dato

           


                txt(x - 1) = linea
            Else

                'Dim largo_linea As Integer = linea.Length
                Dim existe As Boolean = False
                For i As Integer = 0 To linea.Length - 1
                    If i = columna Then
                        existe = True
                    End If
                Next

                If existe = False Then

                End If
                Dim pause As String = ""
            End If
        Next
        txt.Clear()
    End Sub
End Class


versión2
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"1,1,5,33",
                                  "2,8,7,98545456",
                                  "3,2,10,2018-10-22",
                                  "5,4,13,otra línea",
                                  "7,1,5,razón social",
                                  "8,7,7,dirección",
                                  "9,9,21,correo electrónico",
                                  "14,56,100,esto es una prueba",
                                  "100,1,4,fin"}

        ' "9,91,20,correo electrónico",


        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i) ' parte de 1,2...
                Next


                If columna = 1 Then
                    linea = linea & ""
                Else
                    For j As Integer = 0 To columna - 1
                        linea = linea & " "
                    Next

                End If


                Dim tmp As String = ""
                Dim k As Integer = 0
                If columna = 1 Then
                    tmp = linea & tmp_dato
                Else
                    For Each c In linea
                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                End If


                'Dim bytes As Byte() = System.Text.Encoding.Default.GetBytes(tmp)
                'tmp = System.Text.Encoding.UTF8.GetString(bytes)

                'tmp = Encoding.GetEncoding("ISO-8859-1").GetString(Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("ISO-8859-1"), Encoding.UTF8.GetBytes(tmp)))

                Dim n1 As Integer = tmp.Length
                n1 = New System.Globalization.StringInfo(tmp).LengthInTextElements







                'linea = linea & tmp_dato
                linea = tmp
                txt(x - 1) = linea
                linea = ""
            Else
                linea = txt(x - 1)
                Dim total As Integer = columna + largo
                Dim largo_linea As Integer = linea.Length

                If largo_linea < total Then
                    For i As Integer = largo_linea To total - 1
                        linea = linea & " "
                    Next

                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next
                    ' Agregar dato
                    Dim tmp As String = Nothing
                    Dim k As Integer = 0
                    For Each c In linea

                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                    txt(x - 1) = tmp

                End If

                linea = ""


                Dim pause As String = ""




            End If
        Next
        ' archivo
        System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.UTF8)
        'System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding("ISO-8859-1"))
        txt.Clear()

        MessageBox.Show("Fin!")

    End Sub
    'https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c
    'https://stackoverflow.com/questions/11055122/counting-special-utf-8-character
End Class

versión 3
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"1,1,20,TipoDTE:33",
                                  "2,1,10,Folio  :5778",
                                  "3,1,20,FchEmis:2018-10-22",
                                  "4,1,20,FchVenc:2018-10-22",
                                  "5,4,13,otra línea",
                                  "7,1,5,razón social",
                                  "8,7,7,dirección",
                                  "9,9,21,correo electrónico",
        "10,34,10,******",
        "11,56,100,esto es una prueba",
        "12,36,45,juan perez",
        "13,1,4,fin",
        "14,105,10,Descuento",
        "14,119,10,precio",
                                  "15,23,14,."}

        '(fila,columna,largo,dato,True/False,)
        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i) ' parte de 1,2...
                Next


                If columna = 1 Then
                    linea = linea & ""
                Else
                    For j As Integer = 0 To columna - 1
                        linea = linea & " "
                    Next

                End If


                Dim tmp As String = ""
                Dim k As Integer = 0
                If columna = 1 Then
                    tmp = linea & tmp_dato
                Else
                    For Each c In linea
                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                End If
                'linea = linea & tmp_dato
                linea = tmp
                txt(x - 1) = linea
                linea = ""
            Else
                linea = txt(x - 1)
                Dim total As Integer = columna + largo
                Dim largo_linea As Integer = linea.Length

                If largo_linea < total Then
                    For i As Integer = largo_linea To total - 1
                        linea = linea & " "
                    Next

                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next
                    ' Agregar dato
                    Dim tmp As String = Nothing
                    Dim k As Integer = 0
                    For Each c In linea

                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                    txt(x - 1) = tmp

                End If

                linea = ""
                Dim pause As String = ""
            End If
        Next
        ' archivo
        System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding(1252))
        'System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding("ISO-8859-1"))
        txt.Clear()

        MessageBox.Show("Fin!")

    End Sub
    'https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c
    'https://stackoverflow.com/questions/11055122/counting-special-utf-8-character
End Class

versión4
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim txt As New List(Of String)

        Dim config() As String = {"1,30,10,TipoDTE:33",
                                  "2,5,20,Folio  :5778",
                                  "1,1,20,FchEmis:2018-10-22",
                                  "4,108,40,hola",
                                  "4,98,10,chao",
                                  "4,1,97,."}
        '"4,1,20,FchVenc:2018-10-22",
        '"5,4,13,otra línea",
        '"7,1,5,razón social",
        '"8,7,7,dirección",
        '"9,9,21,correo electrónico",
        '"10,34,10,******",
        '"11,56,100,esto es una prueba",
        '"12,36,45,juan perez",
        '"13,1,4,fin",
        '"14,105,10,Descuento",
        '"14,119,10,precio",
        '"15,23,14,."}

        '(fila,columna,largo,dato,True/False,)
        Dim linea As String = ""
        Dim fila As Integer = 0
        Dim columna As Integer = 0
        Dim largo As Integer = 0
        Dim dato As String

        For Each x In config
            fila = x.Split(",")(0)
            columna = x.Split(",")(1)
            largo = x.Split(",")(2)
            dato = x.Split(",")(3)

            x = x.Split(",")(0)

            ' Crear línea
            Dim n As Integer = txt.Count
            For i As Integer = n To x - 1
                txt.Add("")
            Next


            ' Crear dato
            Dim c As Char = Nothing
            Dim tmp_dato As String = ""
            If txt(x - 1) = "" Then

                dato = dato.PadRight(largo)

                For i As Integer = 0 To largo - 1
                    tmp_dato = tmp_dato & dato(i) ' parte de 1,2...
                Next


                If columna = 1 Then
                    linea = linea & ""
                Else
                    For j As Integer = 0 To columna - 1
                        linea = linea & " "
                    Next

                End If


                Dim tmp As String = ""
                Dim k As Integer = 0
                If columna = 1 Then
                    tmp = linea & tmp_dato
                Else
                    For Each c In linea
                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                End If
                'linea = linea & tmp_dato
                linea = tmp
                txt(x - 1) = linea
                linea = ""
            Else
                linea = txt(x - 1)
                Dim total As Integer = columna + largo
                Dim largo_linea As Integer = linea.Length

                If largo_linea < total Then
                    For i As Integer = largo_linea To total - 1
                        linea = linea & " "
                    Next

                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next
                    ' Agregar dato
                    Dim tmp As String = Nothing
                    Dim k As Integer = 0
                    For Each c In linea

                        If k = columna - 1 Then
                            tmp = tmp & tmp_dato
                            Exit For
                        Else
                            tmp = tmp & c
                        End If
                        k += 1
                    Next
                    txt(x - 1) = tmp
                Else
                    ' ya existe una línea con suficiente espacio
                    dato = dato.PadRight(largo)
                    For i As Integer = 0 To largo - 1
                        tmp_dato = tmp_dato & dato(i)
                    Next

                    Dim tmp As String = Nothing
                    'Dim k As Integer = 0
                    'Dim j As Integer = 0
                    Dim z As Integer = 0
                    Dim tmp_linea() As Char = linea.ToCharArray
                    For i As Integer = 0 To linea.Length - 1

                        If i = columna - 1 Then

                            For j As Integer = i To i + largo - 1
                                tmp_linea(j) = tmp_dato(z)
                                z += 1
                            Next
                        End If
                        If z = largo Then
                            Exit For
                        End If


                    Next
                    txt(x - 1) = New String(tmp_linea)

                    Dim pause2 As String = ""

                End If

                linea = ""
                Dim pause As String = ""
                End If
        Next
        ' archivo
        System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding(1252))
        'System.IO.File.WriteAllLines("archivo.txt", txt.ToArray, System.Text.Encoding.GetEncoding("ISO-8859-1"))
        txt.Clear()

        MessageBox.Show("Fin!")

    End Sub
    'https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c
    'https://stackoverflow.com/questions/11055122/counting-special-utf-8-character
End Class

viernes, 19 de octubre de 2018

ordenar lista de tuplas


Versión 1
Dim elem As New List(Of String)
Dim a As Integer
        Dim b As Integer
        Dim temp As String
        Dim c As Integer = -1
        For i As Integer = 0 To elem.Count - 1
            c += 1
            For j As Integer = c To elem.Count - 1
                a = elem(i).Split(",")(0)
                b = elem(j).Split(",")(0)

                If b < a Then
                    temp = elem(i)
                    elem(i) = elem(j)
                    elem(j) = temp
                End If
            Next
        Next
elem.Clear()

Versión 2
Dim elem As New List(Of String)
 Dim sep As Char = ","
        Dim a As Integer
        Dim b As Integer
        Dim temp As String
        Dim c As Integer = -1
        For i As Integer = 0 To elem.Count - 1
            c += 1
            For j As Integer = c To elem.Count - 1
                a = elem(i).Split(sep)(0)
                b = elem(j).Split(sep)(0)

                If b < a Then
                    temp = elem(i)
                    elem(i) = elem(j)
                    elem(j) = temp
                ElseIf b = a Then
                    a = elem(i).Split(sep)(1)
                    b = elem(j).Split(sep)(1)
                    If b < a Then
                        temp = elem(i)
                        elem(i) = elem(j)
                        elem(j) = temp
                    End If

                End If
            Next
        Next
    elem.Clear()

opciones datagrid

Me.DataGridView.Rows.Clear()
Me.DataGridView..Columns.Clear()
Me.DataGridView..Refresh()

miércoles, 17 de octubre de 2018

Certificación regex Casos

 If System.Text.RegularExpressions.Regex.IsMatch(linea, "^((CASO[ ]{1,})\d+-1)+$") Then
 End If


Dim x As String = "^(CASO[ ]{1,}" & numero_de_atencion & "-1)" & "+$"


fuente

martes, 16 de octubre de 2018

sql conexión

Try
Dim query As String =""
Dim con As New SqlConnection(conex)

con.Open
Using cmd As New SqlCommand(query, con)
                    cmd.Parameters.Add("@id", SqlDbType.Char).Value = ""
                    cmd.ExecuteNonQuery()
End Using
con.Close

Catch ex As Exception
End Try

martes, 9 de octubre de 2018

Resetear combobox

ComboBox.ResetText()

Repartir folios

Private Sub btnRepartirFolios_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRepartirFolios.Click

        Dim lista As New ArrayList

        Dim folios As Integer = 76
        Dim porcentaje As Integer

        Dim sucursales() As String = File.ReadAllLines("suc.txt")
        Dim cantidad_sucursales As Integer = sucursales.Length

        Dim restante As Integer = folios
        Dim total_folios As Integer
        Dim p As Integer
        For Each x In sucursales
            p = x.Split(";")(0)

           porcentaje = Math.Truncate(folios * (p / 100))

            If p > 0 AndAlso porcentaje = 0 Then
                porcentaje = 1
            End If
            restante = restante - porcentaje
            lista.Add(porcentaje)
            total_folios = total_folios + porcentaje
            porcentaje = 0
        Next
        If restante >= 0 Then
            While restante > 0
                restante -= 1
            End While
        End If
        lista.Sort()
        lista.Reverse()
        Dim pause As String = ""
    End Sub

miércoles, 3 de octubre de 2018

Dejar aplicación en administrador de tareas

 Imports System.Reflection

Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean


Public Shared Sub HideWindow()
        Dim SW_HIDE As Integer = 0
        For Each pr As Process In Process.GetProcesses
            If pr.ProcessName.Contains("AcroRd32") Then
                'Int32 hWnd = pr.MainWindowHandle.ToInt32();
                ShowWindow(pr.MainWindowHandle, SW_HIDE)
            End If

        Next
    End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      HideWindow()
 End Sub

fuente