Simulating events on UIElement's without inheriting on Windows Phone 7 - c#

I have a very simple requirement, i.e. I want to send a synthetic event to a UIElement, in this case, a Grid. What I want is simply that I be able to send a synthetic event to an UIElement.
For example,
StackPanel myPanel;
StackPanel topPanel;
topPanel.MouseLeftButtonUp += new MouseButtonEventHandler(topPanel_MouseLeftButtonUp);
private void topPanel_MouseLeftButtonUp(object sender, MouseEventArgs args) {
// Here I want to send the MouseLeftButtonUp event to myPanel
}
It is possible using RaiseEvent, but it is a protected event and hence I cannot just call it on an instance of any UIElement. So how do I go about sending a synthetic event on existing classes?
P.S: The reason that I cannot create custom inherited classes is that the current code base is too huge and the number of changes that will be required in case I take such an approach are not feasible.
Any help is greatly appreciated.
Regards,
roahn

Instead of raising events, you could just move the relevant code from the event handler to a method. Then, you can just call the method whenever you want to simulate a button click. However, if you want to simulate the button click on an element, you could do this:
//Assuming myPanel_MouseLeftButtonUp is the event handler for myPanel
myPanel_MouseLeftButtonUp(null, null);

Related

C# DotNetBar switchButton not working

So, I would double click here on my designer
and it should create me the code, but well it doesn't. And there is no value changed event in the events either.
So if anyone knows how to fix this, it would be nice. (I doubt it) so how would I get around this? How would I go on about creating the code myself that should be created when I double click on it?
Click the form or control that you want to create an event handler for.
In the Properties window(F4), click the Events button
In the list of available events, click the event that you want to create an event handler for.
In the box to the right of the event name, type the name of the handler and press ENTER.
Add the appropriate code to the event handler.
To create an event handler in the code editor
Switch to the code editor by using one of the following techniques:
Create a new method like:
private void buttonName_Click(object sender, EventArgs e) { ... }
In the file YourFormName.Designer.cs find your button and add
this.buttonName.Click += new System.EventHandler(this.buttonName_Click);

C# Composite Event

I am creating a UserControl with a other controls inside. I want to create some form of "composite" event to provide for the forms using the UserControl.
For example: I have a lot of panels inside of this UserControl. I am creating one EventHandler inside the UserControl to handle all MouseDown events of these panels. In the end I want to make all MouseDown events on the UserControl to appear as just MouseDown on the parent.
I could create a new Event like MousePressed on this UserControl, but I would rather go with the already existing name MouseDown. How do I have to declare this new event in order to "hide" the other MouseDown Event that already exists?
I hope I could explain my question enough.
Thanks in advance.
Register all events needed in the UserControl (except the event of the UserControl itself, this will be the one needed to register on the parent form).
Most events seem to have a method used to call them. You can call this method starting with "On". For example: OnMousePressed
// You can register all MouseDown Events you need in one EventHandler.
private void Item_MouseDown(object sender, MouseEventArgs e)
{
// Call this method to call the UserControls event.
this.OnMouseDown(e);
// Register to the UserControl's MouseDown event where you want to catch it.
}

Is there an elegant way to attach a single event handler to all the preview mouse events in WPF?

