Public Class Form1

    Public Function CalculaNumeroRomano(ByVal ElNumero As Integer) As String
        CalculaNumeroRomano = ""

        Dim Resultado As String = ""
        If ElNumero < 1000000 Then
            Dim Texto As String = "" & ElNumero
            Dim TextoFijo As String = "" & ElNumero
            Dim x As Integer = 1
            For x = 1 To Strings.Len(TextoFijo) Step 1
                Dim Caracter As String = Strings.Right(Texto, 1)
                Resultado = DevuelveNumeros(x, CInt(Caracter)) & Resultado
                Texto = Strings.Left(TextoFijo, Strings.Len(TextoFijo) - x)
            Next
        End If

        Return Resultado

    End Function

    Public Function DevuelveNumeros(ByVal Posicion As Integer, ByVal Numero As Integer) As String
        DevuelveNumeros = ""
        Select Case Posicion
            Case 1
                Select Case Numero
                    Case 0
                        Return ""
                    Case 1
                        Return "I"
                    Case 2
                        Return "II"
                    Case 3
                        Return "III"
                    Case 4
                        Return "IV"
                    Case 5
                        Return "V"
                    Case 6
                        Return "VI"
                    Case 7
                        Return "VII"
                    Case 8
                        Return "VIII"
                    Case 9
                        Return "IX"
                End Select
            Case 2
                Select Case Numero
                    Case 0
                        Return ""
                    Case 1
                        Return "X"
                    Case 2
                        Return "XX"
                    Case 3
                        Return "XXX"
                    Case 4
                        Return "XL"
                    Case 5
                        Return "L"
                    Case 6
                        Return "LX"
                    Case 7
                        Return "LXX"
                    Case 8
                        Return "LXXX"
                    Case 9
                        Return "XC"

                End Select
            Case 3
                Select Case Numero
                    Case 0
                        Return ""
                    Case 1
                        Return "C"
                    Case 2
                        Return "CC"
                    Case 3
                        Return "CCC"
                    Case 4
                        Return "CD"
                    Case 5
                        Return "D"
                    Case 6
                        Return "DC"
                    Case 7
                        Return "DCC"
                    Case 8
                        Return "DCCC"
                    Case 9
                        Return "CM"
                End Select
            Case 4
                Select Case Numero
                    Case 0
                        Return ""
                    Case 1
                        Return "M"
                    Case 2
                        Return "MM"
                    Case 3
                        Return "MMM"
                    Case 4
                        Return "M<u>V</u>"
                    Case 5
                        Return "<u>V</u>"
                    Case 6
                        Return "<u>V</u>M"
                    Case 7
                        Return "<u>V</u>MM"
                    Case 8
                        Return "<u>V</u>MMM"
                    Case 9
                        Return "M<u>X</u>"

                End Select

            Case 5
                Select Case Numero
                    Case 0
                        Return ""
                    Case 1
                        Return "<u>X</u>"
                    Case 2
                        Return "<u>X</u><u>X</u>"
                    Case 3
                        Return "<u>X</u><u>X</u><u>X</u>"
                    Case 4
                        Return "<u>X</u><u>L</u>"
                    Case 5
                        Return "<u>L</u>"
                    Case 6
                        Return "<u>L</u><u>X</u>"
                    Case 7
                        Return "<u>L</u><u>X</u><u>X</u>"
                    Case 8
                        Return "<u>L</u><u>X</u><u>X</u><u>X</u>"
                    Case 9
                        Return "<u>X</u><u>C</u>"
                End Select
            Case 6
                Select Case Numero
                    Case 0
                        Return ""
                    Case 1
                        Return "<u>C</u>"
                    Case 2
                        Return "<u>C</u><u>C</u>"
                    Case 3
                        Return "<u>C</u><u>C</u><u>C</u>"
                    Case 4
                        Return "<u>C</u><u>D</u>"
                    Case 5
                        Return "<u>D</u>"
                    Case 6
                        Return "<u>D</u><u>C</u>"
                    Case 7
                        Return "<u>D</u><u>C</u><u>C</u>"
                    Case 8
                        Return "<u>D</u><u>C</u><u>C</u><u>C</u>"
                    Case 9
                        Return "<u>C</u><u>M</u>"
                End Select
        End Select
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        On Error Resume Next

        ' Cargamos el Formulario de Presentación
        Dim Cargalo As New Presentacion
        Cargalo.Cargalo()

        ' Y Ponemos la Versión en el Título del Formulario
        Me.Text = "Números Romanos " & Application.ProductVersion


    End Sub

    Private Sub InputTexto_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub InputNumero_KeyUp(sender As Object, e As KeyEventArgs) Handles InputNumero.KeyUp
        On Error Resume Next

        Dim ElNumero As Integer = CInt(InputNumero.Text)

        If Err.Number <> 0 Then Exit Sub

        Dim Resultado As String = CalculaNumeroRomano(ElNumero)

        ResultadoTexto.Text = Resultado

        TextoRico.Text = vbNullString

        Dim Texto As String = Resultado
        Dim TextoFijo As String = Texto
        Dim x As Integer = 1

        'TextoRico.SelectionFont = New Font(TextoRico.Font.FontFamily, TextoRico.Font.Size, FontStyle.Regular)

        Listado.Items.Clear()

        Dim ItemListado As Integer = 1
        For x = 1 To Strings.Len(Resultado) Step 1
            'ItemListado = ItemListado + 1
            Dim Caracteres As String = ""
            Dim Caracter As String = ""
            If Strings.Len(Texto) > 3 Then
                Caracteres = Strings.Left(Texto, 4)
                Caracter = Strings.Left(Texto, 1)
            Else
                Caracter = Strings.Left(Texto, 1)
            End If
            Dim TextoHastaAqui As String = TextoRico.Text

            If Strings.Left(Caracteres, 3) = "<u>" Then
                Listado.Items.Add(ItemListado)
                TextoRico.Text = TextoHastaAqui & Strings.Right(Caracteres, 1)
                Texto = Strings.Right(TextoFijo, Strings.Len(Texto) - 4)
                ItemListado = ItemListado + 1
            ElseIf Strings.Left(Caracteres, 4) = "</u>" Then
                Texto = Strings.Right(TextoFijo, Strings.Len(Texto) - 4)
            Else

                TextoRico.Text = TextoHastaAqui & Caracter
                If Strings.Len(Texto) <> 0 Then
                    Texto = Strings.Right(TextoFijo, Strings.Len(Texto) - 1)
                Else
                    Texto = vbNullString
                End If
                ItemListado = ItemListado + 1
            End If

        Next
        If Listado.items.count > 0 Then
            For x = 0 To Listado.Items.Count - 1 Step 1
                TextoRico.SelectionLength = 1
                TextoRico.SelectionStart = CInt(Listado.Items.Item(x)) - 1
                TextoRico.SelectionFont = New Font(TextoRico.Font.FontFamily, TextoRico.Font.Size, FontStyle.Underline)
            Next
        End If

    End Sub

    Private Sub InputNumero_TextChanged(sender As Object, e As EventArgs) Handles InputNumero.TextChanged

    End Sub

    Private Sub ResultadoTexto_TextChanged(sender As Object, e As EventArgs) Handles ResultadoTexto.TextChanged

    End Sub

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click

    End Sub

    Private Sub TextoRico_TextChanged(sender As Object, e As EventArgs) Handles TextoRico.TextChanged

    End Sub

    Private Sub InputRomano_TextChanged(sender As Object, e As EventArgs) Handles InputRomano.TextChanged

    End Sub

    Private Sub InputRomano_KeyUp(sender As Object, e As KeyEventArgs) Handles InputRomano.KeyUp


    End Sub

    Private Sub Consultar_Click(sender As Object, e As EventArgs) Handles Consultar.Click
        On Error Resume Next

        TextoRico.Text = vbNullString

        Dim X As Integer = 0

        For X = 1 To 999999 Step 1
            If CalculaNumeroRomano(X) = InputRomano.Text Then
                TextoRico.Text = X
                Exit For
            End If
        Next

    End Sub
End Class
