In XAML I have a ListBox MenuItem that for its Click="" method I want to reuse an existing method but which is already declared with (object sender, ExecutedRoutedEventArgs e) parameters.
Using it as so gives the following error:
Error CS1503 Argument 2: cannot convert from 'System.Windows.RoutedEventArgs' to 'System.Windows.Input.ExecutedRoutedEventArgs'
Is it possible to convert ExecutedRoutedEventArgs to RoutedEventArgs for it to work? or any other way?
You can't change the signature of the event delegate - a click event handler only accepts a RoutedEventArgs and nothing else - but you can move the code from the existing event handler into a method that you can call from any event handlers, e.g.:
private void OnClick(object sender, ExecutedRoutedEventArgs e)
{
YourMethod();
}
private void OnClick1(object sender, RoutedEventArgs e)
{
YourMethod();
}
private void YourMethod()
{
//your common logic...
}
Related
I know how to programmatically invoke the event handler of a Button:
button1.PerformClick();
I would like to do the same for the Click event handler of a TextBox. The problem is that TextBox does not have a
textBox1.PerformClick();
I suggest method extraction (why should we mix UI - windows messages and Business Logic):
//TODO: put a better name here
private void onMyTextBoxClick() {
//TODO: relevant code here
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick();
}
Then you can just call onMyTextBoxClick:
...
// Same business logic as if MyTextBox is clicked
onMyTextBoxClick();
...
Edit: If you really want EventArgs aruments, just provide them:
//TODO: put a better name here
private void onMyTextBoxClick(TextBox box, EventArgs e) {
//TODO: relevant code here
}
// Default EventArgs
private void onMyTextBoxClick(TextBox box) {
onMyTextBoxClick(box, EventArgs.Empty);
}
// Both TextBox and EventArgs are default ones
private void onMyTextBoxClick() {
onMyTextBoxClick(MyTextBox, EventArgs.Empty);
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick(sender as TextBox, e);
}
Usage:
// Default EventArgs
onMyTextBoxClick(myTextBox);
// Custom EventArgs
EventArgs args = ...
onMyTextBoxClick(myTextBox, args);
Given the following event handler code:
private void image1_MouseDown(object sender, MouseButtonEventArgs e)
{
///////////
}
How can I call it from another method:
private void timerTick(object sender, EventArgs e)
{
image1_MouseDown(object, e); // error
}
Event handlers are regular methods like any others.
The reason you can't call it like you're trying to do, is because the MouseDown event takes a MouseButtonEventArgs instance, while the Tick event of timers take the base class EventArgs.
You'll need to create a new instance of MouseButtonEventArgs, but then you'll need to initialize it with a device, and other event data.
A better option would be to refactor the body of the MouseDown event handler to a method taking individual parameters, which you can then call without having to create a MouseButtonEventArgs instance.
private void image1_MouseDown(object sender, MouseButtonEventArgs e)
{
this.ProcessImageMouseDown(e.ChangedButton, e.ButtonState, e.GetPosition(sender as FrameworkElement));
}
private void timerTick(object sender, EventArgs e)
{
this.ProcessImageMouseDown(MouseButton.Left, MouseButtonState.Pressed, new Point(10d, 20d));
}
private void ProcessImageMouseDown(MouseButton button, MouseButtonState state, System.Windows.Point position)
{
// Do actual processing here.
}
I am trying to call a OnCellEditEnding event from another event,
private void BillsTableRecords_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
// do stuff here
}
My issue is I don't know how to pass the DataGridCellEditEndingEventArgs into the method, i.e. the e in the below method obviously gives an error as it is referencing RoutedEventArgs not DataGridCellEditEndingEventArgs.
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
BillsTableRecords_OnCellEditEnding(sender, e);
}
So how do obtain the value from DataGridCellEditEndingEventArgs so that I can pass the value in the method? Please note that the DataGrid cell with be selected at this point so it will contain a value.
I wouldn't recommend this approach. Event handlers are to be called by events; their signature does not really fit for a standalone call. In case you execute business code in your event handler, it is also not good design, because your event handlers are UI code, which should be separated from business code.
The best way to go here is to create a dedicated method that does what you want and call it from both event handlers:
private void DoStuff(/* add the parameters you need*/) {
//do stuff
}
private void BillsTableRecords_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DoStuff();
}
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
DoStuff();
}
try that
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
BillsTableRecords_OnCellEditEnding(sender, new DataGridCellEditEndingEventArg());
}
If you want to keep the arguments from the RoutedEventArgs, add them to the constructor of DataGridCellEditEndingEventArg
Calling the LostFocus event of control from other control
Example. : I want to call LostFocus event of Button1 button from the Gotfocus event of Button2 button.
code is :-
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1.gotFocus // this line giving error.
}
Use following code:
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1.RaiseEvent(new RoutedEventArgs(LostFocusEvent, button1));
}
private void button1_LostFocus(object sender, RoutedEventArgs e)
{
}
If this doesn't solve your problem, post your code and state your problem and purpose clearly so that you can get better solution.
you can just call event handler method of Button1's LostFocus event in button2_GotFocus :
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1_LostFocus(this.button1, null);
}
Try this
private void button2_GotFocus(object sender, RoutedEventArgs e)
{
button1_LostFocus(sender,e)
}
Here is the short sample code:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
I wonder if it would be okay to code in the above way?
You can do that - although the code you provide can't be compiled. It should look like this:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
But for best practice and code readability, you're probably better off doing this, especially as you are not making use of sender and e:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowMessageBox();
}
private void button1_Click(object sender, EventArgs e)
{
ShowMessageBox();
}
private void ShowMessageBox()
{
MessageBox.Show(txtbox1.Text);
}
Yes you can do that; an event handler is just another method.
However it might be worth creating a new method that shows the message box, and having both Click event handlers call that:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void button1_Click(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void ShowTextboxMessage()
{
MessageBox.Show(txtbox1.Text);
}
An event handler is nothing more than a method, so you can call it like any other.