I am writing a WPF behavior that is supposed to block all mouse events that occur when the mouse is not inside a specific square. I wanted the behavior to attach a PreviewMouse*** event handler that checks the mouse position and sets Handled=true if the mouse is not inside the specific square. The problem is that there are a lot of different mouse events and I prefer that my code be elegant and clean. Is there a way to catch all preview mouse events in one elegant line?
Thnx,
What you can do is use reflection to attach to all events containing "PreviewMouse" in their name. To achieve that you can simply create an extension method for UIElement and implement it like this
public static class UIElementExtensions
{
public static void HandleAllPreviewMouse(this UIElement uiElement, RoutedEventHandler handler)
{
var elementType = uiElement.GetType();
foreach (var eventInfo in elementType.GetEvents().Where(ei => ei.Name.Contains("PreviewMouse")))
{
var specificHandler = Delegate.CreateDelegate(eventInfo.EventHandlerType, handler.Method);
eventInfo.AddEventHandler(uiElement, specificHandler);
}
}
}
And then in your Window code behind attach your handling code to your root layout Grid (or whavetever parent element you want to hook) using the extension
public MainWindow()
{
InitializeComponent();
var handler = new RoutedEventHandler(delegate(object sender, RoutedEventArgs e)
{
Console.WriteLine("Preview event fired");
// Uncomment if you want to block event propagation
//e.Handled = true;
});
this.LayoutRoot.HandleAllPreviewMouse(handler);
}
This solution is a one-liner solution to your issue but keep in mind it is not extra clean, especially on the part where events are filtered based on their name. But you can surely work it a bit to make it better.
PreviewMouse events are tunnelling events so they tunnel from root element to the actual sender. So, you can hook the PreviewMouse*** event handler on root element if you don't want to attach explicitly to all child elements.
And in handler you can check e.OriginalSource to get control which actually raises that event and can code accordingly.
<Grid PreviewMouseLeftButtonDown="Handler">
<Button/>
<TextBox/>
<TextBlock/>
<AnotherControl/>
</Grid>
Update for comment:
What I am looking for is a single C# line of code that attaches a
single event handler to many different mouse events on the same
element (PreviewMouseDown, PreviewMouseUp, PreviewLeftMouseDown,
PreviewLeftMouseUp, PreviewMouseWheel...). Instead of attaching the
same handler to each event specifically, I would like to attach it to
all of them at once.
I think you are complicating things here just to avoid some lines of code. Those events are independent of each other, so one or other way you have to assign a handler to it (may be with reflection as proposed in other answer). But, I don't think it's a nice approach to do that considering reflection is bit slower. Moreover, with that approach you will end up having same lines of code.
If lines of code is a concern, then you can encapsulate in a method and pass on common handler to it and hook the handler to interested events.
private void HookPreviewMouseEvents(MouseButtonEventHandler handler)
{
PreviewMouseDown += handler;
PreviewMouseLeftButtonUp += handler;
PreviewMouseUp += handler;
}

ListView events set in the properties don't work

I am trying to use the MouseClick event from the properties of a listView to handle left and right mouse clicks.
Unfortunately the event never seems to fire. (Double clicked on the event to create a property, entered a bit of simple code and placed a breakpoint on the first line). The same is true of several other events listed in the properties (ItemSelectionChanged seems to work but the other events I have tried don't fire.
Here is the code added:
In form.designer.cs:
this.listView1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseClick);
In form.cs:
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
Some code
}
That method never gets called when I click on the listview. The listview is inside a tab on top of the stack.
I guess I am probably forgetting something very basic but what?
ListView is a bit unusual, its MouseClick event doesn't fire unless you click an item in the view. Workaround is to use the MouseDown or MouseUp event instead. You typically are much more interested in the ItemSelectionChanged event btw. You probably need its HitTest() method to see exactly what was clicked if you use MouseDown/Up.

Registering a KeyPress Event Method in C# "DialPad.Designer.CS" page

Im designing a dialerPad form using Windows form, there is a textbox which should only take numbers and not text, my problem is that when i add the code
private void txtDialedNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//Blaah Blaah Code;
}
but its not getting registered in the other DialPad.Designer.CS page. For example the fallowing code registers TextChangedEvent
this.txtDailedNumber.TextChanged += new System.EventHandler(this.txtDailedNumber_TextChanged);
Can anybody help me on this?
You should never change *.designer.cs files manually.
What you should be doing is opening the design view of your form, selecting the object, and then setting the event handler in the objects properties:
Alternatively, if you want to register event handler manually (instead of using the designer), just put it under the InitializeComponent() call in the constructor for your form.
I would try to simplify Greg's answer.
Select the text box > go to properties > events tab> KeyPress Event > select the method this.txtDailedNumber.KeyPress
You are done.

Categories

Resources