0
votes

I have to work with touch monitors and sometimes with mouse and normal monitors.

So for drag and drop the for the first would be

private void lvAllowedPPtab2_StylusButtonDown(object sender, StylusButtonEventArgs e)

and for the second

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)

after that I have to execute the same code using sender and e.

I didn't get to make a common code routine. The two event are similar and both have the GetPosition event.

I might have taken the wrong road but I have tought to something like:

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

but then I don't know how to cast e to event type.

Thank you

1
you have to do : if (_e is StylusButtonEventArgs) var eventargs = _e as StylusButtonEventArgs;Ehsan Sajjad
You don't need the type. Just cast it and get your position.juharr
var can't be embedded in if statementLuca
"I didn't get to make a common code routine" - why? It would be the cleanest solution. Create a method and call it from both handlers, e.g.: HandleDownInput(e.GetPosition()).Eli Arbel

1 Answers

1
votes

you can call them both with that

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

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

and then tell inside common code

    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));

    }

Better implementation (thanks to Eli Arbel):

    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

    }