Page 1 of 1

Custom field validation

PostPosted: Fri Feb 01, 2019 8:50 pm
by pricerc
How can I restrict the entry into a custom text cell and/or otherwise validate entered custom data when the cell loses focus?

I would like to add some basic validation to a field, which means a) limiting the character set, and b) specifying some rules around what is and isn't valid.

I would then like to flag invalid data before the user clicks 'Save'.

I tried a RegularExpressionCellType, but it's not very user-friendly (it just ignores any edits that aren't valid, and reverts to the previous value - if you've just entered a 20 character field incorrectly, that could be annoying)

Re: Custom field validation

PostPosted: Sun Feb 03, 2019 4:08 pm
by SBarnes
Hi Ryan,

You could in the Set Up Before of the form hook onto the Grids changed event to get in ahead of Jiwa an example of this is below for a different grid, and show a message / throw an exception if the input was invalid

Code: Select all

jgRunLines.Change += jgRunLines_Change;


private void jgRunLines_Change(object sender, FarPoint.Win.Spread.ChangeEventArgs e)
      {
         //System.Diagnostics.Debugger.Break();
         string Key = null;
         string ColID = jgRunLines.ActiveSheet.Columns[e.Column].Tag.ToString();

         if (ColID == "NoItems" && e.Column == NoItemsColumn)
         {

            Key = jgRunLines.get_GridText("RecID", e.Row).ToString();
            JiwaDeliveries.JiwaDeliveryRunInstanceLine line = this._run.Lines[Key];
            decimal newitems = decimal.Parse(jgRunLines.get_GridText("NoItems", e.Row).ToString());
            if(newitems > 0)
            {
               line.NoItems = decimal.Parse(jgRunLines.get_GridText("NoItems", e.Row).ToString());
               this.UltraToolbarsManager1.Tools["ID_RecordSave"].SharedProps.Enabled = true;
               this.UltraToolbarsManager1.Tools["ID_RecordCancel"].SharedProps.Enabled = true;
               line.ChangeFlag = true;
               this._run.RecalcItemsAndOrders();
               this.utOrders.Value = this._run.NoOrders.ToString();
               this.utItems.Value = this._run.NoItems.ToString();
            }
            else
            {
                jgRunLines.set_GridText("NoItems",e.Row,line.NoItems.ToString());
               System.Windows.Forms.MessageBox.Show("Number must be greater than zero.");
            }

            
         }
         
      }


There is also EditModeOff which looks like

Code: Select all
private void fpSpread1_EditModeOff(object sender, System.EventArgs e)
{
     ListBox1.Items.Add("EditModeOff event fired!");
}



Also CustomFieldValue and CustomField also implement INotifyPropertyChanged so you could hook into this possibly as well.

Re: Custom field validation  Topic is solved

PostPosted: Mon Feb 04, 2019 9:01 am
by Scott.Pearce
Use the MaskCellType. I have attached the documentation for the grid control that gives more information regarding this cell type (ftp://ftp.jiwa.com.au/FarPoint.Win.Spread.8.0.chm). I have also attached a sample plugin that masks the Description column of purchase order lines such that only 4 digits can be entered.

The basic code for a MaskCellType looks like this:

Code: Select all
      FarPoint.Win.Spread.CellType.MaskCellType maskcell = new FarPoint.Win.Spread.CellType.MaskCellType();
      maskcell.Mask = "####";
      maskcell.MaskChar = Convert.ToChar("#");
      purchaseOrderForm.LinesJiwaGrid.ActiveSheet.Columns["Description"].CellType = maskcell;

Re: Custom field validation

PostPosted: Mon Feb 04, 2019 9:09 am
by Mike.Sheen
Note that to read downloaded CHM files you'll have to set the Unblock property of the downloaded CHM or all you'll see is blank pages.

Do this by right-clicking the downloaded file, select properties from the context menu, and then check the Unblock checkbox on the properties dialog.
Unblockdownloadedfile.PNG
Unblockdownloadedfile.PNG (20.62 KiB) Viewed 1778 times

Re: Custom field validation

PostPosted: Mon Feb 04, 2019 4:30 pm
by pricerc
Aha! Thanks chaps.

So the mask was the trick I was looking for (although I dunno how I missed that and yet found Regex!).

I opted to use {Space} for the mask character, otherwise unused numbers are stored in the databases as whatever your mask character is.

But then I (eventually) figured out how that between 'Cell_EditorValueChanged', 'FormatCell' and 'ReadData', I could change the mask on one field, based on a selection in another.

I achieved this by using private fields to store a) the CustomFieldValue object for the values I was tracking, and b) the CellType objects for the related cells.