Results 1 to 18 of 18

Thread: NumericUpDown with letters

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    NumericUpDown with letters

    I'm keeping track of some things that are numbered and others that are lettered; while NumericUpDown is perfect for selecting numbered things, I need an analogue for selecting lettered things.

    The obvious choice would be a listbox that just contains all the letters of the alphabet. But I need to show only the appropriate range of letters, so if I want to use a listbox, I need to write a function that populates the listbox with the first n letters of the alphabet; if n > 26, it should continue with aa, ab, and so on. How do I do this?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    here's a custom userControl. it ranges from A to ZZZ:
    Attached Files Attached Files

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    it's just bare bones, but you could add properties to expose the numericUpDown value, min + max, etc + the readonly textbox text

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: NumericUpDown with letters

    Quote Originally Posted by .paul. View Post
    here's a custom userControl. it ranges from A to ZZZ:
    Interesting!

    Loaded it into VB10 and it says...
    Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable.


    EDIT Changed it like this and Warnings went away.

    Code:
    For a As Integer = 0 To 25
        Dim aa As Integer = a
        Dim alphabet2Chars() As String = Enumerable.Range(0, 26).Select(Function(x) alphabet(aa) & Chr(65 + x)).ToArray
        alphabetString = alphabetString.Concat(alphabet2Chars).ToArray
    Next
    For a1 As Integer = 0 To 25
        For a2 As Integer = 0 To 25
            Dim aa1 As Integer = a1
            Dim aa2 As Integer = a2
            Dim alphabet3Chars() As String = Enumerable.Range(0, 26).Select(Function(x1) alphabet(aa1) & alphabet(aa2) & Chr(65 + x1)).ToArray
            alphabetString = alphabetString.Concat(alphabet3Chars).ToArray
        Next
    Next
    Last edited by Edgemeal; Apr 2nd, 2012 at 05:40 PM. Reason: remove image

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    it'll run. that's a warning, because lambdas work in mysterious ways.
    you can fix the warning:

    vb Code:
    1. For a As Integer = 0 To 25
    2.     dim loopVariable as integer = a
    3.     Dim alphabet2Chars() As String = Enumerable.Range(0, 26).Select(Function(x) alphabet(loopVariable) & Chr(65 + x)).ToArray
    4.     alphabetString = alphabetString.Concat(alphabet2Chars).ToArray
    5. Next
    6. For a1 As Integer = 0 To 25
    7.     For a2 As Integer = 0 To 25
    8.         dim loopVariable1 as integer = a1
    9.         dim loopVariable2 as integer = a2
    10.         Dim alphabet3Chars() As String = Enumerable.Range(0, 26).Select(Function(x1) alphabet(loopVariable1) & alphabet(loopVariable2) & Chr(65 + x1)).ToArray
    11.         alphabetString = alphabetString.Concat(alphabet3Chars).ToArray
    12.     Next
    13. Next

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: NumericUpDown with letters

    Quote Originally Posted by .paul. View Post
    it'll run. that's a warning, because lambdas work in mysterious ways.
    you can fix the warning:
    Ya just tried that before your reply (edited post) and warnings went away. Thanks for the feedback!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: NumericUpDown with letters

    That looks neat, but I don't know how to use user controls; I am only a beginner

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: NumericUpDown with letters

    If the control is in a single file you can just import it into your project using the solution explorer on the right and compile. It should appear in the tool box after with a generic purple icon.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    you just add the userControl to your project:

    Project-->Add Existing Item

    then rebuild your project + you'll find the uc at the top of your toolbox + you can add it like any control.
    what i said about the properties:

    vb Code:
    1. Public Class alphabetUpDown
    2.  
    3.     Dim alphabetString() As String
    4.  
    5.     Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    6.         If alphabetString.Length = 0 Then Return
    7.         TextBox1.Text = alphabetString(CInt(NumericUpDown1.Value) - 1)
    8.     End Sub
    9.  
    10.     Public Sub New()
    11.  
    12.         Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    13.         alphabetString = Array.ConvertAll(alphabet, Function(c) c.ToString)
    14.  
    15.         For a As Integer = 0 To 25
    16.             Dim alphabet2Chars() As String = Enumerable.Range(0, 26).Select(Function(x) alphabet(a) & Chr(65 + x)).ToArray
    17.             alphabetString = alphabetString.Concat(alphabet2Chars).ToArray
    18.         Next
    19.         For a1 As Integer = 0 To 25
    20.             For a2 As Integer = 0 To 25
    21.                 Dim alphabet3Chars() As String = Enumerable.Range(0, 26).Select(Function(x1) alphabet(a1) & alphabet(a2) & Chr(65 + x1)).ToArray
    22.                 alphabetString = alphabetString.Concat(alphabet3Chars).ToArray
    23.             Next
    24.         Next
    25.  
    26.         ' This call is required by the Windows Form Designer.
    27.         InitializeComponent()
    28.  
    29.         ' Add any initialization after the InitializeComponent() call.
    30.  
    31.     End Sub
    32.  
    33.     Public Property Minimum() As Integer
    34.         Get
    35.             Return CInt(NumericUpDown1.Minimum)
    36.         End Get
    37.         Set(ByVal value As Integer)
    38.             If value > 0 And value <= 18278 And value < Maximum Then
    39.                 NumericUpDown1.Minimum = value
    40.             Else
    41.                 Throw New ArgumentOutOfRangeException("Minimum")
    42.             End If
    43.         End Set
    44.     End Property
    45.  
    46.     Public Property Maximum() As Integer
    47.         Get
    48.             Return CInt(NumericUpDown1.Maximum)
    49.         End Get
    50.         Set(ByVal value As Integer)
    51.             If value > 0 And value <= 18278 And value > Minimum Then
    52.                 NumericUpDown1.Maximum = value
    53.             Else
    54.                 Throw New ArgumentOutOfRangeException("Maximum")
    55.             End If
    56.         End Set
    57.     End Property
    58.  
    59.     Public Property Value() As Integer
    60.         Get
    61.             Return CInt(NumericUpDown1.Value)
    62.         End Get
    63.         Set(ByVal value As Integer)
    64.             If value >= Minimum And value <= Maximum Then
    65.                 NumericUpDown1.Value = value
    66.             Else
    67.                 Throw New ArgumentOutOfRangeException("Value")
    68.             End If
    69.         End Set
    70.     End Property
    71.  
    72.     Public ReadOnly Property ValueString() As String
    73.         Get
    74.             Return TextBox1.Text
    75.         End Get
    76.     End Property
    77.  
    78. End Class

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: NumericUpDown with letters

    Thanks, that will work great!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: NumericUpDown with letters

    Just use a DomainUpDown control and populate it with letters.
    vb.net Code:
    1. For i = Convert.ToInt32("A"c) To Convert.ToInt32("Z"c)
    2.     DomainUpDown1.Items.Add(Convert.ToChar(i))
    3. Next
    The DomainUpDown control is not in the Toolbox by default but can be added just like any other control.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: NumericUpDown with letters

    OK, I'd like to make one modification of the control Paul made: I'd like to have it start with a blank space as a "zero" value. How can I add that?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    just change this line:

    vb Code:
    1. Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray

    to this:

    vb Code:
    1. Dim alphabet() As Char = " abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: NumericUpDown with letters

    That messes up the 2-letter part: It goes (space), A, B, ...Z, (space)A, (space)B, ...(space)Z, AA, AB...
    Last edited by colleen.boye; Apr 4th, 2012 at 06:05 PM.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    this works:

    vb Code:
    1. Public Class alphabetUpDown
    2.  
    3.     Dim alphabetString() As String
    4.  
    5.     Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    6.         If alphabetString.Length = 0 Then Return
    7.         TextBox1.Text = alphabetString(CInt(NumericUpDown1.Value) - 1)
    8.     End Sub
    9.  
    10.     Public Sub New()
    11.  
    12.         Dim alphabet() As Char = " abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    13.         alphabetString = Array.ConvertAll(alphabet, Function(c) c.ToString)
    14.  
    15.         For a As Integer = 1 To 26
    16.             Dim loopVariable As Integer = a
    17.             Dim alphabet2Chars() As String = Enumerable.Range(0, 26).Select(Function(x) alphabet(loopVariable) & Chr(65 + x)).ToArray
    18.             alphabetString = alphabetString.Concat(alphabet2Chars).ToArray
    19.         Next
    20.         For a1 As Integer = 1 To 26
    21.             For a2 As Integer = 1 To 26
    22.                 Dim loopVariable1 As Integer = a1
    23.                 Dim loopVariable2 As Integer = a2
    24.                 Dim alphabet3Chars() As String = Enumerable.Range(0, 26).Select(Function(x1) alphabet(loopVariable1) & alphabet(loopVariable2) & Chr(65 + x1)).ToArray
    25.                 alphabetString = alphabetString.Concat(alphabet3Chars).ToArray
    26.             Next
    27.         Next
    28.  
    29.         ' This call is required by the Windows Form Designer.
    30.         InitializeComponent()
    31.  
    32.         ' Add any initialization after the InitializeComponent() call.
    33.  
    34.     End Sub
    35.  
    36.     Public Property Minimum() As Integer
    37.         Get
    38.             Return CInt(NumericUpDown1.Minimum)
    39.         End Get
    40.         Set(ByVal value As Integer)
    41.             If value > 0 And value <= 18278 And value < Maximum Then
    42.                 NumericUpDown1.Minimum = value
    43.             Else
    44.                 Throw New ArgumentOutOfRangeException("Minimum")
    45.             End If
    46.         End Set
    47.     End Property
    48.  
    49.     Public Property Maximum() As Integer
    50.         Get
    51.             Return CInt(NumericUpDown1.Maximum)
    52.         End Get
    53.         Set(ByVal value As Integer)
    54.             If value > 0 And value <= 18278 And value > Minimum Then
    55.                 NumericUpDown1.Maximum = value
    56.             Else
    57.                 Throw New ArgumentOutOfRangeException("Maximum")
    58.             End If
    59.         End Set
    60.     End Property
    61.  
    62.     Public Property Value() As Integer
    63.         Get
    64.             Return CInt(NumericUpDown1.Value)
    65.         End Get
    66.         Set(ByVal value As Integer)
    67.             If value >= Minimum And value <= Maximum Then
    68.                 NumericUpDown1.Value = value
    69.             Else
    70.                 Throw New ArgumentOutOfRangeException("Value")
    71.             End If
    72.         End Set
    73.     End Property
    74.  
    75.     Public ReadOnly Property ValueString() As String
    76.         Get
    77.             Return TextBox1.Text
    78.         End Get
    79.     End Property
    80.  
    81. End Class

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: NumericUpDown with letters

    Great, there we go.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    152

    Re: NumericUpDown with letters

    OK, absolute last question:

    Adding the blank value shifts everything so blank is 1, A is 2, B is 3, etc. How do I change it so that blank is 0, A is 1, etc? (Changing the control's minimum to 0 and the maximum and minimum properties accordingly is part of it, but what else do I need to do?)

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: NumericUpDown with letters

    just change minimum to 0 + this:

    Code:
    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        If alphabetString.Length = 0 Then Return
        TextBox1.Text = alphabetString(CInt(NumericUpDown1.Value) - 1)
    End Sub
    remove the highlighted - 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width