Results 1 to 14 of 14

Thread: Show NUD with Inches mark

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Show NUD with Inches mark

    What would be the easiest way to have a NumericUpDown control showing a double quote " after the value? It's to represent a distance in inches.

    From MS Word:
    Name:  InchesNUD.jpg
Views: 424
Size:  4.5 KB
    Last edited by JuggaloBrotha; Sep 5th, 2012 at 12:29 PM. Reason: Removed the 'Resolved' from the thread
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Show NUD with Inches mark

    I don't think there is a way to format a standard Windows NUD, but you can with the DevExpress one. I know at one time, they were offering their XtraEditiors set (which includes it) for free for signing up with their site, but I don't know if they still do.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Show NUD with Inches mark

    Easiest way would probably be to make your own user control with a textbox and a vscroll control and handle the text input yourself to ensure proper formatting. I don't see how you could use the NUD control for this since it only allows numeric entry.

  4. #4

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Show NUD with Inches mark

    Quote Originally Posted by kleinma View Post
    Easiest way would probably be to make your own user control with a textbox and a vscroll control and handle the text input yourself to ensure proper formatting. I don't see how you could use the NUD control for this since it only allows numeric entry.
    I very well may just make my own.

    The reason for thinking of using a NUD is because it is for just numeric input.

    Edit:
    Actually it may not be all that hard, here's a start I have:
    Code:
    Public Class INumericUpDown
        Inherits System.Windows.Forms.NumericUpDown
    
        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(ByVal Value As String)
                MyBase.Text = MyBase.Value.ToString & """"
            End Set
        End Property
    
    End Class
    Last edited by JuggaloBrotha; Nov 9th, 2010 at 03:23 PM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Show NUD with Inches mark

    You should also probably handle a key event to allow a user to enter the " manually (as I believe word does) in addition to having it auto populate. If you don't let it be entered manually, it could feel like there is something wrong with the control.

    Also, don't name it INumericUpDown, that naming convention implies interface, and you are making a class.

  6. #6

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Show NUD with Inches mark

    Quote Originally Posted by kleinma View Post
    You should also probably handle a key event to allow a user to enter the " manually (as I believe word does) in addition to having it auto populate. If you don't let it be entered manually, it could feel like there is something wrong with the control.

    Also, don't name it INumericUpDown, that naming convention implies interface, and you are making a class.
    That's on my list of things to do with it, when I get the chance. Along with parsing the data in it to set the Value property when the user changes it not using the Up/Down spinner.

    I only called it INumericUpDown to simply give it a name other than NumericUpDown, I've already given it a better name on my end.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  7. #7
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Re: Show NUD with Inches mark

    Unless I have miss understood what you are trying to do couldn't you just use the DomainUpDown control? As this allows text to be entered not just numbers.
    I unfortunately only have VS 2010 installed on my current PC but I'm fairly certain it is in at least VS 2005 as well.

    I hope that this helps

    Satal

  8. #8

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Show NUD with Inches mark

    Quote Originally Posted by Satal Keto View Post
    Unless I have miss understood what you are trying to do couldn't you just use the DomainUpDown control? As this allows text to be entered not just numbers.
    I unfortunately only have VS 2010 installed on my current PC but I'm fairly certain it is in at least VS 2005 as well.

    I hope that this helps

    Satal
    What I need is a control with the UpDown spinner that increments whatever number is in it by 1 within a min & max value and allows a single decimal place. But I would like it to display the unit of measurement that the number being inputted is for, which is inches in this case so I would like it to show a double quote (") in the control as well. What's being used from the control is just the number, which is exactly what a NUD already does.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9
    Lively Member
    Join Date
    Sep 2010
    Location
    Canada
    Posts
    68

    Re: Show NUD with Inches mark

    The start you have could lead to some confusion, as this:
    Code:
    Me.InchesNumericUpDown1.Text = Me.InchesNumericUpDown1.Text
    Would actually change the value. I believe you are better off handling the TextChanged or Leave event of a TextBox in a UserControl. Basically:
    If it does not end with " add the " and select the character before the ".
    in either event.

  10. #10
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Re: Show NUD with Inches mark

    With my idea you would have to deal with setting up the minimum/maximum but, I would still have thought it might have been easier than creating a new control?

    So I think something like this might work?

    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Double = 0
    
            While i < 10
                Me.DomainUpDown1.Items.Add(New InchesDistance(i))
                i += 0.1
            End While
        End Sub
        Public Class InchesDistance
            Property _distance As Double
            Public Sub New(ByVal distance As Double)
                _distance = distance
            End Sub
            Public Overrides Function ToString() As String
                Return _distance.ToString & Chr(34)
            End Function
        End Class
    End Class
    Please bare in mind this is something I've thrown together in about a minute as I'm just about to leave for work so it may not be perfect, for example you would probably write the loop differently.

  11. #11

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Show NUD with Inches mark

    Ran into the first issue with it since I started using it in the actual app, here's the updated code:
    Code:
    Option Explicit On
    Option Strict On
    Option Infer Off
    
    <DebuggerStepThrough()> _
    Public Class InchNUD
        Inherits System.Windows.Forms.NumericUpDown
    
        Private Const _DQuotes As String = """"
    
        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(ByVal Value As String)
                Dim TempValue As Decimal = Decimal.Parse(Value.Replace(_DQuotes, String.Empty))
                Select Case True
                    Case TempValue > Me.Maximum : MyBase.Value = Me.Maximum
                    Case TempValue < Me.Minimum : MyBase.Value = Me.Minimum
                    Case Else : MyBase.Value = TempValue
                End Select
                MyBase.Text = MyBase.Value.ToString & _DQuotes
            End Set
        End Property
    
        Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
            MyBase.OnKeyPress(e)
            If e.KeyChar = _DQuotes Then Me.Text &= e.KeyChar
        End Sub
    
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            MyBase.OnTextChanged(e)
            Dim tmp As Decimal
            If Decimal.TryParse(Me.Text.Replace(_DQuotes, String.Empty), tmp) Then
                If Me.Value <> tmp Then Me.Value = tmp
            End If
        End Sub
    
    End Class
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  12. #12

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Show NUD with Inches mark

    Here's what I ultimately am using:
    vb Code:
    1. Imports System.ComponentModel
    2.  
    3. <ToolboxBitmap(GetType(NumericUpDown))>
    4. Public Class UnitNUD
    5.     Inherits System.Windows.Forms.NumericUpDown
    6.  
    7.     <Category("Appearance"), DefaultValue(""""c)>
    8.     Public Property UnitChar As String = """"
    9.  
    10.     Protected Overrides Sub UpdateEditText()
    11.         Me.Text = Me.Value.ToString() & UnitChar
    12.     End Sub
    13.  
    14. End Class
    The 1 flaw that it has is if the user types in a new number leaving the double quote (") at the end, it doesn't throw an error/exception instead it reverts back to the previous value.
    For example, if it's showing 1.3" in the textbox and the user only highlights the 1.3 (not including the double quote) then types in 2.5 so the text portion shows:
    2.5|" with the | being the blinking carrot, then you tab out of the control, or click on another control, the NUD will default back to 1.3" instead of accepting the new value. What would be a good way to overcome this?
    I don't see a Parse method I can override in the new control to strip the UnitChar before the base control parses the text value.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Show NUD with Inches mark

    If you override the OnValidating method you can get a little closer. Check if the Text ends with the unit character in the OnValidating method, and if it does you trim it out. Now, if you tab out of the control it keeps the new value. Strange enough if you change the focus any other method (or just press enter), it doesn't accept the new value and reverts to the old value. I don't know why it works on tabbing out but not anyway else, that might have to do with the Validating event which I rarely use, perhaps someone has an idea to improve this?

  14. #14

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Show NUD with Inches mark

    Updated the code to include Nick's suggestion, also created a UnitCharChanged event too.
    vb Code:
    1. Imports System.ComponentModel
    2.  
    3. <ToolboxBitmap(GetType(NumericUpDown))>
    4. Public Class UnitNUD
    5.     Inherits System.Windows.Forms.NumericUpDown
    6.  
    7.     Public Event UnitCharChanged As EventHandler
    8.  
    9.     Private m_UnitChar As String = String.Empty
    10.  
    11.     Public Sub New()
    12.         MyBase.New()
    13.         m_UnitChar = """"c
    14.     End Sub
    15.  
    16.     <Category("Appearance"), DefaultValue(""""c)>
    17.     Public Property UnitChar As String
    18.         Get
    19.             Return m_UnitChar
    20.         End Get
    21.         Set(ByVal value As String)
    22.             If m_UnitChar <> value Then
    23.                 m_UnitChar = value
    24.                 Me.ValidateEditText()
    25.                 Call OnUnitCharChanged(EventArgs.Empty)
    26.             End If
    27.         End Set
    28.     End Property
    29.  
    30.     Protected Overrides Sub UpdateEditText()
    31.         Me.Text = Me.Value.ToString() & m_UnitChar
    32.     End Sub
    33.  
    34.     Protected Overrides Sub OnValidating(ByVal e As CancelEventArgs)
    35.         If Not (Me.ChangingText) Then
    36.             If (Me.Text.Trim().EndsWith(m_UnitChar)) Then
    37.                 Me.Text = Me.Text.Substring(0, Me.Text.LastIndexOf(m_UnitChar))
    38.             End If
    39.         End If
    40.     End Sub
    41.  
    42.     Protected Sub OnUnitCharChanged(ByVal e As EventArgs)
    43.         RaiseEvent UnitCharChanged(Me, e)
    44.     End Sub
    45.  
    46.     'Protected Overrides Sub ValidateEditText()
    47.     '    Me.Text = Me.Text.Replace(m_UnitChar, String.Empty)
    48.     '    MyBase.ValidateEditText()
    49.     'End Sub
    50.  
    51. End Class
    Last edited by JuggaloBrotha; Sep 5th, 2012 at 01:33 PM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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