locked
how do i make comboboxes capitals only RRS feed

  • Question

  • Hi Good People

     how do I make comboboxes capital letters only

    Gary Simpson

    Thursday, January 30, 2014 5:43 PM

Answers

  • Gary,

    I assume that you mean the items in the ComboBox?

    If so, when you're adding them, use ".ToUpper" and it will add the uppercase of the item (assuming it's a string of course).


    Please call me Frank :)

    • Proposed as answer by IronRazerz Thursday, January 30, 2014 7:03 PM
    • Marked as answer by Carl Cai Tuesday, February 11, 2014 8:46 AM
    Thursday, January 30, 2014 5:46 PM
  • Gary,

    I assume that you mean the items in the ComboBox?

    If so, when you're adding them, use ".ToUpper" and it will add the uppercase of the item (assuming it's a string of course).


    Please call me Frank :)

    Hi Frank

    Yes the comboboxes I want any text typed into the comboboxes to be capitals only (Uppercase)

    and where do I find ToUpper? in the settings or do I have to write ToUpper into the code. under combobox enter or somthing like that.

    Kind regards

    Gary


    Gary Simpson

    Hello Gary,

    To compliment Frank's reply the following uses ToUpper in the KeyPress event of the ComboBox.

    Private Sub ComboBox1_KeyPress(
        ByVal sender As Object,
        ByVal e As KeyPressEventArgs) _
    Handles ComboBox1.KeyPress
    
        If Char.IsLower(e.KeyChar) Then
            e.KeyChar = Char.ToUpper(e.KeyChar)
        End If
    
    End Sub

    EDIT

    Come to think of it you could create a custom ComboBox that a) does the above b) works with pasting from the Windows Clipboard.

    Imports System.ComponentModel
    Imports System.Collections.ObjectModel
    Public Class ComboboxUpperCase
        Inherits ComboBox
        Private Sub CheckedCombobox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
            If Char.IsLower(e.KeyChar) Then
                e.KeyChar = Char.ToUpper(e.KeyChar)
            End If
        End Sub
        ''' <summary>
        ''' Capture text from Clipboard
        ''' </summary>
        ''' <param name="m"></param>
        ''' <remarks></remarks>
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = 8465 Then
                Dim Value As String = Me.Text
                Me.Text = Me.Text.ToUpper
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    Dang, in the class above when pasting from the clipboard the cursor is set to the start but should (at least for me) be at the end or select all.

    So I have set the cursor to the last character and added a property to control casing for normal, lower or upper.

    Imports System.ComponentModel
    Imports System.Collections.ObjectModel
    Public Enum CharCasing
        Normal
        Upper
        Lower
    End Enum
    ''' <summary>
    ''' a-z entered is forced to upper case.
    ''' Text pasted from the Clipboard is converted to upper case.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ComboboxCasing
        Inherits ComboBox
    
        <Category("Behavior"), _
        Description("Character casing"), _
        DefaultValue(CharCasing.Normal)> _
        Public Property CharCasing As CharCasing
        Private Sub CheckedCombobox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
            If CharCasing = CharCasing.Lower Then
                e.KeyChar = Char.ToLower(e.KeyChar)
            ElseIf CharCasing = CharCasing.Upper Then
                e.KeyChar = Char.ToUpper(e.KeyChar)
            End If
        End Sub
        ''' <summary>
        ''' Capture text from Clipboard
        ''' </summary>
        ''' <param name="m"></param>
        ''' <remarks></remarks>
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = 8465 Then
                Dim Value As String = Me.Text
                If CharCasing = CharCasing.Lower Then
                    Me.Text = Me.Text.ToLower
                    Me.Select(Me.Text.Length, Me.Text.Length)
                ElseIf CharCasing = CharCasing.Upper Then
                    Me.Text = Me.Text.ToUpper
                    Me.Select(Me.Text.Length, Me.Text.Length)
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.



    • Proposed as answer by IronRazerz Sunday, February 23, 2014 2:07 PM
    • Edited by KareninstructorMVP Sunday, February 23, 2014 2:45 PM Added revised control
    • Marked as answer by Gary Simpson Sunday, February 23, 2014 9:02 PM
    Sunday, February 23, 2014 2:01 PM

All replies

  • Gary,

    I assume that you mean the items in the ComboBox?

    If so, when you're adding them, use ".ToUpper" and it will add the uppercase of the item (assuming it's a string of course).


    Please call me Frank :)

    • Proposed as answer by IronRazerz Thursday, January 30, 2014 7:03 PM
    • Marked as answer by Carl Cai Tuesday, February 11, 2014 8:46 AM
    Thursday, January 30, 2014 5:46 PM
  • Set the font as Copperplate Gothic Light. The following code queries the installed fonts on the user's machine.

    Dim T as Font

    Dim F as Boolean

            F = False ' provide alternate font
            For Each FF As FontFamily In FontFamily.Families
                If FF.Name.ToLower = "copperplate gothic light" Then F = True
            Next
            If F Then
                T = New Font("copperplate gothic light", 16, FontStyle.Regular, GraphicsUnit.Pixel)
            Else
                T = New Font("times new roman", 16, FontStyle.Regular, GraphicsUnit.Pixel)
            End If
            ComboBox1.Font = T


    Leon C Stanley - - A dinky di VBer - - Code is like fresh baked bread - it goes stale after a while - - so why try to immortalize it ? and turn it into a Class? ^^ :-)

    Friday, January 31, 2014 7:38 AM
  • Hi Frank

    Sorry I have had Trouble Answering you i was using VS10 and I could not reply to you it was really frustrating for me. in the end i downloaded VS 2013 express and now i can answer you. 

    Sorry Gary


    Gary Simpson

    Saturday, February 22, 2014 8:13 PM
  • Hi Frank

    Sorry I have had Trouble Answering you i was using VS10 and I could not reply to you it was really frustrating for me. in the end i downloaded VS 2013 express and now i can answer you. 

    Sorry Gary


    Gary Simpson

    No apology needed.

    Did what I suggest help or did I misunderstand all along?


    Please call me Frank :)

    Saturday, February 22, 2014 8:15 PM
  • Gary,

    I assume that you mean the items in the ComboBox?

    If so, when you're adding them, use ".ToUpper" and it will add the uppercase of the item (assuming it's a string of course).


    Please call me Frank :)

    Hi Frank

    Yes the comboboxes I want any text typed into the comboboxes to be capitals only (Uppercase)

    and where do I find ToUpper? in the settings or do I have to write ToUpper into the code. under combobox enter or somthing like that.

    Kind regards

    Gary


    Gary Simpson

    Sunday, February 23, 2014 1:37 PM
  • Hi Frank

    Yes the comboboxes I want any text typed into the comboboxes to be capitals only (Uppercase)

    and where do I find ToUpper? in the settings or do I have to write ToUpper into the code. under combobox enter or somthing like that.

    What I meant was in reference to the items collection itself.

    If you're adding them programattically, then using the string's .ToUpper when adding them will result in the item collection containing all upper-case strings.

    Depending on the drop-down style and whether or not you've set up AutoComplete, then the user cannot select something which isn't all upper-case.

    How are you adding the items to the ComboBox? Maybe that's a good starting point here.


    Please call me Frank :)

    Sunday, February 23, 2014 1:45 PM
  • Gary,

    I assume that you mean the items in the ComboBox?

    If so, when you're adding them, use ".ToUpper" and it will add the uppercase of the item (assuming it's a string of course).


    Please call me Frank :)

    Hi Frank

    Yes the comboboxes I want any text typed into the comboboxes to be capitals only (Uppercase)

    and where do I find ToUpper? in the settings or do I have to write ToUpper into the code. under combobox enter or somthing like that.

    Kind regards

    Gary


    Gary Simpson

    Hello Gary,

    To compliment Frank's reply the following uses ToUpper in the KeyPress event of the ComboBox.

    Private Sub ComboBox1_KeyPress(
        ByVal sender As Object,
        ByVal e As KeyPressEventArgs) _
    Handles ComboBox1.KeyPress
    
        If Char.IsLower(e.KeyChar) Then
            e.KeyChar = Char.ToUpper(e.KeyChar)
        End If
    
    End Sub

    EDIT

    Come to think of it you could create a custom ComboBox that a) does the above b) works with pasting from the Windows Clipboard.

    Imports System.ComponentModel
    Imports System.Collections.ObjectModel
    Public Class ComboboxUpperCase
        Inherits ComboBox
        Private Sub CheckedCombobox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
            If Char.IsLower(e.KeyChar) Then
                e.KeyChar = Char.ToUpper(e.KeyChar)
            End If
        End Sub
        ''' <summary>
        ''' Capture text from Clipboard
        ''' </summary>
        ''' <param name="m"></param>
        ''' <remarks></remarks>
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = 8465 Then
                Dim Value As String = Me.Text
                Me.Text = Me.Text.ToUpper
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    Dang, in the class above when pasting from the clipboard the cursor is set to the start but should (at least for me) be at the end or select all.

    So I have set the cursor to the last character and added a property to control casing for normal, lower or upper.

    Imports System.ComponentModel
    Imports System.Collections.ObjectModel
    Public Enum CharCasing
        Normal
        Upper
        Lower
    End Enum
    ''' <summary>
    ''' a-z entered is forced to upper case.
    ''' Text pasted from the Clipboard is converted to upper case.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ComboboxCasing
        Inherits ComboBox
    
        <Category("Behavior"), _
        Description("Character casing"), _
        DefaultValue(CharCasing.Normal)> _
        Public Property CharCasing As CharCasing
        Private Sub CheckedCombobox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
            If CharCasing = CharCasing.Lower Then
                e.KeyChar = Char.ToLower(e.KeyChar)
            ElseIf CharCasing = CharCasing.Upper Then
                e.KeyChar = Char.ToUpper(e.KeyChar)
            End If
        End Sub
        ''' <summary>
        ''' Capture text from Clipboard
        ''' </summary>
        ''' <param name="m"></param>
        ''' <remarks></remarks>
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = 8465 Then
                Dim Value As String = Me.Text
                If CharCasing = CharCasing.Lower Then
                    Me.Text = Me.Text.ToLower
                    Me.Select(Me.Text.Length, Me.Text.Length)
                ElseIf CharCasing = CharCasing.Upper Then
                    Me.Text = Me.Text.ToUpper
                    Me.Select(Me.Text.Length, Me.Text.Length)
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.



    • Proposed as answer by IronRazerz Sunday, February 23, 2014 2:07 PM
    • Edited by KareninstructorMVP Sunday, February 23, 2014 2:45 PM Added revised control
    • Marked as answer by Gary Simpson Sunday, February 23, 2014 9:02 PM
    Sunday, February 23, 2014 2:01 PM