2016-11-30 23 views
0

私はXtraGridを持っていて、別の列の値によってはいくつかの列がマスクされることがあります。
私は、CustomRowCellEditイベントのエディタでマスクを設定することでそれを達成できました。設定不可能なコントロールでwndProcをオーバーライドする方法

以下Snipet:

private void myGridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) 
{ 
    if (e.Column.FieldName == "MyTargetField") 
    { 
     GridView gv = sender as GridView; 
     var typeValue = gv.GetRowCellValue(e.RowHandle, gv.Columns["MaskOrNotDependsOnThisField"]); 

     if (typeValue == DynamicMask) 
     { 

      var maskToApply = GetMaskForThisValue(gv.GetRowCellValue(e.RowHandle, gv.Columns["MyTargetField"])); 

      RepositoryItemTextEdit riMaskedTextEdit = new RepositoryItemTextEdit(); 
      riMaskedTextEdit.Mask.MaskType = MaskType.Simple; 
      riMaskedTextEdit.Mask.UseMaskAsDisplayFormat = true; 
      riMaskedTextEdit.Mask.SaveLiteral = false; 
      riMaskedTextEdit.Mask.EditMask = maskToApply; 
      e.RepositoryItem = riMaskedTextEdit; 
      //riMaskedTextEdit.KeyDown += EditorKeyDownDinamicMask; 
     } 
    } 
} 

それは魅力のように働いたが、いくつかのマスクは(ちょっと、私はrequerimentsをしないでください!)動的にすることができます。
このイベントハンドラの行のコメントを外してKeyDownEventHandlerを作成しました。

は、ユーザーがのような入力を続けるとしての私のマスクの変更を言ってみましょう:7桁の数字8桁ため

999(999)/ 99のための6桁

999.999から9のための

999.999を

メモマスクでValueChangedイベントが発生しないようにするため、KeyDownを使用する必要があります。
私はキーダウンを処理し、このことをうまく動作させることができました。

質問:貼り付けイベントの処理方法は? (キーボードとマウスの両方ペーストイベントから)

私がコントロールにWNDPROCを上書きする方法を知って、私はすでに前にそれを数回やりました。
ここに問題がありますRepositoryItemTextEditクラスはオーバーライドするwndProcを持つコントロールではありません。
私は少し掘っ上記のクラスは

public TextEdit OwnerEdit { get; } 

を持っており、それがイベントを処理本当の男だ見つけました。
残念ながら、devxpressは設定できないメンバーとして残しました。

今では、マスクが貼り付けられた値を切り捨てるため、999.999の値をコピー貼り付けの999.999-9に置き換えることはできません。
は、それはまた、TextEditクラスのインプレースインスタンスのプロパティを保持するために使用され

答えて

0

RepositoryItemTextEditクラスを発射するValueChagedイベントを防ぎます。技術的にTextEditクラスは、TextBoxMaskBoxクラスのインスタンスを保持するボックスです。 TextBoxMaskBoxクラスはSystem.Windows.Forms.TextBoxクラスから継承されています。
したがって、独自のRepositoryItemTextEdit,TextEditTextBoxMaskBox子孫を作成し、自分のTextBoxMaskBox子孫にWndProcメソッドを上書きすることができます。 OnPasteイベントがRepositoryItemPasteHandlerEditクラスに追加され、この例では

[UserRepositoryItem("RegisterPasteHandlerEdit")] 
public class RepositoryItemPasteHandlerEdit : RepositoryItemTextEdit 
{ 
    static RepositoryItemPasteHandlerEdit() 
    { 
     RegisterPasteHandlerEdit(); 
    } 

    static private readonly object EventPaste; 

    public const string CustomEditName = "PasteHandlerEdit"; 

    public RepositoryItemPasteHandlerEdit() { } 

    public override string EditorTypeName => CustomEditName; 

    public static void RegisterPasteHandlerEdit() 
    { 
     Image img = null; 
     EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(PasteHandlerEdit), typeof(RepositoryItemPasteHandlerEdit), typeof(TextEditViewInfo), new TextEditPainter(), true, img)); 
    } 

    public override void Assign(RepositoryItem item) 
    { 
     BeginUpdate(); 
     try 
     { 
      base.Assign(item); 
      var source = item as RepositoryItemPasteHandlerEdit; 
      if (source == null) return; 

      Events.AddHandler(EventPaste, source.Events[EventPaste]); 
     } 
     finally 
     { 
      EndUpdate(); 
     } 
    } 

    public event PasteEventHandler OnPaste 
    { 
     add 
     { 
      Events.AddHandler(EventPaste, value); 
     } 
     remove 
     { 
      Events.RemoveHandler(EventPaste, value); 
     } 
    } 

    protected internal void RaiseOnPaste(PasteEventArgs e) 
    { 
     if (IsLockEvents) 
      return; 

     var handler = (PasteEventHandler)Events[EventPaste]; 
     handler?.Invoke(GetEventSender(), e); 
    } 
} 

[ToolboxItem(true)] 
public class PasteHandlerEdit : TextEdit 
{ 
    static PasteHandlerEdit() 
    { 
     RepositoryItemPasteHandlerEdit.RegisterPasteHandlerEdit(); 
    } 

    public PasteHandlerEdit() { } 

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    public new RepositoryItemPasteHandlerEdit Properties => base.Properties as RepositoryItemPasteHandlerEdit; 

    public override string EditorTypeName => RepositoryItemPasteHandlerEdit.CustomEditName; 

    protected override TextBoxMaskBox CreateMaskBoxInstance() => new PasteHandlerMaskBox(this); 

    protected override void CreateMaskBox() 
    { 
     base.CreateMaskBox(); 

     var pasteHandlerMaskBox = MaskBox as PasteHandlerMaskBox; 
     pasteHandlerMaskBox.OnPaste += PasteHandlerMaskBox_OnPaste; 
    } 

    private void PasteHandlerMaskBox_OnPaste(object sender, PasteEventArgs e) => Properties.RaiseOnPaste(e); 
} 

public class PasteHandlerMaskBox : TextBoxMaskBox 
{ 
    private const int WM_PASTE = 0x0302; 

    public PasteHandlerMaskBox(TextEdit ownerEdit) : base(ownerEdit) { } 

    public event PasteEventHandler OnPaste; 

    protected override void WndProc(ref Message msg) 
    { 
     if (msg.Msg == WM_PASTE) 
     { 
      var e = new PasteEventArgs(); 

      OnPaste?.Invoke(this, e); 

      if (e.Cancel) 
       return; 
     } 

     base.WndProc(ref msg); 
    } 
} 

public class PasteEventArgs : CancelEventArgs 
{ 
    public PasteEventArgs() { } 
    public PasteEventArgs(bool cancel) : base(cancel) { } 

    public string Text => Clipboard.GetText(); 
} 

public delegate void PasteEventHandler(object sender, PasteEventArgs e); 

:ここ
は一例です。これを使用できます:

private void repositoryItemPasteHandlerEdit1_OnPaste(object sender, PasteEventArgs e) 
{ 
    MessageBox.Show(e.Text); 

    if (...) // Perform some checks. 
     e.Cancel = true; 
} 
関連する問題