Client now enforcing field creations to guarantee client side fields always
authorsl
Tue, 16 Dec 2014 18:05:55 +0100
changeset 80408ef0501cb7
parent 79 76564df23849
child 81 9bcab0dfa376
Client now enforcing field creations to guarantee client side fields always
in sync.
Client/Client.cs
Client/MainForm.Designer.cs
Client/MainForm.cs
     1.1 --- a/Client/Client.cs	Tue Dec 16 16:35:55 2014 +0100
     1.2 +++ b/Client/Client.cs	Tue Dec 16 18:05:55 2014 +0100
     1.3 @@ -171,7 +171,7 @@
     1.4                      }
     1.5  
     1.6                      SetLayout(Layout);
     1.7 -                    SetFields(Fields);
     1.8 +                    iClient.SetFields(Fields);
     1.9                  }
    1.10                  finally
    1.11                  {
    1.12 @@ -196,34 +196,85 @@
    1.13              iClient.SetLayout(aLayout);
    1.14          }
    1.15  
    1.16 -
    1.17 -        public void SetField(DataField aField)
    1.18 +        /// <summary>
    1.19 +        /// Set the specified field.
    1.20 +        /// </summary>
    1.21 +        /// <param name="aField"></param>
    1.22 +        /// <returns>True if the specified field was set client side. False means you need to redefine all your fields using CreateFields.</returns>
    1.23 +        public bool SetField(DataField aField)
    1.24          {
    1.25 -            //TODO: Create fields if not present
    1.26              int i = 0;
    1.27 +            bool fieldFound = false;
    1.28              foreach (DataField field in Fields)
    1.29              {
    1.30                  if (field.Index == aField.Index)
    1.31                  {
    1.32                      //Update our field then
    1.33                      Fields[i] = aField;
    1.34 +                    fieldFound = true;
    1.35                      break;
    1.36                  }
    1.37                  i++;
    1.38              }
    1.39  
    1.40 +            if (!fieldFound)
    1.41 +            {
    1.42 +                //Field not found, make to use SetFields with all your fields at least once after setting your layout.
    1.43 +                return false;
    1.44 +            }
    1.45 +
    1.46              CheckConnection();
    1.47              iClient.SetField(aField);
    1.48 +            return true;
    1.49          }
    1.50  
    1.51 -        public void SetFields(System.Collections.Generic.IList<DataField> aFields)
    1.52 +        /// <summary>
    1.53 +        /// Use this function when updating existing fields.
    1.54 +        /// </summary>
    1.55 +        /// <param name="aFields"></param>
    1.56 +        public bool SetFields(System.Collections.Generic.IList<DataField> aFields)
    1.57 +        {
    1.58 +            int fieldFoundCount = 0;
    1.59 +            foreach (DataField fieldUpdate in aFields)
    1.60 +            {
    1.61 +                int i = 0;
    1.62 +                foreach (DataField existingField in Fields)
    1.63 +                {
    1.64 +                    if (existingField.Index == fieldUpdate.Index)
    1.65 +                    {
    1.66 +                        //Update our field then
    1.67 +                        Fields[i] = fieldUpdate;
    1.68 +                        fieldFoundCount++;
    1.69 +                        //Move on to the next field
    1.70 +                        break;
    1.71 +                    }
    1.72 +                    i++;
    1.73 +                }
    1.74 +            }
    1.75 +
    1.76 +            //
    1.77 +            if (fieldFoundCount!=aFields.Count)
    1.78 +            {
    1.79 +                //Field not found, make sure to use SetFields with all your fields at least once after setting your layout.
    1.80 +                return false;
    1.81 +            }
    1.82 +
    1.83 +            CheckConnection();
    1.84 +            iClient.SetFields(aFields);
    1.85 +            return true;
    1.86 +        }
    1.87 +
    1.88 +        /// <summary>
    1.89 +        /// Use this function when creating your fields.
    1.90 +        /// </summary>
    1.91 +        /// <param name="aFields"></param>
    1.92 +        public void CreateFields(System.Collections.Generic.IList<DataField> aFields)
    1.93          {
    1.94              Fields = aFields;
    1.95              CheckConnection();
    1.96              iClient.SetFields(aFields);
    1.97          }
    1.98  
    1.99 -
   1.100          public int ClientCount()
   1.101          {
   1.102              CheckConnection();
     2.1 --- a/Client/MainForm.Designer.cs	Tue Dec 16 16:35:55 2014 +0100
     2.2 +++ b/Client/MainForm.Designer.cs	Tue Dec 16 18:05:55 2014 +0100
     2.3 @@ -39,6 +39,7 @@
     2.4              this.buttonSetBitmap = new System.Windows.Forms.Button();
     2.5              this.buttonBitmapLayout = new System.Windows.Forms.Button();
     2.6              this.buttonIndicatorsLayout = new System.Windows.Forms.Button();
     2.7 +            this.buttonUpdateTexts = new System.Windows.Forms.Button();
     2.8              this.SuspendLayout();
     2.9              // 
    2.10              // buttonSetText
    2.11 @@ -145,11 +146,22 @@
    2.12              this.buttonIndicatorsLayout.UseVisualStyleBackColor = true;
    2.13              this.buttonIndicatorsLayout.Click += new System.EventHandler(this.buttonIndicatorsLayout_Click);
    2.14              // 
    2.15 +            // buttonUpdateTexts
    2.16 +            // 
    2.17 +            this.buttonUpdateTexts.Location = new System.Drawing.Point(257, 189);
    2.18 +            this.buttonUpdateTexts.Name = "buttonUpdateTexts";
    2.19 +            this.buttonUpdateTexts.Size = new System.Drawing.Size(75, 35);
    2.20 +            this.buttonUpdateTexts.TabIndex = 29;
    2.21 +            this.buttonUpdateTexts.Text = "Update Texts";
    2.22 +            this.buttonUpdateTexts.UseVisualStyleBackColor = true;
    2.23 +            this.buttonUpdateTexts.Click += new System.EventHandler(this.buttonUpdateTexts_Click);
    2.24 +            // 
    2.25              // MainForm
    2.26              // 
    2.27              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    2.28              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    2.29              this.ClientSize = new System.Drawing.Size(443, 252);
    2.30 +            this.Controls.Add(this.buttonUpdateTexts);
    2.31              this.Controls.Add(this.buttonIndicatorsLayout);
    2.32              this.Controls.Add(this.buttonBitmapLayout);
    2.33              this.Controls.Add(this.buttonSetBitmap);
    2.34 @@ -183,6 +195,7 @@
    2.35          private System.Windows.Forms.Button buttonSetBitmap;
    2.36          private System.Windows.Forms.Button buttonBitmapLayout;
    2.37          private System.Windows.Forms.Button buttonIndicatorsLayout;
    2.38 +        private System.Windows.Forms.Button buttonUpdateTexts;
    2.39      }
    2.40  }
    2.41  
     3.1 --- a/Client/MainForm.cs	Tue Dec 16 16:35:55 2014 +0100
     3.2 +++ b/Client/MainForm.cs	Tue Dec 16 18:05:55 2014 +0100
     3.3 @@ -139,16 +139,12 @@
     3.4  
     3.5          private void buttonSetText_Click(object sender, EventArgs e)
     3.6          {
     3.7 -            //iClient.SetText(0,"Top");
     3.8 -            //iClient.SetText(1, "Bottom");
     3.9 -            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
    3.10 -
    3.11              //Set one column two lines layout
    3.12              TableLayout layout = new TableLayout(1, 2);
    3.13              iClient.SetLayout(layout);
    3.14  
    3.15              //Set our fields
    3.16 -            iClient.SetFields(new DataField[]
    3.17 +            iClient.CreateFields(new DataField[]
    3.18              {
    3.19                  new DataField(0, textBoxTop.Text, Alignment),
    3.20                  new DataField(1, textBoxBottom.Text, Alignment)
    3.21 @@ -183,7 +179,7 @@
    3.22              }
    3.23  
    3.24              DataField field = new DataField(0, bitmap);
    3.25 -            field.ColumnSpan = 2;
    3.26 +            //field.ColumnSpan = 2;
    3.27              iClient.SetField(field);
    3.28          }
    3.29  
    3.30 @@ -228,7 +224,7 @@
    3.31              field.RowSpan = 2;
    3.32  
    3.33              //Set texts
    3.34 -            iClient.SetFields(new DataField[]
    3.35 +            iClient.CreateFields(new DataField[]
    3.36              {
    3.37                  field,
    3.38                  new DataField(1, textBoxTop.Text, Alignment),
    3.39 @@ -282,7 +278,7 @@
    3.40  
    3.41  
    3.42              //Set texts
    3.43 -            iClient.SetFields(new DataField[]
    3.44 +            iClient.CreateFields(new DataField[]
    3.45              {
    3.46                  textFieldTop,
    3.47                  indicator1,
    3.48 @@ -293,5 +289,21 @@
    3.49              });
    3.50  
    3.51          }
    3.52 +
    3.53 +        private void buttonUpdateTexts_Click(object sender, EventArgs e)
    3.54 +        {
    3.55 +
    3.56 +            bool res = iClient.SetFields(new DataField[]
    3.57 +            {
    3.58 +                new DataField(0, textBoxTop.Text, Alignment),
    3.59 +                new DataField(1, textBoxBottom.Text, Alignment)
    3.60 +            });
    3.61 +
    3.62 +            if (!res)
    3.63 +            {
    3.64 +                MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    3.65 +            }
    3.66 +
    3.67 +        }
    3.68      }
    3.69  }