PDA

View Full Version : محدود کردن DataGridViewTextBoxColumn



Hossein Bazyan
چهارشنبه 13 شهریور 1387, 18:13 عصر
سلام
من در برنامه از datagridview استفاده نموده ام که چند تا از ستونهای آن فقط باید عدد بگیرد . یعنی DataGridViewTextBoxColumn کد زیر را نوشته ام و خوب هم کار میکند وفقط به عدد اجازه ورود میدهد اما مشکلی که داره اینه که با نوشتن عدد و زدن اینتر یا با رفتن به سطر بعد که مقدار رشته را باید بگیرد باز هم محدودیت اعمال میکند و حق ورود به رشته را نمیدهد .
در اینترنت و در همین سایت هم گشتم اما چیزی دستگیرم نشد.
لطفا راهنمایی کنید.

در قسمت EditingControlShowing کد زیر را قرار داده ام


Private Sub dgvVast_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs) Handles dgvVast.EditingControlShowing
If dgvVast.CurrentCell.ColumnIndex = 3 Then
Dim txtEdit As TextBox = TryCast(e.Control, TextBox)
AddHandler txtEdit.KeyPress, AddressOf txtEdit_KeyPress
End If
End Sub


و این هم از تابع txtEdit


Sub txtEdit_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Then
e.Handled = False
ElseIf Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 44 Then ' back space
e.Handled = False
Else
e.Handled = True
End If

حالا در ردیف شماره 3 فقط عدد میکیرد اما پس از وارد نمودن عدد در ردیفهای دیگر هم اجازه ورود به رشته را نمیدهد. دلیلش هم اینه که رویداد keypress در txtEdit_KeyPress هر بار با زدن کلیدی در دیتاگرید دوباره فعال میشود.

Dariuosh
چهارشنبه 13 شهریور 1387, 23:18 عصر
شما ميتوني اول يه TextBox درست کني با شرايطي که ميخواي
مثل هميني که بالا نوشتي


PublicClass TxtNumber
Inherits System.Windows.Forms.TextBox
PrivateSub TxtNumber_KeyPress(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyPressEventArgs) HandlesMe.KeyPress
If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Then
e.Handled = False
ElseIf Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 44 Then' back space
e.Handled = False
Else
e.Handled = True
EndIf
EndSub
EndClass

بعدش جايي که ميخواي ازش يه نمونه ميگيري
و در زمان Edit اون Column مورد نظر جايگزينش ميکني


