SampleFieldPlugin
- Last UpdatedJul 05, 2024
- 2 minute read
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using OASySDNA.Common;
using OASySDNA.ADE.ClientForms;
using OASySDNA.ADE.Controls;
using OASySDNA.Common.DAL.DBEditCommon;
namespace OASySDNA.ADE.SamplePlugIns
{
/// <summary>
/// Sample Field Plug In
/// </summary>
class SampleFieldPlugIn
{
Form form;
DataSetFieldCType thisField;
string args;
int firstChange = 0;
/// <summary>
/// Constructor
/// </summary>
public SampleFieldPlugIn()
{
}
/// <summary>
/// This is the method that is called for the field plug in.
/// </summary>
/// <param name="form">The ADERowEditForm that is currently being built </param>
/// <param name="currentSubGroup">The current sub group (the group box)</param>
/// <param name="currentSubSubGroup">The current sub sub group (can be null)</param>
/// <param name="currentItem">The current item</param>
/// <param name="argStr"></param>
public void AddRule(
ADERowEditForm form,
DataSetSubGroupCType currentSubGroup,
DataSetSubSubGroupCType currentSubSubGroup,
object currentItem,
string argStr)
{
//MessageBox.Show("SampleFieldPlugIn:AddRule " + argStr);
DNALog.WriteLog(DNALog.LogLevel.Verbose, "SampleFieldPlugIn:AddRule " + argStr);
this.form = form as ADEBaseEditForm;
this.args = argStr;
// These settings will determine the kind of item it is (field, button, or comment)
//
thisField = currentItem as DataSetFieldCType;
// If this is not a DataSetFieldCType, return
if (thisField == null)
{
return "";
}
// Field PlugIns are called once for each array.
// This is so the programmer can do one operation for the entire array or
// do an operation for each element.
// The example below, is doing an operation for each element in the array.
//
if (currentSubGroup.IsArray)
{
// Some arrays reserve the first few rows for other uses (aceconfig). Handle this.
int arrayStartIndex;
int maxIndexToDisplay;
DBEditCommon.GetArrayRangeToDisplay(currentSubGroup, out arrayStartIndex, out maxIndexToDisplay);
string arrayName = currentSubGroup.Name;
for (int i = arrayStartIndex; i < maxIndexToDisplay; i++)
{
// Build the name of the field in the db
string fieldName = ADEBaseEditForm.GetArrayFieldName(arrayName, i, thisField.Name);
if (AddChangedEventToControl(fieldName) == false)
return;
}
}
else
{
AddChangedEventToControl(thisField.Name);
}
}
private bool AddChangedEventToControl(string fieldName)
{
Control cntl = null;
cntl = ADEBaseEditForm.FindControl(form.Controls, fieldName, typeof(ADECheckBoxString));
if (cntl != null)
{
ADECheckBoxString cmb = cntl as ADECheckBoxString;
cmb.CheckedChanged += new EventHandler(item_Changed);
cmb.TextChanged += new EventHandler(item_Changed);
firstChange += 2;
return true;
}
cntl = ADEBaseEditForm.FindControl(form.Controls, fieldName, typeof(ADEComboBox));
if (cntl != null)
{
ADEComboBox cmb = cntl as ADEComboBox;
cmb.SelectedValueChanged += new EventHandler(item_Changed);
firstChange++;
return true;
}
cntl = ADEBaseEditForm.FindControl(form.Controls, fieldName, typeof(ADETextBoxButton));
if (cntl != null)
{
ADETextBoxButton tb = cntl as ADETextBoxButton;
tb.Changed += new ADETextBoxButton.ChangedEventHandler(item_Changed);
firstChange++;
return true;
}
// Note: Check for the basic types last as CheckBoxString has
// both a checkbox and a text box with the same name.
//
cntl = ADEBaseEditForm.FindControl(form.Controls, fieldName, typeof(CheckBox));
if (cntl != null)
{
CheckBox cmb = cntl as CheckBox;
cmb.CheckedChanged += new EventHandler(item_Changed);
firstChange++;
return true;
}
cntl = ADEBaseEditForm.FindControl(form.Controls, fieldName, typeof(TextBox));
if (cntl != null)
{
TextBox cmb = cntl as TextBox;
cmb.TextChanged += new EventHandler(item_Changed);
firstChange++;
return true;
}
MessageBox.Show("Could not find control for " + fieldName);
DNALog.WriteLog(DNALog.LogLevel.Always, "Controls for form");
ADEBaseEditForm.DumpControls(form.Controls, "");
return false;
}
private void item_Changed(object sender, EventArgs e)
{
// Ignore the first change due to the databind.
if (firstChange > 1)
{
firstChange--;
return;
}
string fieldName = thisField.Name;
Control ctl = sender as Control;
if (ctl != null)
{
fieldName = ctl.Name;
}
MessageBox.Show("item changed " + args + " for " + fieldName);
}
}
}