i want to redefine an event, i mean, i have this:
boton.Click += infoApp;
//i think thats similar to: boton.Click = new System.EventHandler(infoApp).
when button 'boton' is clicked, the function/method 'infoApp' triggers,
this method its something like this:
private void infoApp(object sender, EventArgs e)
{
/* my code*/
}
Until here, evetithing goes well; but I NEED to send another parameter to that method:
boton.Click += infoApp(string par)
so i thought that this could work:
private void infoApp(object sender, EventArgs e, string par)
{
/*My code*/
}
but it doesn't.
I've readen things like delegates but i don't understand; and i dont know what to do in order to solve my problem; any ideas?
Thanks in advance
pd: sorry by my terrible english, i'm not english speaker. Try to be simple explaining. I'm using VS2008.
One way you could solve this, would be to wrap your event handler in a closure:
Instead of this line:
boton.Click += infoApp;
Do this:
string par = "something";
boton.Click += (sender, e) => {
//Now call your specific method here:
infoApp(sender, e, par);
};
Related
I tried to create a USB controller class and just tried to expose my internal EventArrivedEventHandler from ManagementEventWatcher to allow the consumer to do something if a USB is detected.
I had expected to be able to cast the EventArrivedEventHandler to a EventHandler, as they are all just delegates... but apparently not.
Is there a reason why this is not possible?
EDIT: I have found an approach that lets me do what I wanted very cleanly.
_watcher.EventArrived += (sender, eventArgs) => DeviceDetected?.Invoke(null, null);
The reason why this is not possible is that EventArraivedEventHandler and EventHandler have different signatures. As you can see, the EventArrivedEventHandler take as second argument EventArrivedEventArgs and not EventArgs as EventHandler does.
public delegate void EventArrivedEventHandler(object sender, EventArrivedEventArgs e)
In theory it should be possible to cast this to an EventHandler<EventArrivedEventArgs>.
Visit the MSDN Page for EventArivedEventHandler and EventArrivedEventArgs for further details about this issue.
public event EventHandler DriveDetected;
private void workaround(object sender, EventArrivedEventArgs e)
{
DriveDetected?.Invoke(sender, e as EventArgs);
}
watcher.EventArrived += new EventArrivedEventHandler(workaround);
Based on your post. Cheers.
I have 15 images on my WPF application. I want it so that whenever MouseUp on any of the images is called.. it'll call the same method.
I would like to do something similar to the psuedo code written here.. This would save so much time instead of writing 15 individual methods for each button. How can I do something like this?
private void BluePick1_Image_MouseUp_1(object sender, MouseButtonEventArgs e)
{
sender.ImageSource = something;
}
thank you for any help
if your event is always on a button :
private void ButtonMouseUp(object sender, MouseButtonEventArgs e) {
((Button)sender).ImageSource = something;
}
and
button1.MouseUp += ButtonMouseUp;
button2.MouseUp += ButtonMouseUp;
In my program I would like to call to a SelectedItemChanged event using c# code-behind, I am just unsure about what to pass as parameters. This is for a TreeViewItem.
//Gets selected item in TreeView
private void TreeOne_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
MainWindowViewModel.SelectedItem = e.NewValue as TreeViewItem;
}
//I'm calling the SelectedItemChanged event from a RightButtonDown event
private void TreeOne_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
TreeOne_SelectedItemChanged(/* What would go here? **/);
}
Also, when I try to build this I receive this compiler error that pretty much led to this question...
No overload for method TreeOne_SelectedItemChanged takes '0' arguments
I'm hoping that this is an easy question, but if I have not provided enough information, or haven't been clear enough please let me know.
Adding to #Bart Friederichs' answer and assuming that you have a reference to your TreeView, you could add the following method:
private void SetSelectedItem()
{
MainWindowViewModel.SelectedItem = TreeOne.SelectedItem;
}
Then you can simply call this from wherever you like:
private void TreeOne_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
SetSelectedItem();
}
private void TreeOne_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
SetSelectedItem();
}
The usual design pattern would be to call some kind of processing method, and not to "manually" fire events:
private TreeOne_SelectedItemChaned(object sender,
RoutedPropertyChangedEventArgs<object> e) {
processChange();
}
Then, from withing your code, you just call processChange(), no need to call TreeOne_SelectedItemChanged.
try to call
TreeOne_SelectedItemChanged(null, null);
I have a class named MifareReader. I instantiate it as Global So i Have on my form_load:
MifareReader mf = new MifareReader()
private void Main_Load(object sender, EventArgs e)
{
mf.MyEvent += new EventName(My_Method);
Connect();
}
private void My_Method()
{
//Code Here
}
private void Connect()
{
//Some Code Here
mf.MyEvent += new EventName(My_Method); //The same code of the Main_Load
}
Now let me explain. On my Main_Load I've set the event MyEvent and set it's method to My_Method Right ? Also, I called the other method Connect(), this methods repeat what i've done on the Main_Load
mf.MyEvent += new EventName(My_Method);
Right ?
So, I don't know why, but if I do not repeat this code, the application DOES NOT fire the MyEvent without Closing/Reopening the application.
Ok, its working perfect the way it is, but when I close/reopen my application, it fires MyEvent twice. So, Is there a way to work around this ?
Maybe check if the mf.MyEvent has already a method set to it?
As the title really, I'm in one part of my code and I would like to invoke any methods that have been added to the Button.Click handler.
How can I do this?
Do you mean you need to access it from elsewhere in your code? It may be an idea to refactor that section to it's own method then call that method whenever you need to access it (including in the Click event)
AVOID. Really. Seems like you handle some important logic right in the event handler.
Move the logic out of the handler.
You can do it via reflection..
Type t = typeof(Button);
object[] p = new object[1];
p[0] = EventArgs.Empty;
MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
m.Invoke(btnYourButton, p);
You will need an event to act as a proxy, but you are pretty much better off just refactoring your code.
private EventHandler ButtonClick;
protected override void CreateChildControls()
{
base.CreateChildControls();
m_Button = new Button{Text = "Do something"};
m_Button.Click += ButtonClick;
ButtonClick += button_Click;
Controls.Add(m_Button);
}
private void MakeButtonDoStuff()
{
ButtonClick.Invoke(this, new EventArgs());
}
private void button_Click(object sender, EventArgs e)
{
}
Do not do this if you really dont need it. It will make a mess of your code.