Jump to content

Text Box 1ª letra maiúscula


esquima

Recommended Posts

Usas uma masked text box e resolves isso fácil ou utilizas o evento keypress ou textchanged da textbox.

A máscara será MaskedTextBox1.Mask = ">L#######" (o uso do '>' faz com que a primeira letra seja convertida em maiúscula! e L -> obriga a que o primeiro caracter seja uma letra)

No caso de ser como evento deve ser relativamente simples (não testei e foi escrito na hora)

   Private Sub xisUp(ByVal xxxX) Handles TextBox1.TextChanged

      TextBox1.Text.Chars(0) = TextBox1.Text.Chars(0).ToUpper

   End Sub

Espero ter ajudado! 1abraço

Link to comment
Share on other sites

Ora bem, estive a rever a cena e o que consegui foi isto:

 Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp, TextBox2.KeyUp
      Dim txb As New TextBox
      txb = sender
      Try
' se é um caractere
         If e.KeyValue > 64 And e.KeyValue < 91 Then
            If txb.Text.Length = 1 Then
               ' é a primeira letra da TextBox
               txb.Text = Char.ToUpper(txb.Text.Chars(0))
               ' coloca o cursor na ultima posição da textbox
               txb.Select(txb.TextLength, 1)
            Else
               ' não é a primeira letra mas podemos sempre alterar a primeira letra
               ' converter a string
               Dim s As String = ""
               ' só o primeiro caracter
               s = Char.ToUpper(txb.Text.Chars(0))
               ' texto convertido
               txb.Text = s + txb.Text.Substring(1).ToLower ' o to lower faz com que as restantes letras sejam minúsculas também
                ' coloca o cursor na ultima posição da textbox
                txb.Select(txb.TextLength, 1)
            End If
         End If
      Catch ex As Exception

      End Try

   End Sub

Espero que resolva. Há uma pequena latência também entre o largar da tecla (uma vez que o evento está associado ao keyUp...) e a conversão!

Quanto a usares para várias textbox em simultâneo é uma questão de olhares para o cabeçalho da função (textbox1.keyup, textbox999.keyup - funciona na txt box 1 e txt box 999)!

😄

1abraço

Link to comment
Share on other sites

morsa, um bocadinho confuso esse código não ?

Pode-se capitalizar as textboxes depois de clicar num butão ?

Se sim faz assim, é mais rápido e simples e apanha todas as textboxes sem mais códigos:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each c As Control In Me.Controls
            If TypeOf c Is TextBox Then
                c.Text = c.Text.Substring(0, 1).ToUpper & c.Text.Substring(1)
            End If
        Next
    End Sub

Knowledge to the masses


Link to comment
Share on other sites

Podes fazer

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus

        If (TextBox1.TextLength > 0) Then

            TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper & TextBox1.Text.Substring(1)

        End If

    End Sub

Mas mostra lá esse código pode ser que se dê um jeito 😄

Knowledge to the masses


Link to comment
Share on other sites

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _    Handles TextBox1.TextChanged      
   Dim selStart As Integer        
   Dim selLength As Integer      
   selStart = Me.TextBox1.SelectionStart       
   selLength = Me.TextBox1.SelectionLength      
   Me.TextBox1.Text = StrConv(Me.TextBox1.Text, VbStrConv.ProperCase)      
   Me.TextBox1.SelectionStart = selStart       
  Me.TextBox1.SelectionLength = selLength 
End Sub 


Link to comment
Share on other sites

O código da morsa pode ser perfeitamente aplicado no teu evento TetxChanged da TextBox... Ou tem de ser mesmo do tipo Integer??

No meu programa funcionou perfeitamente.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles TextBox1.TextChanged, TextBox2.TextChanged
        

        Dim txb As New TextBox
        txb = sender

        Dim s As String = ""
                   
        s = Char.ToUpper(txb.Text.Chars(0))
                      
        txb.Text = s + txb.Text.Substring(1).ToLower
                 
        
        txb.Select(txb.TextLength, 1)

    End Sub

Tal como ela diz, na mesma linha de código onde se encontra o evento TextChanged, podes escrever o nome de TODAS as TextBoxes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.