In my project I have a grid cell bound to a data point that has a fixed allowable length (36 chars)
There is a routine in the program that attempts to "Auto Build" this data however sometimes due to the other inputs the results is greater than 36 chars.
The user can then make manual corrections to the point to shorten it
What I wanted to be able to do was provide a quick reference point to show when the string was with-in the accepted length.
My first attempt was using a .MaskInput = ">&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&|" but I forgot that this would still truncate the excess string (Duh!)
I was unable to find anything that would just allow "anything" after the pipe regardless of length so I just added 25 more '&' after the pipe character to allow for some excess data - this is 'OK' and will likely be enough space but it causes Column width issues (grid thinks the column should be wide enough to accommodate the full mask even when there's no data) and when the cell enters edit mode for the user to make corrections the MaskInput makes the cell / string act like there are padded white spaces on the end instead of going to the last visible character as one would expect.
Is there some format string character or MaskInput option that I'm missing that will allow for a non-fixed length of trailing characters after the pipe or is there a different / better way to tackle this problem than using an Input Mask?
Thanks in advance for your help.
Stephen
Edit - the allowable length is constrained in the database on the back end and I handle trimming the data prior to save. I have not constrained the column in any way (i.e. maxlength) so as to allow the display of the auto generated data
Hello Stephen,
Thank you for contacting Infragistics.
It's not really possible to display an indicator inside the cell since different characters can be different widths. What I would suggest is using a label or status bar outside of the grid to display the current length of the text in the cell. You would get this length by handling the CellChange event of the grid and accessing e.Cell.Text.Length inside the event handler.
Dave,
Thanks for the fast response.
The forums threw an error when I posted my edit, so I had to try and edit my post a second time and apparently I omitted the fact that I am using a fixed width font that time around.
Anyhow - in that case character width is the same and therefore not really a hurdle. Also this is an "in house" application so I have total control over the font used and size and don't have to worry about someone else changing them.
This could allow for a "low tech" solution like a background image in the cell but it is not ideal and I would not like to go that route if at all possible.
I have used your solution in one of the predecessors to this application and it was not very well received and dropped altogether in the currently active version.
For the small percentage of time they will actually have to edit the data this may just be something they have to live with or maybe I can do something with the text length and selection start properties on edit of the cell. I'll keep digging, just wondered if anyone else had crossed this bridge or had a better solution.
Thanks Again
Steve
Hi Steve,
Presumably, you want the indicator to appear only while the user is editing the cell. Is that right? I mean, leaving the cell with data that is too long is completely out of the question, since this will force the data to be saved to the data source, which cannot handle it and will raise an exception. So this only makes sense while the caret is in the cell during editing.
I don't see any way to do this using masking.
I've seen some web site where they have a text box with a limited character input and what they do is they display some text outside of the textbox with a running total of the current number of characters. So maybe you could put up a tooltip or a label somewhere next to the grid that says something like:
"Maximum Characters Allowed: 100"
"Current length: 105"
Then you update the label using the TextChanged event so the current length is updated as the user types.
Another option would be to use color. You could set the ForeColor on the cell.Appearance to Red, for example, while the text is too long. Again, you would use CellChange for this and as soon as the text is short enough, you would revert to the normal cell color. The down side of this approach is that it may not be immediately obvious to the user why the text is red.
Another possible solution would be to use the UltraValidator component. I'm not sure, but I think it has the ability to enforce a length constraint, change the text color and also display an error icon which can display a tooltip with more detailed error information. It's been a while since I used it, though, so I might be overestimating it's capabilities.
Hi Mike,
Thanks for the suggestions.
I am still testing but I think I have a workable solution.
I used some of the information here ( http://stackoverflow.com/questions/17042149/format-ultragridrow-cells ) coupled with the source code of the WinGrid Sample Explorer section referenced in that link to create a custom editor for each cell only if it exceeds the max length
How it works is; during the InitializeRow event I'm calling a Read Only Property from my custom Defaults Class that builds a custom mask for each entry this way I don't have extra trailing spaces in the mask. If the length of data for the field in that row is within the required length the routine returns Nothing otherwise it returns a '&' for every character of allowed length, a Pipe, and a '&' for every character above the allowed length in the length of the string.
Public Shared ReadOnly Property mask_ExtendedName(point As _IPoint) As String Get If point.ExtendedName Is Nothing OrElse point.ExtendedName.Trim.Length <= length_ExtendedName Then Return Nothing Else ' Start with the Upper Case all Character Dim tmpstr As String = ">" ' Add placeholder for every position in allowed length For x As Integer = 1 To length_ExtendedName tmpstr = tmpstr & "&" Next x ' Add Pipe character tmpstr = tmpstr & "|" ' Add placeholder for every extra character For x As Integer = length_ExtendedName + 1 To point.ExtendedName.Trim.Length tmpstr = tmpstr & "&" Next x Return tmpstr End If End Get End Property
Next I test the results of that routine and if it's NOT nothing then I create a new EditorWithMask using the MaskInput string returned by my routine and set the cell editor to that new editor, otherwise nothing happens and it's business as usual for the grid.
Here's the snipit from the InitializeRow event
Dim _mask As String = DataLengths.Point.mask_ExtendedName(CType(e.Row.ListObject, _IPoint)) If _mask IsNot Nothing Then Dim editorSettings As New DefaultEditorOwnerSettings With editorSettings .MaskDataMode = UltraWinMaskedEdit.MaskMode.Raw .MaskDisplayMode = UltraWinMaskedEdit.MaskMode.IncludeBoth .MaskInput = _mask End With e.Row.Cells("ExtendedName").Editor = New EditorWithMask(New DefaultEditorOwner(editorSettings)) End If
I am already using background color change to indicate what cell(s) have errors and my Save routine will trim the data to the max allowed length to prevent SQL based exceptions.
And to answer your first question, it would not be a deal breaker if the Pipe was visible all the time as it would give an indicator of how much free space was left. This current solution works as you describe, showing only the Pipe character when the cell is actually in edit mode, It might actually be nice to have it show all the time once it's set on the values in error because the user then knows how much they have exceeded it limit by without having to click into the cell.
Thanks again for all the help.