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 :
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
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Re: problem at host my custom control in datagridview
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?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Re: problem at host my custom control in datagridview
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:
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 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 !
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
Re: problem at host my custom control in datagridview
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?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
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.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Re: problem at host my custom control in datagridview
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
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.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
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
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