-
Nov 9th, 2010, 11:05 AM
#1
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:
Last edited by JuggaloBrotha; Sep 5th, 2012 at 12:29 PM.
Reason: Removed the 'Resolved' from the thread
-
Nov 9th, 2010, 01:55 PM
#2
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.
-
Nov 9th, 2010, 01:58 PM
#3
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.
-
Nov 9th, 2010, 03:06 PM
#4
Re: Show NUD with Inches mark
Originally Posted by kleinma
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.
-
Nov 9th, 2010, 03:32 PM
#5
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.
-
Nov 9th, 2010, 06:22 PM
#6
Re: Show NUD with Inches mark
Originally Posted by kleinma
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.
-
Nov 9th, 2010, 06:30 PM
#7
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
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Nov 9th, 2010, 07:09 PM
#8
Re: Show NUD with Inches mark
Originally Posted by Satal Keto
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.
-
Nov 9th, 2010, 09:49 PM
#9
Lively Member
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.
-
Nov 10th, 2010, 02:54 AM
#10
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.
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Nov 17th, 2010, 03:17 PM
#11
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
-
Sep 5th, 2012, 12:27 PM
#12
Re: [RESOLVED] Show NUD with Inches mark
Here's what I ultimately am using:
vb Code:
Imports System.ComponentModel <ToolboxBitmap(GetType(NumericUpDown))> Public Class UnitNUD Inherits System.Windows.Forms.NumericUpDown <Category("Appearance"), DefaultValue(""""c)> Public Property UnitChar As String = """" Protected Overrides Sub UpdateEditText() Me.Text = Me.Value.ToString() & UnitChar End Sub 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.
-
Sep 5th, 2012, 12:52 PM
#13
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?
-
Sep 5th, 2012, 01:02 PM
#14
Re: Show NUD with Inches mark
Updated the code to include Nick's suggestion, also created a UnitCharChanged event too.
vb Code:
Imports System.ComponentModel <ToolboxBitmap(GetType(NumericUpDown))> Public Class UnitNUD Inherits System.Windows.Forms.NumericUpDown Public Event UnitCharChanged As EventHandler Private m_UnitChar As String = String.Empty Public Sub New() MyBase.New() m_UnitChar = """"c End Sub <Category("Appearance"), DefaultValue(""""c)> Public Property UnitChar As String Get Return m_UnitChar End Get Set(ByVal value As String) If m_UnitChar <> value Then m_UnitChar = value Me.ValidateEditText() Call OnUnitCharChanged(EventArgs.Empty) End If End Set End Property Protected Overrides Sub UpdateEditText() Me.Text = Me.Value.ToString() & m_UnitChar End Sub Protected Overrides Sub OnValidating(ByVal e As CancelEventArgs) If Not (Me.ChangingText) Then If (Me.Text.Trim().EndsWith(m_UnitChar)) Then Me.Text = Me.Text.Substring(0, Me.Text.LastIndexOf(m_UnitChar)) End If End If End Sub Protected Sub OnUnitCharChanged(ByVal e As EventArgs) RaiseEvent UnitCharChanged(Me, e) End Sub 'Protected Overrides Sub ValidateEditText() ' Me.Text = Me.Text.Replace(m_UnitChar, String.Empty) ' MyBase.ValidateEditText() 'End Sub End Class
Last edited by JuggaloBrotha; Sep 5th, 2012 at 01:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|