Search code examples
characterdatagridviewcolumnrestrictmaskedtextbox

maskedEditColumn datagridview how to use class? is it what i need?


I am trying to mask user's input in a datagridview column and i found this ready class Masked edit column Class that adds a 'mask edit column' option in the column types list. When i select this column type a mask field is being added in the list of column properties. I tried to do my job by adding some mask elements in this 'Mask' field, but when I run the code it didnt restrict me from adding other characters. I re-opened the 'edit columns menu' and I saw that the 'Mask' field was empty.

I want the text cell to accept 20 chars maximum and only: 1.Capital Letters(English & Greek), 2.these three chars(.,-), 3.Numbers 0-9

So as a first test i used only this mask(>????????????????????) but it didnt work as it didnt convert my characters to Uppercase and accepted more than 20 chars when i end the cell edit.

i am not sure the way to go is the Masked Text Box way. i have made many projects on vb and i used to use a loop in the textChanged event of a text box to restrict characters entry. the loop is this : (but i cant use it now in the valueChanged event cause it seems that 'value' doesn't have a selectionStart property.)

Dim charactersDisallowed As String = "!@#$%^&*()+=|}{][:;?/><.,~""

        Dim theText As String = txtCopies.Text
        Dim Letter As String
        Dim SelectionIndex As Integer = txtCopies.SelectionStart
        Dim Change As Integer

        For x As Integer = 0 To txtCopies.Text.Length - 1
            Letter = txtCopies.Text.Substring(x, 1)
            If charactersDisallowed.Contains(Letter) Then
                theText = theText.Replace(Letter, String.Empty)
                Change = 1
            End If
        Next

        txtCopies.Text = theText
        txtCopies.Select(SelectionIndex - Change, 0)

So,

  1. Is a masked text cell what i need? and if yes( Why is this mask box not keeping the mask i enter? And how can i use this class to do my job?)

  2. What can i alternately do to restrict some characters in a column's cells? (I will then convert to Uppercase on cellEndEdit)


Solution

  • I finally did it by removing the unwanted characters on cellvaluechanged event, which seems that is being raised when I end the cell's edit by for example hitting "Enter".