2016-07-27 16 views
0

私はタッチモニターで作業する必要があります。マウスや通常のモニターで作業する必要があります。異なるイベントで同じコードを実行する方法

ドラッグのためにそう

ます。private void lvAllowedPPtab2_StylusButtonDown(オブジェクト送信者、StylusButtonEventArgs e)のだろう最初のドロップ

と第二

ます。private void ListBox_PreviewMouseLeftButtonDownため

(オブジェクト送信者、MouseButtonEventArgs e)

その後、送信者とeを使用して同じコードを実行する必要があります。

私は共通のコードルーチンを作ることはできませんでした。 2つのイベントは似ており、どちらもGetPositionイベントを持っています。

私が間違って道をとっているかもしれないが、私のような何かに取り払われている:

Type eventType; 
if (_e is StylusButtonEventArgs) 
    eventType = typeof (StylusButtonEventArgs); 
else 
    eventType = typeof(MouseEventArgs); 

が、その後、私はイベントの種類に電子をキャストする方法がわかりません。

は、あなたがその

private void listView_StylusButtonDown(object sender, StylusButtonEventArgs e) { CommonCode(sender, e); } 

    private void listView_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e) { CommonCode(sender, e); } 

でそれらの両方を呼び出してから、共通のコード内

private void CommonCode(object sender, object _e) 
    { 

     //Sender is common 
     ListView parent = (ListView)sender; 
     string strListViewButtonName = (sender as ListView).Name; 

     if (_e is StylusButtonEventArgs) 
      ... (_e as StylusButtonEventArgs).GetPosition(parent)); 
     else 
      ... (_e as MouseEventArgs).GetPosition(parent)); 

    } 

より良い実装(イーライArbelのおかげで)伝えることができ、あなたに

+1

する必要があります: '' if(_e is StylusButtonEventArgs) var eventargs = _e as StylusButtonEventArgs; '' –

+0

このタイプは必要ありません。ちょうどそれをキャストし、あなたの位置を取得します。 – juharr

+0

if文にvarを埋め込むことはできません – Luca

答えて

1

ありがとう:

private void listView_StylusButtonDown(object sender, StylusButtonEventArgs e) { CommonCode(sender, e.GetPosition((ListView)sender)); } 

    private void listView_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e) { CommonCode(sender, e.GetPosition((ListView)sender)); } 


    private void CommonCode(object sender, Point p) 
    { 
     //Sender is common 
     ListView parent = (ListView)sender; 
     string strListViewButtonName = (sender as ListView).Name; 

     //you don't need getPosition since P is known 

    } 
+2

本当にそのようなイベント引数をキャストする理由はありません。ハンドラからの位置を共通メソッドに渡すだけです。 –

+0

あなたは正しいです。それは良いかもしれない – Patrick

関連する問題