# Item Changes

## ItemChanging

Manipulate the behavior of the control before the change has taken place. This method can be found in wndProjektPainter (Shortcut: **PP**)

ItemChanging can return different values:

* ItemChangingResult.Continue: validates the changing and continues&#x20;
*

    ItemChangingResult.Reject: does not reject the value, keeps the wrong one and does not continue until a valid value is given
*

    ItemChangingResult.RejectAndContinue: reject the changes and continues; keeps the previous value of tha variable;

{% hint style="success" %}
Best practice: use RejectAndContinue.
{% endhint %}

```csharp
public override ItemChangingResult ItemChanging(UIElement container, FrameworkElement element, object selectedItem, object newValue, object oldValue)
{
    if (element == tbTerminJahr)
    {
        var newJahr = newValue.ToInt(0);
        if (newJahr < 1900 || newJahr > 2100)
        {
            LoggerSink.PushMessage(this, "Ungültiges Jahr", Severity.Warning);
            return ItemChangingResult.Reject;
        }
    }
    else if (element == tbTerminKW)
    {
        var newKw = newValue.ToInt(0);
        if (newKw < 1 || newKw > 54)
        {
            LoggerSink.PushMessage(this, "Ungültige KW", Severity.Warning);
            return ItemChangingResult.Reject;
        }
    }

    return base.ItemChanging(container, element, selectedItem, newValue, oldValue);
}
```

## ItemChanged

Behavior after changing value of an control.&#x20;

{% hint style="warning" %}
Since v16 ItemChanged took over the functionality of ItemSearched.
{% endhint %}

{% hint style="success" %}
Best-practice: Group your code by selecting the container (DataPanel, DataGrid), followed by the control from the container (TextBox, DataGrid Column).
{% endhint %}

```csharp
public override void ItemChanged(UIElement container, FrameworkElement element, object selectedItem, object newValue, object oldValue)
{
    base.ItemChanged(container, element, selectedItem, newValue, oldValue);
        
    if (container == dpKopf)         //DataPanel
    {
        if (element == tbsBetriebsauftrag)           //TextBoxSearch
        {
            var babTyp = tbsBetriebsauftrag.ResultRow["typ"].ToInt(0);
            var babNr = tbsBetriebsauftrag.ResultRow["nr"].ToInt(0);

            if (babTyp > 0 && babNr > 0)
            {
                //Clearview, Retrieve and Set Focus
                NewRueckmeldung(babTyp, babNr);
            }
        }
        else if (element == tbsMitarbeiter)
        {
            var mitarbId = newValue.ToStringNN();
            if (!mitarbId.IsNullOrEmpty())
            {
                ChangeMitarbeiter(mitarbId);
            }
        }
    }
    else if (container == dgZeit)             //DataGrid
    {
        if (element.In(dgtbMenge, dgtbAusschuss))            //DataGrid Column
        {
            RefreshMenge();
        }
        else if (element == dgtbIstzeit)
        {
            RefreshRueckmRes();
            RefreshEffektivZeit();
        }
    }
}
```

Example where in ItemChanged event we can access the searched row/rows via **searchControl.ResultRow/ExtraRows**:

{% hint style="info" %}
The full implementation of this example can be found in PKP.
{% endhint %}

<pre class="language-csharp"><code class="lang-csharp">public override void ItemChanged(UIElement container, FrameworkElement element, object selectedItem, object newValue, object oldValue)
{
    base.ItemChanged(container, element, selectedItem, newValue, oldValue);
 
    //...
    
<strong>    else if (element == tbsAfoArbgkatNr)
</strong>    {
	RetrieveAFOArbgkatMulti((xTextBoxSearch)element);
    }
<strong>    else if (element == tbsAfoArbgkatDG)
</strong>    {
	xTextBoxSearch tbsArbgkat = ((DxTextBoxSearchColumn)element).ActiveTextBoxSearch;
	RetrieveAFOArbgkatMulti(tbsArbgkat);
    }
}

public virtual void RetrieveAFOArbgkatMulti(xTextBoxSearch searchControl)
{
    if (searchControl == null) return;
    if (dxgarbpl_afo.SelectedRow == null) return;
    dsProdKontrolPlan.arbpl_afoRow selectedRow = dxgarbpl_afo.SelectedRow;

    BusObj.RetrieveAFOArbgkatMulti(selectedRow, searchControl.ResultRow, searchControl.ExtraRows);

    dpAplAfo.SetFocus(tbAfoBez1.Name);
    this.RefreshMdePanel();
}
</code></pre>
