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);
Related
I use 6 different buttons doing practically the same thing.
private void VisaCreaDoc_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.CREADOC);
}
private void VisaTravaux_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.TRAVAUX);
}
private void VisaRemiseOuvrageIR_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.REMISEOUVRIR);
}
private void VisaRemiseOuvrageExpl_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.REMISEOUVREXPL);
}
private void VisaMES_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.MISEENSERVICE);
}
private void VisaEncodageArchivage_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.ENCODAGEARCHIVAGE);
As you can see, they're using a function from the ViewModel with a different parameter.
Is there any way to regroup the 6 button events to have only one and kind of pass the parameter directly in the XAML call or something similar to avoid having the "code duplication" ?
Not sure if you like it "better" but you can check which button was clicked inside the handler:
void HandleButton_Click(object sender, RoutetEventArgs e)
{
if (sender is Button b)
{
if (b == VisaCreaDoc) # VisaCreaDoc is the given name of your button instace in xaml
ViewModel.ValidateItem(InfosPosteViewModel.CREADOC);
else if (b == VisaTravaux)
ViewModel.ValidateItem(InfosPosteViewModel.TRAVAUX);
else if (...) // etc.
}
}
You can spice it up with a switch pattern matching to get rid of the if / else if / else if / ... chains.
Maybe you can avoid some duplicate code by creating a command for the ViewModel function (see Introduction to WPF Commands). As far as I know you can also define a CommandParameter in XAML.
Hello I have this code:
private void txtNumero_KeyDown(object sender, KeyEventArgs e)
{
CercaCliente();
}
private void txtNote_KeyDown(object sender, KeyEventArgs e)
{
CercaCliente();
}
private void txtNominativo_KeyDown(object sender, KeyEventArgs e)
{
CercaCliente();
}
How can I write this code in better mode? Thanks
there are two approaches you can choose from, and it depends what type of effort you are ready to do...
first when you bind the event there itself call the method rather than creating handler method for each.
Currently, you are doing -
txtNumero.KeyDown += new txtNumero_KeyDown;
..
..
txtNote.KeyDown += new txtNumero_KeyDown;
then in your method you are calling this common method 'CercaCliente()'. you can directly use func delegate to call you common method. e.g.
txtNumero.KeyDown += (o,e)=>CercaCliente();
..
..
txtNote.KeyDown += (o, e)=>CercaCliente();
OR
You can create custom control, derived from textbox, and there you can handle it.
Add this common method
private void HandlerMethod(object sender, EventArgs e)
{
CercaCliente();
}
Then inside your form load Attach this handler method to all events
this.txtNominativo.KeyDown += HandlerMethod;
this.txtNote.KeyDown += HandlerMethod;
this.txtNumero.KeyDown += HandlerMethod;
I want to call the event from within another event.
I want to call this event
private void gv_client_CellContentClick(object sender, DataGridViewCellEventArgs e){}
From this event as
private void update_staff_Click(object sender, EventArgs e){
//some codes
gv_client_CellContentClick(); // i want to call this event here
}
If the event is in same class you can call it like
private void update_staff_Click(object sender, EventArgs e){
//some codes
gv_client_CellContentClick(sender,e); // i want to call this event here
}
As per our comment thread, it seems you want to call the method (as opposed to raising the event).
In your original handler, you can simply call the method:
private void update_staff_Click(object sender, EventArgs e)
{
var rowIndex = ???;
var columnIndex = ???;
var args = new DataGridViewCellEventArgs(columnIndex, rowIndex);
gv_client_CellContentClick(sender, args); // Note: You might need to change sender too if you know this function uses it...
}
The bit you'll need to figure out is the row/column indexes. Presumably this can be retrieved based on the location of the "update_staff" button/control being clicked - tip: cast "sender" to whatever control type you know it is to work out which button/control was clicked.
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 a WinForms solution, you have multiple controls of the same type. You need to add an event handler to each of the control and at the current time the event handler will be doing the same thing. You do not expect there to be difference between them down the road any reason.
eg:
ScheduledPatientsGrid.ProcessGridKey += ScheduledPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;
...
private void ScheduledPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
...
}
private void RecentPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
...
}
private void PatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
...
}
Now is it better to sharing an single Event Handler between the different events as shown below or use different ones like in the code sample shown above?
ScheduledPatientsGrid.ProcessGridKey += ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += ProcessGridKey;
private void ProcessGridKey(object sender, KeyEventArgs e)
{
...
}
In the following page, Microsoft seems to suggest that sharing is better, however I notice that they have not updated it since .NET 2.0 (ie: Visual Studio 2008)
http://msdn.microsoft.com/en-us/library/4ac48519%28v=vs.90%29.aspx
Is there a Guide that makes a best practices recommendation in this case?
I would absolutely use the same method. What possible benefit is there to having multiple methods which do exactly the same, none of which is named to say what it does?
Personally I abhor the source_EventName convention that Visual Studio spawns. I prefer to give my event handler methods meaningful names which say what they do. Then when you look down the event handler list in the designer, you can see that when a button is clicked, X will happen rather than "the button's click event handler will be called" which is useless.
Alternatively, use lambda expressions to subscribe to the events and call meaningful methods with meaningful parameters. (The sender and args are often useless for event handlers.)
In this case, I usually have them wrap a common method, but I keep their event handlers named per usage. This allows me to easily unit test the method and (usually) reduce the needed parameters, and any errors in the stack trace will be very readable as to which grid the process failed for:
ScheduledPatientsGrid.ProcessGridKey += ScheduledPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;
RecentPatientsGrid.ProcessGridKey += RecentPatientsGrid_ProcessGridKey;
...
private void ScheduledPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
ProcessGridKey(e.Key);
}
private void RecentPatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
ProcessGridKey(e.Key);
}
private void PatientsGrid_ProcessGridKey(object sender, KeyEventArgs e)
{
ProcessGridKey(e.Key);
}
private void ProcessGridKey(Key e)
{
...
}
Your mileage may vary depending on what the shared method does, or the parameters passed in. (For example, in my above sample, I duplicate the pulling of the Key from the KeyEventArgs.
I prefer sharing, if the logic gets out of hand you can always just use the single event as a router to the correct method like...
private void ProcessGridKey(object sender, KeyEventArgs e)
{
if (sender is x)
xmethod();
if (sender is y)
ymethod(); //etc
}
I'm aware this syntax doesn't quite make sense as the sender will always be the same object in OP example, but you get the idea.