すべてのコントロールにプログラムで残されたエラーパバイダーアイコンを設定する方法

StackOverflow https://stackoverflow.com/questions/2864638

  •  30-09-2019
  •  | 
  •  

質問

ソフトウェアに1つのベースフォームクラスを備えた派生フォームクラスを使用します。

派生したフォームでは、Databindを広範囲に使用してBusinessObjectsに対処します。すべてがIdataerrorinfoを実装し、ErrorProvidersを使用してGUIに誤った入力にカスタムエラーメスを投げます。

ここで、ベースフォームクラスに関数を実装して、フォーム上のすべてのエラープロバイダーコンポーネントを取得し、フォーム上のすべてのコントロールのアイコンアライメントを左に設定する方法を検索します(右は間隔の問題であるため)。

どんなヒントを歓迎します...

ICONALIGNMENTを設定するためのコード:

private void SetErrorProviderIconAlignment(ErrorProvider errorProvider, Control control)
{
    errorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);

    foreach (Control subControl in control.Controls)
    {
        SetErrorProviderIcon(errorProvider, subControl);
    }
}
役に立ちましたか?

解決

継承を使用しました ErrorProvider 代わりに、ICONALIGNMENT拡張プロパティのデフォルトを強制的に設定/返すコンポーネント。

例えば

[ToolboxBitmap(typeof(ErrorProvider))]
[ProvideProperty("IconAlignment", typeof(Control))]
public class MyErrorProvider : ErrorProvider
{
    #region Base functionality overrides

    // We need to have a default that is explicitly different to 
    // what we actually want so that the designer generates calls
    // to our SetIconAlignment method so that we can then change
    // the base value. If the base class made the GetIconAlignment
    // method virtual we wouldn't have to waste our time.
    [DefaultValue(ErrorIconAlignment.MiddleRight)]
    public new ErrorIconAlignment GetIconAlignment(Control control)
    {
        return ErrorIconAlignment.MiddleLeft;
    }

    public new void SetIconAlignment(Control control, ErrorIconAlignment value)
    {
        base.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
    }

    #endregion
}

その後、検索/交換を簡単に実行できます new ErrorProvider() と交換します new MyErrorProvider().

私は正確に思い出すことはできませんが、渡された値を再指定するためにフォームのデザイナーを開く必要があるかもしれないかもしれません SetIconAlignment の中に form.designer.cs ファイル...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top