PDA

View Full Version : گفتگو: کدی برای نامبر باکس ها



hadgph
دوشنبه 19 تیر 1391, 20:35 عصر
برای ایجاد نامبرباکسها راه های مختلفی است، بهترین موردی که پیدا کردم ماژول زیر بود
Public Class NumericTextBox
Inherits TextBox
Private SpaceOK As Boolean = False

' Restricts the entry of characters to digits (including hex),
' the negative sign, the e decimal point, and editing keystrokes (backspace).
Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
MyBase.OnKeyPress(e)

Dim numberFormatInfo As Globalization.NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat
Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
Dim negativeSign As String = numberFormatInfo.NegativeSign

Dim keyInput As String = e.KeyChar.ToString()

If [Char].IsDigit(e.KeyChar) Then
' Digits are OK
ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
' Decimal separator is OK
ElseIf e.KeyChar = vbBack Then
' Backspace key is OK
Else
' Consume this invalid key and beep.
e.Handled = True
End If

End Sub


Public ReadOnly Property IntValue() As Integer
Get
Return Int32.Parse(Me.Text)
End Get
End Property


Public ReadOnly Property DecimalValue() As Decimal
Get
Return [Decimal].Parse(Me.Text)
End Get
End Property


Public Property AllowSpace() As Boolean

Get
Return Me.SpaceOK
End Get
Set(ByVal value As Boolean)
Me.SpaceOK = value
End Set
End Property
End Class

اما مشکلی که داره کار کردن داخل فرم باهاش راحت نیست مثلا باید به شکل زیر داخل فرم تعریف بشه

' Create an instance of NumericTextBox.
Dim NumericTextBox1 As NumericTextBox = New NumericTextBox()
NumericTextBox1.Parent = Me

' Draw the bounds of the NumericTextBox.
NumericTextBox1.Bounds = New Rectangle(10, 10, 100, 100)


دوستان راه حل بهتری پیشنهاد میکنن؟:بوس:

ROSTAM2
دوشنبه 19 تیر 1391, 21:12 عصر
يه UserControl اضافه كنيد اين كدها رو بهش اختصاص بديد (بهمراه يك TextBox)

در قسمت General -- Declarations


Imports System.Globalization


در قسمت NumericTextBox -- Declaration


Private SpaceOK As Boolean = False
Public ReadOnly Property IntValue() As Integer
Get
Return Int32.Parse(TextBox1.Text)
End Get
End Property
Public ReadOnly Property DecimalValue() As Decimal
Get
Return [Decimal].Parse(TextBox1.Text)
End Get
End Property
Public Property AllowSpace() As Boolean

Get
Return Me.SpaceOK
End Get
Set(ByVal value As Boolean)
Me.SpaceOK = value
End Set
End Property


در رويداد KeyPress‌ از TextBox


Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim numberFormatInfo As Globalization.NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat
Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
Dim negativeSign As String = numberFormatInfo.NegativeSign

Dim keyInput As String = e.KeyChar.ToString()

If [Char].IsDigit(e.KeyChar) Then
' Digits are OK
ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
' Decimal separator is OK
ElseIf e.KeyChar = vbBack Then
' Backspace key is OK
Else
' Consume this invalid key and beep.
e.Handled = True
End If
End Sub


يك بار پروژه رو ReBuild كرده و در TooBox‌دنبال شئ تون براي اضافه كردنش به برنامه بگرديد ...

hadgph
سه شنبه 20 تیر 1391, 20:40 عصر
ممنون ! راه حل جالب و موثری بود