I have this code that I attach to the DoubleClick event on the Tray Icon for my app:
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
this.MainWindow.Show();
};
But, is it possible to use this code for two events (DoubleClick and Click), like so:
ni.DoubleClick, ni.Click +=
delegate(object sender, EventArgs args)
{
this.MainWindow.Show();
};
Just for minimalize a code size and readability. Thanks
Put the handler in its own function:
private void ClickHandler(object sender, EventArgs args)
{
this.MainWindow.Show();
}
Then wire it up to both events:
ni.DoubleClick += ClickHandler;
ni.Click += ClickHandler;
Just create EventHandler using lambda expression and add it to both event.
EventHandler e = (sender, args) => this.MainWindow.Show();
ni.DoubleClick += e;
ni.Click += e;
No, but you can make it a standard, non anonymous function and use it for both events.
private void OnClick(object sender, EventArgs e) { ... }
ni.DoubleClick += OnClick;
ni.Click += OnClick;
Just assign it to a variable beforehand:
EventHandler eventHandler = delegate(object sender, EventArgs args)
{
this.MainWindow.Show();
};
ni.DoubleClick += eventHandler;
ni.Click += eventHandler;
BTW, the event handler definition can be simplified using the anonymous method syntax:
EventHandler eventHandler = (s, e) => this.MainWindow.Show();
Related
Tell me a method by which in C # it would be possible to generate a GridView Event, for example, FocusedRowChanged, like the on ..... methods for WinForms?
If you want to create an Event like FocusedRowChanged
You can use this codes
//using DevExpress.XtraGrid.Views.Base;
//the All is ok
public event FocusedRowChangedEventHandler MyFocusedRowChanged;
//Or
public event EventHandler<FocusedRowChangedEventArgs> MyFocusedRowChanged2;
If you want to handle the FocusedRowChanged event,
You can use this codes
private void YourForm_Load(object sender, EventArgs e)
{
gridView.FocusedRowChanged += GridView_FocusedRowChanged;
this.MyFocusedRowChanged += GridView_FocusedRowChanged;
this.MyFocusedRowChanged2 += GridView_FocusedRowChanged;
}
private void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
//your code
throw new NotImplementedException();
}
If you want to raise FocusedRowChanged,
You can use this codes
private void YourForm_Load(object sender, EventArgs e)
{
//gridView.FocusedRowChanged += GridView_FocusedRowChanged;
//this.MyFocusedRowChanged += GridView_FocusedRowChanged;
//this.MyFocusedRowChanged2 += GridView_FocusedRowChanged;
//invoke the handle method
GridView_FocusedRowChanged(gridView, new FocusedRowChangedEventArgs(-1, gridView.FocusedRowHandle));
//or change focused row to fire event
gridView.FocusedRowHandle++;
}
Which one do you mean?
private void createButton()
{
flowLayoutPanel1.Controls.Clear();
for (int i = 0; i < 4; i++)
{
Button b = new Button();
b.Name = i.ToString();
b.Text = "Button" + i.ToString();
flowLayoutPanel1.Controls.Add(b);
}
}
private void button1_Click(object sender, EventArgs e)
{
createButton();
}
I Used this code to create some buttons on runtime , now how can i use those created buttons to perform diffrent actions? Im kindz new to this so please help me , very much appreciated :)
You can assign an event handler to the click event:
b.Click += SomeMethod;
SomeMethod must have the following signature:
void SomeMethod(object sender, EventArgs e)
b.Click += delegate(object sender, EventArgs e) {
Button clickedButton = (Button)sender; //gets the clicked button
});
When you create your button, you need to subscribe to the Click event like this :
Button b = new Button();
b.Click += new EventHandler(b_Click);
// or
b.Click += b_Click;
// or
b.Click += delegate(object sender, EventArgs e) {/* any action */});
// or
b.Click += (s, e) => { /* any action */ };
void b_Click(object sender, EventArgs e)
{
// any action
}
This is something that is automatically done when your are is the designer in Visual Studio, and you click on a button to create the method button1_Click.
You can search in the Designer.cs of your form, you will find an equivalent line:
button1.Click += new EventHandler(button1_Click);
Related question:
How can i create dynamic button click event on dynamic button
In Visual Basic I knew how to do it, but I'm new to C#, so can you guys tell me how do I make a "private void" with mouse hover that applies the same event to multiple controls? There's an example:
private void button1, button2, button3, button4_MouseHover(object sender, EventArgs e)
{
btn.Image = pic
}
Just declare one event handler and point each button at it:
private void Common_MouseHover(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
btn.Image = pic
}
Then in code or designer:
button1.MouseHover += Common_MouseHover;
button2.MouseHover += Common_MouseHover;
// .. etc
When you subscribe to the event on a button, it's just a standard event handler:
button1.Click += myEventHandler;
You can use the same code to add handlers for every button:
button1.Click += myEventHandler;
button2.Click += myEventHandler;
button3.Click += myEventHandler;
button4.Click += myEventHandler;
button5.Click += myEventHandler;
button6.Click += myEventHandler;
button1.MouseOver += OnMouseOver(...)
button2.MouseOver += OnMouseOver(...)
button3.MouseOver += OnMouseOver(...)
button4.MouseOver += OnMouseOver(...)
I have a button which slides out a menu using a storyboard by calling begin() on it like so
private void ShareBtn_Click(object sender, RoutedEventArgs e)
{
SlideIn.Begin();
}
On the grid which then slides out there are buttons.
Each button then slides the grid back and when that storyboard completes the action for the button then runs so like so,
private void PictureBtn_Click(object sender, RoutedEventArgs e)
{
CertificateDisplay.SaveAsPicture();
}
private void FacebookBtn_Click(object sender, RoutedEventArgs e)
{
App.facebookSuccess = false;
NavigationService.Navigate(new Uri("/FBLogin.xaml", UriKind.Relative));
}
private void SMSBtn_Click(object sender, RoutedEventArgs e)
{
SlideOut.Begin();
SlideOut.Completed += delegate(object s, EventArgs se) { SlideOut_Completed(s, se, "Email"); };
}
private void EmailBtn_Click(object sender, RoutedEventArgs e)
{
SlideOut.Begin();
SlideOut.Completed += delegate(object s, EventArgs se) { SlideOut_Completed(s, se, "Email"); };
}
void SlideOut_Completed(object sender, EventArgs e, String shareType)
{
switch (shareType)
{
case "Email":
...
default:
break;
}
}
The flaw I encountered if that I cannot remove the anonymous functions from the event stack.
I've managed to solve it by making shareType a common variable for all of the above functions and not using a anonymous delegate and then removing the "named" functions from the event stack when OnNavigatedFrom is called.
Is there a way to do this by still using those delegates because it looks neater?
One option is to remove it within the handler itself:
EventHandler handler = null;
handler = delegate(object s, EventArgs se) {
SlideOut_Completed(s, se, "Email");
SlideOut.Completed -= handler;
};
SlideOut.Completed += handler;
SlideOut.Begin();
Why assign the Completed event handler EmailBtn_Click at each click? Do it in the form constructor or in the form load event.
I have this snippet
private void westButton_click(object sender, EventArgs ea)
{
PlayerCharacter.Go(Direction.West);
}
repeated for North, South and East.
How can I declare a function that'd let me generate methods like ir programmatically?
e.g., I'd like to be able to call
northButton.Click += GoMethod(Direction.North);
instead of defining the method, and then
northButton.Click += new EventHandler(northButton.Click);
northButton.Click += (s, e) => GoMethod(Direction.North);
(or...)
northButton.Click += (s, e) => PlayerCharacter.Go(Direction.North);
I seem to have found a solution:
private EventHandler GoMethod(Direction dir)
{
return new EventHandler((object sender, EventArgs ea) => PlayerCharacter.Go(dir));
}
My only concern would be about binding time; if PlayerCharacter is null when I call GoMethod, what would happen?
northButton.Click += (s,e) => GoMethod(Direction.North);