Private txtNo AsNew TxtNumber
PrivateSub dataGridView1_CellBeginEdit(ByVal sender AsObject, ByVal e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
If e.ColumnIndex = 3 Then
Dim rect As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(e.ColumnI ndex, e.RowIndex, True)
WithMe.txtNo
.Text = sender.CurrentCell.Value
.Location = rect.Location
.Size = rect.Size
.Visible = True
EndWith
EndIf
EndSub
PrivateSub dataGridView1_CellEndEdit(ByVal sender AsObject, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
If e.ColumnIndex = 3 Then
Me.DataGridView1.CurrentCell.Value = Me.txtNo.Text
Me.txtNo.Visible = False
EndIf
EndSub
PrivateSub dataGridView1_Scroll(ByVal sender AsObject, ByVal e As ScrollEventArgs) Handles DataGridView1.Scroll
IfMe.txtNo.Visible = TrueThen
Dim r As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(Me.DataGr idView1.CurrentCell.ColumnIndex, Me.DataGridView1.CurrentCell.RowIndex, True)
Me.txtNo.Location = r.Location
Me.txtNo.Size = r.Size
EndIf
EndSub
PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
Me.txtNo.Visible = False
Me.DataGridView1.Controls.Add(Me.txtNo)
EndSub

اميدوارم مفهوم گفته باشم

mostafaaa
پنج شنبه 14 شهریور 1387, 11:19 صبح
هم میتونی یه شرط دیگه به تابع txtedit اضافه کنی

Sub txtEdit_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Me.DataGridView1.CurrentCell.ColumnIndex = 3 Then
If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Then
e.Handled = False
ElseIf Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 44 Then ' back space
e.Handled = False
Else
e.Handled = True
End If
End If
End Sub
و هم میتونی از متد RemoveHandler به همراه AddHandler استفاده کنی

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs) Handles DataGridView1.EditingControlShowing
Dim txtEdit As TextBox = TryCast(e.Control, TextBox)
If Me.DataGridView1.CurrentCell.ColumnIndex = 3 Then
AddHandler txtEdit.KeyPress, AddressOf txtEdit_KeyPress
Else
RemoveHandler txtEdit.KeyPress, AddressOf txtEdit_KeyPress
End If
End Sub

Hossein Bazyan
پنج شنبه 14 شهریور 1387, 11:46 صبح
شما ميتوني اول يه TextBox درست کني با شرايطي که ميخواي
مثل هميني که بالا نوشتي
اميدوارم مفهوم گفته باشم
سلام
روش جالبی بود . استفاده کردم
مرسی

Hossein Bazyan
پنج شنبه 14 شهریور 1387, 12:20 عصر
هم میتونی یه شرط دیگه به تابع txtedit اضافه کنی
و هم میتونی از متد RemoveHandler به همراه AddHandler استفاده کنی

RemoveHandler txtEdit.KeyPress, AddressOf txtEdit_KeyPress

[/code]
سلام
این هم شیوه خوبی است اما به RemoveHandler گیر میده . چون زمانی به این قسمت از شرط وارد میشه که هنوز هندل نشده و میخوهیم حذفش کنیم .
به هر حال من شیوه دیگری به کار بردم یعنی یه جورایی کلک مرغابی
شرط گذاشتم زمانی به رویداد KeyPress وارد شه که در همان سلول باشیم

ali_md110
پنج شنبه 14 شهریور 1387, 13:53 عصر
Dim nonNumberEntered As Boolean
Private Sub editingControl_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
Dim selPos As Integer = 0

Dim regPos As Integer = 0

Dim temp As TextBox = CType(sender, TextBox)

selPos = temp.SelectedText.IndexOf(".")

regPos = temp.Text.IndexOf(".")

nonNumberEntered = False

If regPos <> -1 AndAlso (e.KeyCode = Keys.OemPeriod OrElse e.KeyCode = Keys.Decimal) Then


If selPos < 0 Then


nonNumberEntered = True

End If

End If

If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 OrElse e.Modifiers <> Keys.None Then

If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 OrElse e.Modifiers <> Keys.None Then

If (e.KeyCode <> Keys.Back AndAlso e.KeyCode <> Keys.OemPeriod AndAlso e.KeyCode <> Keys.Decimal) OrElse e.Modifiers <> Keys.None Then
nonNumberEntered = True

End If

End If

End If

End Sub

Private Sub editingControl_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If nonNumberEntered = True Then ' قرار دهید فقط عدد وارد و برعکس فقط حروفtrue اگر در حالت

e.Handled = True

End If

End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs) Handles DataGridView1.EditingControlShowing
Dim row As DataGridViewRow = DataGridView1.CurrentRow
Dim cell0 As DataGridViewCell = row.Cells(0) 'در اینجا سلوهایی که جهت فیلتر شدن لازم است رو بر اساس ایندکس معرفی کرده
Dim cell1 As DataGridViewCell = row.Cells(1)
Dim cell2 As DataGridViewCell = row.Cells(2)


RemoveHandler DataGridView1.EditingControl.KeyPress, AddressOf editingControl_KeyPress

RemoveHandler DataGridView1.EditingControl.KeyDown, AddressOf editingControl_KeyDown

If DataGridView1.CurrentCell Is cell1 OrElse DataGridView1.CurrentCell Is cell2 Then 'و در اینجا سلولها رو بکار میبریم

'
AddHandler DataGridView1.EditingControl.KeyPress, AddressOf editingControl_KeyPress

AddHandler DataGridView1.EditingControl.KeyDown, AddressOf editingControl_KeyDown

End If
End Sub