-
1 Attachment(s)
problem at host my custom control in datagridview
hi
I crate a textbox control by Additional property as:
InputType as inputs , inputs is enum :
Public Enum Inputs
Standard = 0 (user can enter anything)
Numbers = 1 (only number)
Alphabets = 2
NumberAndAlphabet = 3
Decimals = 4 (only decimal by 3 decimal place)
NationalCode = 5 (my special country code for each person )
End Enum
Separator (if set to true , digit can be grouping)
And some other properties
this control(SHTextBox) Working properly , when I finish another component based on shtextbox (SHtextBoxDataGridViewColumn) based on following link information :
Code:
http://www.vb-tips.com/dbpages.aspx?ID=43d85ac7-1715-47fd-9e1f-dd17b41ace9a
but when add my column to datagridview , I can not change the value of inputtype property.
I read following article and see the note :
Code:
http://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.110).aspx
Must I override the Clone method ? if yes how I do it ?
I'm confused about host my control in datagridview and did not know what is the correct way
I attach my project here , Please check the code and solve my problem.
tank you
-
Re: problem at host my custom control in datagridview
As that page says, you have to override Clone in the derived cell and column classes IF you add properties. If you don't add properties then there's no additional properties to clone so the base implementation is fine.
-
Re: problem at host my custom control in datagridview
my shtextbox control , have few properties for example , user can only enter numbers, I would like to know the properties of the previous definition in shtextbox , must be define again in shdatagridviewcolumn ? or when i inherit form shtextbox , all of last defined properties Available in new column ?
please help me to solve my problem. if maybe correct my code please
-
Re: problem at host my custom control in datagridview
I can't open your project. What version of Visual Studio was this made in ?
-
Re: problem at host my custom control in datagridview
-
Re: problem at host my custom control in datagridview
sorry , in this class : SHDataGridViewTextBoxColumn
replase this code at line 25
Return cinputtype
-
Re: problem at host my custom control in datagridview
Quote:
Originally Posted by
jmcilhinney
As that page says, you have to override Clone in the derived cell and column classes IF you add properties. If you don't add properties then there's no additional properties to clone so the base implementation is fine.
Well clearly in this case there are added properties and there is therefore a need to do it. Which leaves the second part of the question "if yes how I do it ?" unanswered. The MSDN article gives no help whatsoever (as usual!) and any attempt I've made results in Vb2012 crashing (not that that's difficult!) so, how do you do it?
-
Re: problem at host my custom control in datagridview
Quote:
Originally Posted by
dunfiddlin
Well clearly in this case there are added properties and there is therefore a need to do it. Which leaves the second part of the question "if yes how I do it ?" unanswered. The MSDN article gives no help whatsoever (as usual!) and any attempt I've made results in Vb2012 crashing (not that that's difficult!) so, how do you do it?
This is the quote from that MSDN topic:
Quote:
When you derive from DataGridViewCell or DataGridViewColumn and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell or column.
I would hope that it is clear from that that you need to do this:
Code:
Public Overrides Function Clone() As Object
Dim copy = DirectCast(MyBase.Clone(), TheTypeOfThisClass)
copy.MyAddedProperty = Me.MyAddedProperty
'etc
Return copy
End Function
The only time you need to override Clone in your column and cell classes is if you have added properties to your column and cell classes so all you need to do in those Clone methods is copy the values of the added properties.
-
Re: problem at host my custom control in datagridview
After a few days I solve my problem in hosting my custom control in datagridview. but there is another problem in cellvalidation event and Thousand Separator.
keypress and textchanged events from my custom control (SHTextbox) work well in datagridview .if you change inputtype to number in SHDataGridViewTextBoxColumn , user can only input number in editing mode or separator work fine.
but validation event not working!
Of course i use the following article in form1 to validate all shtextboxes
http://blog.scosby.com/post/2010/02/...ows-Forms.aspx
but in datagridview , i want when user inpute unallowable entry , cellvalidating event run and preven move to another cell untill user modify and correct his entry. for example , numbers by length greater than 15 is not vlid in my program , or decimal valid length is 3
. or negative symbol must be first character or illegal numbers is not allowed (.2123 or 1323.),
how can set shtextbox validation event to SHDataGridViewTextBox ?
another problem is , when user typing numbers in shtextbox , Thousand Separator run well but in SHDataGridViewTextBox only when control is in editing mode an user typing numbers , it working , and Upon move to other cell , Thousand Separator Disappears !
pleae help me
I don't know way I cant upload my project again here!
the link from dropbox
https://www.dropbox.com/s/7zm02hj44t...MyTextBox2.zip
-
Re: problem at host my custom control in datagridview
So, you're saying that you want to validate the data to ensure that it is a number and it doesn't exceed a certain precision and scale, correct? If so, here's a function that will do that:
Code:
Private Function ValidateNumber(text As String, precision As Integer, scale As Integer) As Boolean
Dim number As Decimal
Return Decimal.TryParse(text, number) AndAlso
ValidatePrecision(number, precision) AndAlso
ValidateScale(number, scale)
End Function
Private Function ValidatePrecision(number As Decimal, precision As Integer) As Boolean
'Count the digits and check whether the result is within the prescribed precision.
Return number.ToString().Select(Function(ch) Char.IsDigit(ch)).Count() <= precision
End Function
Private Function ValidateScale(number As Decimal, scale As Integer) As Boolean
Dim text = number.ToString()
Dim decimalSeparator = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
Dim decimalSeparatorIndex = text.IndexOf(decimalSeparator)
'If there is a decimal separator, count the digits after it and check whether the result is within the prescribed scale.
Return decimalSeparatorIndex = -1 OrElse
text.Skip(decimalSeparatorIndex + decimalSeparator.Length).Count() <= scale
End Function
-
Re: problem at host my custom control in datagridview
hi
i don't have porblem in validaing numbers ! number validating is in my shtextbox in textbox validating event. but when i host my control in datagridview , validation event don't work .
my Question is why keypress and textchanged events (form shtextbox) work in datagridvew when editingcontrol showing but why validation event don't fired
tank you
-
1 Attachment(s)
Re: problem at host my custom control in datagridview
i attach new project , don't get it from first post
-
Re: problem at host my custom control in datagridview
Quote:
but when i host my control in datagridview , validation event don't work .
I really don't understand what you mean. It seems to work perfectly. In Numbers mode only numbers are permitted so surely all the validation events are taking place as expected?
-
Re: problem at host my custom control in datagridview
in above project that attached, I change AutoValidate property of form1 to EnablePreventFocusChange. then I overrides onvalidating and onvalidated events at end of SHDataGridViewTextBoxEditingControl class
now validation on my customcolumn work fine but when I click by mouse to move to another cell , validating event fired 1 time and prevent to move and when I press tab key or enter , validating fired 2 time and move to another cell whitout prevent!. how solve it to fire only one time by press tab key ?
Code:
Protected Overrides Sub OnValidating(e As CancelEventArgs)
If AllowNull = False Then
If String.IsNullOrEmpty(Me.Text.Trim) = True Then
e.Cancel = True
' show message that cell value can not be null
MessageBox.Show("لطفاً اطلاعات را وارد نمائید", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Select Case InputType
Case Inputs.NationalCode
If ValidateNcode(Me.Text.Trim) = False Then
e.Cancel = True
'show message that nationalcode is not in correct format
MessageBox.Show("لطفاً کد ملی را بصورت صحیح وارد نمائید", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Case Inputs.Numbers
If AllowNegative = True Then
If ismatched(Me.Text.Trim, negshregex) = False Then
e.Cancel = True
'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
If ismatched(Me.Text.Trim, shregex) = False Then
e.Cancel = True
'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Case Inputs.Decimals
If AllowNegative = True Then
If ismatched(Me.Text.Trim, negshregex) = False Then
e.Cancel = True
'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
If ismatched(Me.Text.Trim, shregex) = False Then
e.Cancel = True
'show message that integer number length limit to 15 and decimal numbers maximum length is 3 for example 123456789012345.123
MessageBox.Show("فرمت اعداد صحیح نمی باشد" & vbNewLine & "حداکثر طول اعداد صحیح 15 رقم" & vbNewLine & "و حداکثر طول اعداد اعشار سه رقم می باشد", "ارزیابی اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Select
End If
MyBase.OnValidating(e)
End Sub
Protected Overrides Sub OnValidated(e As EventArgs)
MyBase.OnValidated(e)
End Sub
-
Re: problem at host my custom control in datagridview
when SHDataGridViewTextBox is in editing mode and I click by mouse on other cell , validating fired but when I press enter or tab my column exit from editing mode and validating don't work well. mean just show messagebox and don't prevent to move to other cells.
I think we must capture enter or tab in editing mode an call validating , I don't know is this a way or not
please help
-
Re: problem at host my custom control in datagridview
Why would you want to prevent moving to another cell? Your users will press Tab or Enter specifically to do that very thing.
-
Re: problem at host my custom control in datagridview
Quote:
Originally Posted by
dunfiddlin
Why would you want to prevent moving to another cell? Your users will press Tab or Enter specifically to do that very thing.
Because the user has to be prevented from committing invalid data so you have to decide whether you stop them at each invalid entry or pull them up on all invalid entires at the end. Generally speaking, I prefer the first option. It is perfectly legitimate to prevent the user moving on until they ensure that the current data is valid.
@sh-a, I'm not sure that it will make any difference but, as a test at least, try calling the base OnValidating before performing your own validation.
-
Re: problem at host my custom control in datagridview
dunfiddlin :
Quote:
Why would you want to prevent moving to another cell
As I said above, the numbers should not enter more than 15 digits
Quote:
try calling the base OnValidating before performing your own validation.
i edit oncellvalidating as following :
Code:
Protected Overrides Sub OnValidating(e As CancelEventArgs)
MyBase.OnValidating(e)
If AllowNull = False Then
..................
but not working.
-
Re: problem at host my custom control in datagridview
Quote:
Because the user has to be prevented from committing invalid data
Well normally yes, but surely the whole point of the custom editing control is to make it impossible to enter invalid data in the first place? The 15 digit maximum (or at least a MaximumDigits property) should be an inherent part of the custom control and therefore 'self' validating.
-
Re: problem at host my custom control in datagridview
-
Re: problem at host my custom control in datagridview
i debug program and found following information:
when user typing in my custom column and then click on another cell or any where of form , this events fired :
onlostfocus
onleave
onvalidating
and validating work well
when user typing in my custom column and then press ENTER, TAB or Esc , this events fired :
onprivewkeydownn
geteditingcontrolformattedvalue
editingcontrolvaluechanged
onlostfocus
onleave
onleave (shTextbox)
onvalidating (my custom column)
onvalidating (shtextbox)
onvalidating (my custom column)
by click enter or tab , esc , validating messagebox showing 1 time then my coustom column value ,Not shown , again messagebox showing , value showing in cell and without prevent , current cell changed
why there is Difference between going to other by mouse VS keyboard ?
i think i must capture (Tab , Enter , Esc) and call OnlostFocus and onleave or onvalidating
but i don't know how i do it
please help me
-
Re: problem at host my custom control in datagridview
After a month, finally found what I needed at this link.
http://www.aspnet-answers.com/micros...alidation.aspx
-
Re: problem at host my custom control in datagridview
if your datagridview loaded from database , you must edit InitializeEditingControl
Code:
Public Class SHDataGridViewTextBoxCell
Inherits DataGridViewTextBoxCell
Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim shcol As SHDataGridViewTextBoxColumn = DirectCast(OwningColumn, SHDataGridViewTextBoxColumn)
Dim shctl As SHDataGridViewTextBoxEditingControl = CType(DataGridView.EditingControl, SHDataGridViewTextBoxEditingControl)
' Because the datagridview fill from database , dosn't need set text
'Try
' shctl.Text = Me.Value.ToString
'Catch ex As Exception
' shctl.Text = ""
'End Try
shctl.InputType = shcol.InputType
shctl.AllowNegative = shcol.AllowNegative
shctl.AllowNull = shcol.AllowNull
shctl.Separator = shcol.Separator
End Sub