DigitTool.DigitDockableWindow.ValidateFields C# (CSharp) Method

ValidateFields() public method

public ValidateFields ( ) : bool
return bool
		public bool ValidateFields()
		{
			bool blnValidated = true;
			System.Windows.Forms.MaskedTextBox mctrl = null;
			string errors = "";
			string linkTypeChoice = "";
			bool firstime = true;

			//check all mandatory fields
			System.Windows.Forms.Control ctrl2;


			foreach (System.Windows.Forms.Control ctrl in m_curPanel.Controls)
			{
				ctrl2 = ctrl;

				if (ctrl2.GetType() == typeof(System.Windows.Forms.Label))
				{
				}
				//ignore labels
				else
				{
					if ((string)ctrl2.Tag == "Mandatory")
					{
						if (ctrl2.GetType() == typeof(System.Windows.Forms.MaskedTextBox))
						{
							mctrl = (System.Windows.Forms.MaskedTextBox)ctrl2;
							if (mctrl.MaskCompleted == false)
							{
								blnValidated = false;
								if (errors.Length > 0)
									errors = "Incomplete mandatory field" + Environment.NewLine + "Complete missing data and click on OK button";
								else
									errors = errors + Environment.NewLine + "Incomplete mandatory field" + Environment.NewLine + "Complete missing data and click on OK button";
							}
						}
						else
						{
							if (ctrl2.Text.Length <= 0)
							{
								blnValidated = false;
								if (errors.Length <= 0)
									errors = "Incomplete mandatory field" + Environment.NewLine + "Complete missing data and click on OK button";
								else
									errors = errors + Environment.NewLine + "Incomplete mandatory field" + Environment.NewLine + "Complete missing data and click on OK button";
							}
						}
					}


					//check masked edit controls
					if (ctrl2.GetType() == typeof(System.Windows.Forms.MaskedTextBox))
					{
						mctrl = (System.Windows.Forms.MaskedTextBox)ctrl2;
						//if they typed something, but didn't complete it, then error
						//if they typed nothing and it is not mandatory, then it is OK
						if ((mctrl.Text.Length > 0) && mctrl.Modified && (!mctrl.MaskCompleted))
						{
							blnValidated = false;
							if (errors.Length > 0)
								errors = "Invalid entry in a masked text field";
							else
								errors = errors + Environment.NewLine + "Invalid entry in a masked text field";
						}
					}

					//check link connections
					if (m_curPanel == Splitter.Panel2)
					{
						//make sure that the relation is correct if it exists
						XmlNodeList fields = m_curLink.SelectNodes("descendant::Field");
						foreach (XmlElement field in fields)
						{
							//find the field with a type of "Relation"
							if (field.GetAttribute("Type") == "Relation")
							{
								ESRI.ArcGIS.Geodatabase.IDataset pDataset1;
								ESRI.ArcGIS.Geodatabase.IDataset pDataset2;

								string FeatureClass1Name;
								string FeatureClass2Name;

								pDataset1 = (ESRI.ArcGIS.Geodatabase.IDataset)m_schematicFeature1.SchematicElementClass;
								pDataset2 = (ESRI.ArcGIS.Geodatabase.IDataset)m_schematicFeature2.SchematicElementClass;

								FeatureClass1Name = pDataset1.Name;
								FeatureClass2Name = pDataset2.Name;

								foreach (XmlElement rel in m_relations)
								{

									//loop through the xml relations to match based on the from node and to node types

									if ((rel.GetAttribute("FromType") == FeatureClass1Name) && (rel.GetAttribute("ToType") == FeatureClass2Name))
									{
										//find the control with the pick list for relationships
										Control[] ctrls = m_curPanel.Controls.Find(field.GetAttribute("DBColumnName"), true);
										if (ctrls.Length > 0)
											ctrl2 = ctrls[0];

										XmlNodeList vals = rel.SelectNodes("descendant::Value");
										string myString = rel.GetAttribute("FromType") + "-" + rel.GetAttribute("ToType");
										string linkTypeClicking = myString;

										//validate that the current control string is correct
										//if there are values, use them
										bool blnfound = false;

										if (vals.Count > 0)
										{
											foreach (XmlElement val in vals)
											{
												linkTypeClicking = myString + "-" + val.InnerText.ToString();

												if (myString + "-" + val.InnerText.ToString() == ctrl2.Text)
												{
													blnfound = true;
													break;
												}
												else
												{
													blnfound = false;
													if (firstime)
													{
														linkTypeChoice = ctrl2.Text;
														firstime = false;
													}
												}
											}

											if (!blnfound)
											{
												blnValidated = false;
												if (errors.Length > 0)
												{
													errors = "Invalid link connection because :";
													errors = errors + Environment.NewLine + "Type's link clicked : " + linkTypeClicking;
													errors = errors + Environment.NewLine + "Type's link chosen : " + linkTypeChoice;

												}
												else
												{
													errors = errors + Environment.NewLine + "Invalid link connection because :";
													errors = errors + Environment.NewLine + "Type's link clicked : " + linkTypeClicking;
													errors = errors + Environment.NewLine + "Type's link chosen : " + linkTypeChoice;
												}
											}
										}
										else
										{
											if (ctrl2.Text != myString)
											{
												if (firstime)
												{
													linkTypeChoice = ctrl2.Text;
													firstime = false;
												}

												blnValidated = false;
												if (errors.Length > 0)
												{
													errors = "Invalid link connection because :";
													errors = errors + Environment.NewLine + "Type's link clicked : " + linkTypeClicking;
													errors = errors + Environment.NewLine + "Type's link chosen : " + linkTypeChoice;
												}
												else
												{
													errors = errors + Environment.NewLine + "Invalid link connection because :";
													errors = errors + Environment.NewLine + "Type's link clicked : " + linkTypeClicking;
													errors = errors + Environment.NewLine + "Type's link chosen : " + linkTypeChoice;
												}
											}
											else //// treatment case ctrl2.Text = myString
											{
												blnfound = true;
											}
										}

										if (!blnfound) //fix connection list
										{
											XmlNodeList vlist = m_dom.SelectNodes("descendant::Relation");
											XmlNodeList rellist = null;
											System.Windows.Forms.ComboBox cboconn = (System.Windows.Forms.ComboBox)ctrl2;
											cboconn.Items.Clear();
											foreach (XmlElement v in vlist)
											{
												if ((v.GetAttribute("LinkType").ToString() == m_curLink.GetAttribute("FeatureClassName").ToString())
													//make sure the node types are ok 
														&& (v.GetAttribute("FromType").ToString() == FeatureClass1Name || v.GetAttribute("ToType").ToString() == FeatureClass2Name))
												{
													rellist = v.SelectNodes("descendant::Value");
													if (rellist.Count > 0)
													{
														foreach (XmlElement r in rellist)
														{
															myString = v.GetAttribute("FromType").ToString() + "-" + v.GetAttribute("ToType").ToString() + "-" + r.InnerText.ToString();
															cboconn.Items.Add(myString);
														}
													}
													else //assume they are not using subtypes
														cboconn.Items.Add(v.GetAttribute("FromType").ToString() + "-" + v.GetAttribute("ToType").ToString());
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}

			if (errors.Length > 0)
			{

				if (m_curPanel == Splitter.Panel1)
				{
					btnOKPanel1.Visible = true;
					ErrorProvider1.SetError(btnOKPanel1, errors);
				}
				else
				{
					btnOKPanel2.Visible = true;
					ErrorProvider1.SetError(btnOKPanel2, errors);
				}
			}
			return blnValidated;

		}

Usage Example

        protected override void OnMouseUp(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            bool abortOperation = false;

            ESRI.ArcGIS.Schematic.ISchematicOperation schematicOperation = null;

            try
            {
                if (m_dockableDigit == null)
                {
                    return;
                }

                if (arg != null)
                {
                    m_x = arg.X;
                    m_y = arg.Y;
                }

                if (m_dockableWindow == null)
                {
                    return;
                }

                if (m_dockableWindow.IsVisible() == false)
                {
                    m_dockableWindow.Show(true);
                }

                ESRI.ArcGIS.SchematicControls.ISchematicTarget target = (ESRI.ArcGIS.SchematicControls.ISchematicTarget)m_schematicExtension;

                if (target != null)
                {
                    m_schematicLayer = target.SchematicTarget;
                }

                if (m_schematicLayer == null)
                {
                    System.Windows.Forms.MessageBox.Show("No target Layer");
                    return;
                }

                ISchematicInMemoryDiagram               inMemoryDiagram;
                ISchematicInMemoryFeatureClass          schematicInMemoryFeatureClass;
                ISchematicInMemoryFeatureClassContainer schematicInMemoryFeatureClassContainer;

                //Get the point
                ESRI.ArcGIS.Geometry.Point point = new ESRI.ArcGIS.Geometry.Point();

                ESRI.ArcGIS.ArcMapUI.IMxApplication mxApp;
                ESRI.ArcGIS.Display.IAppDisplay     appDisplay;
                IScreenDisplay         screenDisplay;
                IDisplay               display;
                IDisplayTransformation transform;
                ISpatialReference      spatialReference;

                inMemoryDiagram = m_schematicLayer.SchematicInMemoryDiagram;
                schematicInMemoryFeatureClassContainer = (ISchematicInMemoryFeatureClassContainer)inMemoryDiagram;

                if (schematicInMemoryFeatureClassContainer == null)
                {
                    return;
                }

                mxApp = (ESRI.ArcGIS.ArcMapUI.IMxApplication)m_app;

                if (mxApp == null)
                {
                    return;
                }

                appDisplay = mxApp.Display;

                if (appDisplay == null)
                {
                    return;
                }

                screenDisplay = appDisplay.FocusScreen;
                display       = screenDisplay;

                if (display == null)
                {
                    return;
                }

                transform = display.DisplayTransformation;

                if (transform == null)
                {
                    return;
                }

                spatialReference = transform.SpatialReference;

                WKSPoint mapPt = new WKSPoint();
                ESRI.ArcGIS.Display.tagPOINT devPoint;
                devPoint.x = m_x;
                devPoint.y = m_y;
                transform.TransformCoords(ref mapPt, ref devPoint, 1, 1); //'esriTransformToMap

                point.SpatialReference = spatialReference;
                point.Project(spatialReference);
                point.X = mapPt.X;
                point.Y = mapPt.Y;

                schematicInMemoryFeatureClass = schematicInMemoryFeatureClassContainer.GetSchematicInMemoryFeatureClass(m_dockableDigit.FeatureClass());

                if (schematicInMemoryFeatureClass == null)
                {
                    System.Windows.Forms.MessageBox.Show("Invalid Type.");
                    return;
                }

                if (m_dockableDigit.CreateNode())
                {
                    //TestMandatoryField
                    m_dockableDigit.btnOKPanel1.Visible = false;

                    if (m_dockableDigit.ValidateFields() == false)
                    {
                        m_dockableDigit.x(m_x);
                        m_dockableDigit.y(m_y);

                        System.Windows.Forms.MessageBox.Show(m_dockableDigit.ErrorProvider1.GetError(m_dockableDigit.btnOKPanel1) + m_messageFromOK);
                        return;
                    }

                    ESRI.ArcGIS.Geometry.IGeometry geometry;

                    ISchematicInMemoryFeature schematicInMemoryFeatureNode;

                    geometry = point;

                    schematicOperation = (ESRI.ArcGIS.Schematic.ISchematicOperation) new ESRI.ArcGIS.SchematicControls.SchematicOperation();

                    //digit operation is undo(redo)able we add it in the stack
                    IMxDocument doc = (IMxDocument)m_app.Document;
                    ESRI.ArcGIS.SystemUI.IOperationStack operationStack;
                    operationStack = doc.OperationStack;
                    operationStack.Do(schematicOperation);
                    schematicOperation.StartOperation("Digit", m_app, m_schematicLayer, true);

                    //do abort operation
                    abortOperation = true;

                    schematicInMemoryFeatureNode = schematicInMemoryFeatureClass.CreateSchematicInMemoryFeatureNode(geometry, "");
                    //schematicInMemoryFeatureNode.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusNew; if we want the node deleted after update
                    schematicInMemoryFeatureNode.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusLocked;

                    abortOperation = false;
                    schematicOperation.StopOperation();

                    ISchematicFeature schematicFeature = schematicInMemoryFeatureNode;
                    m_dockableDigit.FillValue(ref schematicFeature);

                    if (m_dockableDigit.AutoClear())
                    {
                        m_dockableDigit.SelectionChanged();
                    }
                }
                else
                {
                    m_dockableDigit.btnOKPanel2.Visible = false;

                    //Get the Tolerance of ArcMap
                    Double      tolerance;
                    IMxDocument mxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)m_app.Document;
                    ESRI.ArcGIS.esriSystem.WKSPoint point2 = new WKSPoint();
                    ESRI.ArcGIS.Display.tagPOINT    devPt;

                    tolerance = mxDocument.SearchTolerancePixels;
                    devPt.x   = (int)tolerance;
                    devPt.y   = (int)tolerance;

                    transform.TransformCoords(ref point2, ref devPt, 1, 2); //2 <-> esriTransformSize 4 <-> esriTransformToMap

                    tolerance = point2.X * 5;                               //increase the tolerance value

                    IEnumSchematicFeature schematicFeatures = m_schematicLayer.GetSchematicFeaturesAtPoint(point, tolerance, false, true);

                    ISchematicFeature schematicFeatureSelected = null;
                    double            distancetmp;
                    double            distance = 0;
                    schematicFeatures.Reset();

                    if (schematicFeatures.Count <= 0)
                    {
                        return;
                    }

                    //pSchematicFeatures may contain several features, we are choosing the closest node.
                    ISchematicFeature schematicFeature2 = schematicFeatures.Next();

                    double dX;
                    double dY;
                    ISchematicInMemoryFeatureNode schematicInMemoryFeatureNode = null;
                    if (schematicFeature2 != null)
                    {
                        if (schematicFeature2.SchematicElementClass.SchematicElementType == ESRI.ArcGIS.Schematic.esriSchematicElementType.esriSchematicNodeType)
                        {
                            schematicInMemoryFeatureNode = (ISchematicInMemoryFeatureNode)schematicFeature2;
                        }
                    }

                    ISchematicInMemoryFeatureNodeGeometry schematicInMemoryFeatureNodeGeometry = (ISchematicInMemoryFeatureNodeGeometry)schematicInMemoryFeatureNode;
                    dX = schematicInMemoryFeatureNodeGeometry.Position.X;
                    dY = schematicInMemoryFeatureNodeGeometry.Position.Y;
                    schematicFeatureSelected = schematicFeature2;
                    distance = SquareDistance(dX - point.X, dY - point.Y);

                    while (schematicFeature2 != null)
                    {
                        //find the closest featureNode...
                        if (schematicInMemoryFeatureNode != null)
                        {
                            schematicInMemoryFeatureNodeGeometry = (ISchematicInMemoryFeatureNodeGeometry)schematicInMemoryFeatureNode;

                            if (schematicInMemoryFeatureNodeGeometry == null)
                            {
                                continue;
                            }

                            dX          = schematicInMemoryFeatureNodeGeometry.Position.X;
                            dY          = schematicInMemoryFeatureNodeGeometry.Position.Y;
                            distancetmp = SquareDistance(dX - point.X, dY - point.Y);

                            if (distancetmp < distance)
                            {
                                distance = distancetmp;
                                schematicFeatureSelected = schematicFeature2;
                            }
                        }

                        schematicFeature2 = schematicFeatures.Next();

                        if (schematicFeature2 != null)
                        {
                            if (schematicFeature2.SchematicElementClass.SchematicElementType == ESRI.ArcGIS.Schematic.esriSchematicElementType.esriSchematicNodeType)
                            {
                                schematicInMemoryFeatureNode = (ISchematicInMemoryFeatureNode)schematicFeature2;
                            }
                        }
                    }

                    if (schematicFeatureSelected == null)
                    {
                        return;
                    }

                    if (schematicFeatureSelected.SchematicElementClass.SchematicElementType != esriSchematicElementType.esriSchematicNodeType)
                    {
                        return;
                    }

                    if (m_schematicFeature1 == null)
                    {
                        m_schematicFeature1 = schematicFeatureSelected;
                        m_dockableDigit.SchematicFeature1(m_schematicFeature1);

                        if (!m_dockableDigit.CheckValidFeature(true))
                        {
                            m_schematicFeature1 = null;
                            m_dockableDigit.SchematicFeature1(m_schematicFeature1);
                            throw new Exception("Invalid starting node for this link type");
                        }

                        //Begin Feedback
                        m_linkFbk         = new NewLineFeedback();
                        m_linkFbk.Display = screenDisplay;

                        //symbol
                        ISimpleLineSymbol sLnSym;
                        IRgbColor         rGB = new RgbColor();

                        sLnSym = (ESRI.ArcGIS.Display.ISimpleLineSymbol)m_linkFbk.Symbol;

                        //Make a color
                        rGB.Red   = 255;
                        rGB.Green = 0;
                        rGB.Blue  = 0;

                        // Setup the symbol with color and style
                        sLnSym.Color = rGB;

                        m_linkFbk.Start(point);
                        //End Feedback

                        //To know if we are in the same diagram.
                        m_schematicLayerForLink = m_schematicLayer;
                    }
                    else
                    {
                        if (m_schematicLayerForLink != m_schematicLayer)
                        {
                            System.Windows.Forms.MessageBox.Show("wrong Target layer");
                            m_schematicLayerForLink = null;
                            EndFeedBack();
                            return;
                        }
                        m_schematicFeature2 = schematicFeatureSelected;
                        m_dockableDigit.SchematicFeature2(m_schematicFeature2);

                        //TestMandatoryField
                        if (m_dockableDigit.ValidateFields() == false)
                        {
                            m_dockableDigit.x(m_x);
                            m_dockableDigit.y(m_y);

                            System.Windows.Forms.MessageBox.Show(m_dockableDigit.ErrorProvider1.GetError(m_dockableDigit.btnOKPanel2) + m_messageFromOK);
                            return;
                        }

                        if (!m_dockableDigit.CheckValidFeature(false))
                        {
                            m_schematicFeature2 = null;
                            m_dockableDigit.SchematicFeature2(m_schematicFeature2);
                            throw new Exception("Invalid End node for this link type");
                        }

                        //CreateLink
                        ISchematicInMemoryFeature schematicInMemoryFeatureLink;

                        schematicOperation = (ESRI.ArcGIS.Schematic.ISchematicOperation) new ESRI.ArcGIS.SchematicControls.SchematicOperation();

                        //digit operation is undo(redo)able we add it in the stack
                        IMxDocument doc = (IMxDocument)m_app.Document;
                        ESRI.ArcGIS.SystemUI.IOperationStack operationStack;
                        operationStack = doc.OperationStack;
                        operationStack.Do(schematicOperation);
                        schematicOperation.StartOperation("Digit", m_app, m_schematicLayer, true);

                        //do abort operation
                        abortOperation = true;

                        schematicInMemoryFeatureLink = schematicInMemoryFeatureClass.CreateSchematicInMemoryFeatureLink((ESRI.ArcGIS.Schematic.ISchematicInMemoryFeatureNode)m_schematicFeature1, (ESRI.ArcGIS.Schematic.ISchematicInMemoryFeatureNode)m_schematicFeature2, "");
                        //schematicInMemoryFeatureLink.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusNew; if we want the node deleted after update
                        schematicInMemoryFeatureLink.UpdateStatus = esriSchematicUpdateStatus.esriSchematicUpdateStatusLocked;

                        abortOperation = false;
                        schematicOperation.StopOperation();

                        ISchematicFeature schematicFeature = schematicInMemoryFeatureLink;
                        m_dockableDigit.FillValue(ref schematicFeature);

                        //End Feedback
                        EndFeedBack();

                        m_schematicLayerForLink = null;

                        if (m_dockableDigit.AutoClear())
                        {
                            m_dockableDigit.SelectionChanged();
                        }
                    }
                }

                //Refresh the view and viewer windows
                RefreshView();
            }
            catch (System.Exception e)
            {
                if (abortOperation && (schematicOperation != null))
                {
                    schematicOperation.AbortOperation();
                }

                EndFeedBack();
                System.Windows.Forms.MessageBox.Show(e.Message);
            }

            return;
        }