I need some help. I am new to Events and handlers and I would like to begin using events for decoupling purposes but I am confused.
I have a class where I have a list view that is populated on OnAppearing. However to prevent onAppearing to happen each time the page is clicked I load the list once and then I would like to have items to get added or deleted to the list upon being added or removed from the server through the use of events.
The ListView page is a list of my favorite newspaper article Links. When clicking on any one of these links I get redirected to a LinksDetailsPage where I pass in the selected link and then display any details associated with the link.
Anyways...
I would like to add or remove an item on the my Favorites list seamlessly. So when I click on the AddItem or RemoveItem in the LinksDetailsPage I would like the item to either remove or add to theFavoritesPage List. Before I was only relying on the OnAppearing to work its magic and update the favorites list but it would lag to remove the item from the list, so this way I hope it would remove or add as soon as the even is invoked and not when the page loads on OnAppearing. However I think I am either not invoking the event properly or my subscriber is not subscribed properly. Events and delegates have been confusing for me from the get go. On top of that my favorites list is grouped so and it's my first time working with grouped lists as well.
Check out my code:
Subscriber
My Favorites Page:
public FavoritesPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
if (_isDataLoaded)
return;
_isDataLoaded = true;
base.OnAppearing();
await LoadFavorites();
}
private async Task LoadFavorites()
{
groups = new ObservableCollection<LinksTypeGroup<string, NewspaperLink>>();
var links = await _db.GetAllFavorites();
var linkType = await _manager.GetLinkCategories();
foreach(var type in linkType)
{
var typegroup = links.FindAll(
delegate(NewspaperLink link)
{
return link.iLinkTypeID == type.iLinkTypeID;
});
groups.Add(new LinksTypeGroup<string, NewspaperLink>(type.Title, typegroup));
MyList.GroupDisplayBinding = new Binding("GroupKey");
MyList.ItemsSource = groups;
}
}
public void Listener(FavoritesPage P)
{
P.LinkAdded += Item_Added;
P.LinkDeleted += Item_Deleted;
}
void Item_Deleted(object sender, int e)
{
Console.WriteLine("Item_Deleted");
// remove item from groups ..see code above
}
void Item_Added(object sender, int e)
{
Console.WriteLine("Item_Added");
// add to groups ..see code above
}
I am not accessing anything so far.
Publisher
LinksDetailsPage:
private NewspaperLink _link;
public event EventHandler< NewspaperLink> ItemAdded;
public event EventHandler< NewspaperLink> ItemDeleted;
public LinksDetailsPage(NewspaperLink link)
{
_link = link;
BindingContext = _link;
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
await LoadLink();
}
private async Task LoadLink()
{
var url = await db.ReturnRecipeLink(_link.iLinkID);
linkWebView.Source = url;
CheckifExists(_link);
}
}
void AddLink(object sender, System.EventArgs e)
{
var link = BindingContext as NewspaperLink;
db.InsertIntoMyList(_link);
ItemAdded?.Invoke(this, link);
}
void DeleteLink(object sender, System.EventArgs e)
{
var link = BindingContext as NewspaperLink;
db.DeleteFromMyList(_link);
ItemDeleted?.Invoke(this, link);
}
Can someone guide me on how to make this even process work?
Event
If want to use Events, LinksDetailsPage should be declared something like following:
public partial class LinksDetailsPage : ContentPage
{
public event EventHandler<NewspaperLink> ItemAdded;
public event EventHandler<NewspaperLink> ItemDeleted;
public LinksDetailsPage()
{
InitializeComponent();
}
protected virtual void AddLink(NewspaperLink e)
{
EventHandler<NewspaperLink> handler = ItemAdded;
if (handler != null)
{
handler(this, e);
}
}
protected virtual void DeleteLink( NewspaperLink e)
{
EventHandler<NewspaperLink> handler = ItemDeleted;
if (handler != null)
{
handler(this, e);
}
}
// Add click event
private void Add_Clicked(object sender, EventArgs e)
{
AddLink(new NewspaperLink() {link="first link" });
}
// Delete click event
private void Delete_Clicked(object sender, EventArgs e)
{
DeleteLink(new NewspaperLink() { link = "first link" });
}
}
public class NewspaperLink : EventArgs
{
public string link { get; set; }
}
Then you need to subscribe it in the ListView page when navigating to the LinksDetailsPage page:
private async void Button_Clicked(object sender, EventArgs e)
{
LinksDetailsPage detailPage = new LinksDetailsPage();
detailPage.ItemAdded += DetailGridPage_ItemAdded;
detailPage.ItemDeleted += DetailGridPage_ItemDeleted;
await Navigation.PushModalAsync(detailGridPage);
}
private void DetailGridPage_ItemDeleted(object sender, NewspaperLink e)
{
Console.WriteLine("The tlink was deleted : " + e.link);
}
private void DetailGridPage_ItemAdded(object sender, NewspaperLink e)
{
Console.WriteLine("The link was added : "+e.link);
}
Delegate
Similarly, if want to use Delegate, you only need to declare something in List Page as follows:
public partial class ListViewPage : ContentPage
{
public delegate void ItemAddedDelegate(NewspaperLink e);
public delegate void ItemDeletedDelegate(NewspaperLink e);
public ListViewPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
ItemAddedDelegate itemAddedDelegate = AddMethod;
ItemDeletedDelegate itemDeletedDelegate = DeleteMethod;
DetailGridPage detailGridPage = new DetailGridPage(itemAddedDelegate, itemDeletedDelegate);
await Navigation.PushModalAsync(detailGridPage);
}
public static void AddMethod(NewspaperLink item)
{
Console.WriteLine("Add" + item.link);
}
public static void DeleteMethod(NewspaperLink link)
{
Console.WriteLine("Delete" + item.link);
}
}
Then in LinksDetailsPage, you can pass the add or delete delegate method to ListViewPage.
public partial class LinksDetailsPage : ContentPage
{
private ListViewPage.ItemAddedDelegate itemAddedDelegate;
private ListViewPage.ItemDeletedDelegate itemDeletedDelegate;
public DetailGridPage()
{
InitializeComponent();
}
public LinksDetailsPage(ListViewPage.ItemAddedDelegate itemAddedDelegate, ListViewPage.ItemDeletedDelegate itemDeletedDelegate)
{
InitializeComponent();
this.itemAddedDelegate = itemAddedDelegate;
this.itemDeletedDelegate = itemDeletedDelegate;
}
// Add click event
private void Add_Clicked(object sender, EventArgs e)
{
itemAddedDelegate(new NewspaperLink() { link = "first link" });
}
// Delete click event
private void Delete_Clicked(object sender, EventArgs e)
{
itemDeletedDelegate(new NewspaperLink() { link = "first link" });
}
}
public class NewspaperLink : EventArgs
{
public string link { get; set; }
}
MessageCenter
If using MessageCenter, it should be the best convenient method to achieve that.
Only Subscribe it in ListView page:
public ListViewPage()
{
InitializeComponent();
MessagingCenter.Subscribe<object, NewspaperLink>(this, "Add", async (sender, arg) =>
{
await DisplayAlert("Message received", "arg=" + arg.link, "OK");
});
MessagingCenter.Subscribe<object, NewspaperLink>(this, "Delete", async (sender, arg) =>
{
await DisplayAlert("Message received", "arg=" + arg.link, "OK");
});
}
And send message in LinksDetailsPage as follows:
// Add click event
private void Add_Clicked(object sender, EventArgs e)
{
NewspaperLink newspaperLink= new NewspaperLink() { link = "first link" };
MessagingCenter.Send<object, NewspaperLink>(this, "Add", newspaperLink);
}
// Delete click event
private void Delete_Clicked(object sender, EventArgs e)
{
NewspaperLink newspaperLink = new NewspaperLink() { link = "first link" };
MessagingCenter.Send<object, NewspaperLink>(this, "Delete", newspaperLink);
}
Related
I have 18 buttons on the child form "Control Test" which send event to the parent form
Out of 18 buttons, 14 are ON and OFF functionality, making 7 pairs as in the picture
The problem is raising the event for each button, it causes very long and messy code, both in the child and the parent form,
Is there any less complex way to do it? like I have done with the menu.
Child Form:
Child Form:
// B Plus Relay On Button
public event EventHandler BPRElayOnBtnClicked;
protected virtual void WhenBPRelayOnBtnClicked(EventArgs e)
{
BPRElayOnBtnClicked.Invoke(this, e);
}
private void BPRelayOn_btn_Click(object sender, EventArgs e)
{
WhenBPRelayOnBtnClicked(e);
}
// B Plus Relay OFF Button
public event EventHandler BPRElayOffBtnClicked;
protected virtual void WhenBPRelayOffBtnClicked(EventArgs e)
{
BPRElayOnBtnClicked.Invoke(this, e);
}
private void BPRelayOff_btn_Click(object sender, EventArgs e)
{
WhenBPRelayOffBtnClicked(e);
}
// B Minus Relay ON Button
public event EventHandler BMRElayOnBtnClicked;
protected virtual void WhenBMRelayOnBtnClicked(EventArgs e)
{
BMRElayOnBtnClicked.Invoke(this, e);
}
private void BMRelayOn_btn_Click(object sender, EventArgs e)
{
WhenBMRelayOnBtnClicked(e);
}
// B Minus Relay OFF Button
public event EventHandler BMRElayOffBtnClicked;
protected virtual void WhenBMRelayOffBtnClicked(EventArgs e)
{
BMRElayOffBtnClicked.Invoke(this, e);
}
private void BMRelayOff_btn_Click(object sender, EventArgs e)
{
WhenBMRelayOffBtnClicked(e);
}
...... //event for each button
Parent Form:
private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
switch (menu.Name)
{
case "controlTestToolStripMenuItem":
if (Application.OpenForms["CtrlTest"] is CtrlTest ctrlTest)
{
ctrlTest.Focus();
return;
}
ctrlTest = new CtrlTest();
ctrlTest.BPRElayOnBtnClicked += CtrlTest_BPRElayOnBtnClicked;
ctrlTest.BPRElayOffBtnClicked += CtrlTest_BPRElayOffBtnClicked;
ctrlTest.BMRElayOnBtnClicked += CtrlTest_BMRElayOnBtnClicked;
ctrlTest.BMRElayOffBtnClicked += CtrlTest_BMRElayOffBtnClicked;
ctrlTest.PreRElayOnBtnClicked += CtrlTest_PreRElayOnBtnClicked;
ctrlTest.PreRElayOffBtnClicked += CtrlTest_PreRElayOffBtnClicked;
ctrlTest.MdiParent = this;
ctrlTest.Show();
break;
.........//Other menus ...
default:
break;
private void CtrlTest_BPRElayOnBtnClicked(object sender, EventArgs e)
{
//Do something here
}
private void CtrlTest_BPRElayOffBtnClicked(object sender, EventArgs e)
{
//Do something here
}
private void CtrlTest_BMRElayOnBtnClicked(object sender, EventArgs e)
{
//Do something here
}
private void CtrlTest_BMRElayOffBtnClicked(object sender, EventArgs e)
{
//Do something here
}
For the on/off, take a look at the following control. For the others setup one event and work out logic similar to the on/off buttons.
Download the source, add the class project to your Visual Studio solution. Open the class project and change the .NET Framework currently set to 4, to your project's .NET Framework version.
Setup an enum where for each on/off button set it's tag to one of the members. this ways things are clearer using a switch and enums.
public enum OperationType
{
BPlusRelay,
BMinusRelay,
PreRelay,
CycleCount,
PairDown,
TestMode,
StandbyMode
}
In the child form, setup and event and set tags for each on/off control. Change the names to reflect their purpose, I simply added them quickly for demoing purposes.
public partial class ChildForm : Form
{
public delegate void OnClicked(OperationType operationType, bool state);
public event OnClicked ClickedEvent;
public ChildForm()
{
InitializeComponent();
SetProperties();
}
public void SetProperties()
{
ToggleSwitch1.Tag = OperationType.BPlusRelay;
ToggleSwitch2.Tag = OperationType.BMinusRelay;
ToggleSwitch3.Tag = OperationType.PreRelay;
ToggleSwitch4.Tag = OperationType.CycleCount;
ToggleSwitch5.Tag = OperationType.PairDown;
ToggleSwitch6.Tag = OperationType.TestMode;
ToggleSwitch7.Tag = OperationType.StandbyMode;
var list = Controls.OfType<JCS.ToggleSwitch>().ToList();
foreach (var toggleSwitch in list)
{
toggleSwitch.CheckedChanged += ToggleSwitchOnCheckedChanged;
}
}
private void ToggleSwitchOnCheckedChanged(object sender, EventArgs e)
{
var current = (JCS.ToggleSwitch)sender;
ClickedEvent?.Invoke((OperationType)current.Tag, current.Checked);
}
}
In the main form, show the child form, subscribe to the event above.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ShowChildFormButton_Click(object sender, EventArgs e)
{
var childForm = new ChildForm();
childForm.ClickedEvent += ClickedEvent;
try
{
childForm.ShowDialog();
}
finally
{
childForm.Dispose();
}
}
private void ClickedEvent(OperationType operationType, bool state)
{
switch (operationType)
{
case OperationType.BPlusRelay:
// TODO
break;
case OperationType.BMinusRelay:
// TODO
break;
case OperationType.PreRelay:
// TODO
break;
case OperationType.CycleCount:
// TODO
break;
case OperationType.PairDown:
// TODO
break;
case OperationType.TestMode:
// TODO
break;
case OperationType.StandbyMode:
// TODO
break;
}
}
}
Partly done form, and note you can change the size of the buttons.
I have a class that derives from ContextMenuStrip. This class has standard buttons and is used throughout the project:
public class ItemMenu : ContextMenuStrip
{
public ItemMenu (IContainer container)
: base(container)
{
MenuItemAdd = new ToolStripMenuItem("Add", null, AddNew);
this.Items.AddRange(new ToolStripItem[]
{
mnuAdd,
});
}
public void AddNew(object sender, EventArgs e)
{
//Code to add new item here...
}
}
And in the form:
cmsMenu = new ItemMenu(this.components);
Now I have a particular situation where I want that the form itself handles the code for adding an item.
How can I change the above class so that in default cases the class itself handles the actions, but in particular situations, other methods (events/delegates?) are used?
The following changes were required to get it to work:
public class ItemMenu : ContextMenuStrip
{
public event EventHandler AddNewItem;
public ItemMenu (IContainer container)
: base(container)
{
MenuItemAdd = new ToolStripMenuItem("Add", null, AddNew);
this.Items.AddRange(new ToolStripItem[]
{
mnuAdd,
});
}
public void AddNew(object sender, EventArgs e)
{
EventHandler handler = AddNewItem;
if (handler != null)
{
handler(sender, e);
}
else
{
OnAddNew(sender, e);
}
}
protected void OnAddNew(object sender, EventArgs e)
{
//Code to add new item here...
}
}
Here is my code in my userControl
public partial class UserControlHomeScreen : UserControl
{
public event EventHandler SomethingHappened;
public void DoSomething()
{
EventHandler handler = SomethingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
public void HandleEvent(object sender, EventArgs args)
{
MessageBox.Show("Wafak.");
}
public UserControlHomeScreen()
{
InitializeComponent();
}
private void btnAverageDailyBal_Click(object sender, EventArgs e)
{
this.Tag = 0;
this.Hide();
}
private void btnComputeTransferPricing_Click(object sender, EventArgs e)
{
this.Tag = 1;
this.Hide();
}
}
And here is my code in my main form
private void HomeScreen()
{
uHomeScreen = new UserControlHomeScreen();
uHomeScreen.Dock = DockStyle.Fill;
//uHomeScreen.Disposed += new EventHandler(uHomeScreen_Disposed);
uHomeScreen.SomethingHappened += new EventHandler(uHomeScreen_SomethingHappened);
panelMain.Controls.Add(uHomeScreen);
}
void uHomeScreen_SomethingHappened(object sender, EventArgs e)
{
MessageBox.Show("throw new NotImplementedException();");
}
What i want to happen is that when the usercontrol is hidden i want to fire an event in my main form but does not work, what am i missing? please help. thanks!
Your naming convention for event raiser (DoSomething) is confusing, your code doesn't call DoSomething (or raise the event SomethingHappened), so how could it fire for you? Add the following code in your user control class:
//override the OnVisibleChanged
protected override void OnVisibleChanged(EventArgs e){
if(!Visible) DoSomething();
}
I've followed this question and tried to build my solution. The problem is that 'UserControlButtonClicked' appears to be null! So 'UserControlButtonClicked(this, EventArgs.Empty)' inside the if, doesn't run, and the method 'addStepContent' in the parent page is never called.
UserControl 'StepsBar'
public sealed partial class StepsBar : UserControl
{
public event EventHandler UserControlAddStepContent;
[...]
public StepsBar()
{
this.InitializeComponent();
Image step_1 = new Image();
ButtonInfo step_1Info = new ButtonInfo();
step_1Info.Add((int)stepNumber.one, (int)stepStatus.normal);
step_1.Tag = step_1Info;
step_1.Source = setBackground((int)stepStatus.normal);
step_1.Tapped += stepTapped;
[...]
}
public void stepTapped(Object sender, RoutedEventArgs e)
{
[...]
if (step != null)
{
[...]
firePageEvent();
}
}
public void firePageEvent()
{
if (UserControlAddStepContent != null)
{
UserControlAddStepContent(this, EventArgs.Empty);
}
}
Parent Page
public Violation()
{
this.InitializeComponent();
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
}
private void addStepContent(object sender, EventArgs e)
{
CheckBox check_1 = new CheckBox();
check_1.Content = "Check me!";
bodyStackPanel.Children.Add(check_1);
}
This assumes that you want to use an existing delegate rather than make your own and you aren't passing anything specific to the parent page by event args.
In the user control's code-behind (adapt as necessary if not using code-behind or C#):
public partial class MyUserControl : System.Web.UI.UserControl
{
public event EventHandler UserControlButtonClicked;
private void OnUserControlButtonClick()
{
if (UserControlButtonClicked != null)
{
UserControlButtonClicked(this, EventArgs.Empty);
}
}
protected void TheButton_Click(object sender, EventArgs e)
{
// .... do stuff then fire off the event
OnUserControlButtonClick();
}
// .... other code for the user control beyond this point
}
In the page itself you subscribe to the event with something like this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// hook up event handler for exposed user control event
MyUserControl.UserControlButtonClicked += new
EventHandler(MyUserControl_UserControlButtonClicked);
}
private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
{
// ... do something when event is fired
}
}
Solved. The problem was this, on the parent page.
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
The istance of StepsBar was not added to the page. D'OH!
So here's what I've done:
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
and on the xaml of the parent page:
<local:StepsBar x:Name="stepsBar"/>
i created user control which looks like this:
and code behind looks like:
using System;
using System.Windows.Forms;
namespace Controlls
{
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
}
private void pbNazaj_Click(object sender, EventArgs e)
{
}
private void pbNovo_Click(object sender, EventArgs e)
{
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
}
}
}
now i move this UC to form but there is a problem. How to get this events from UC. I need to implement click events for buttons in form where i use this user control. What i can see is that i can use it like one component and i can not get separate buttons from it.
Add 2 event handlers for yor control, and subscribe to the toobox event at the place you use it:
using System;
using System.Windows.Forms;
namespace Controlls
{
public enum ToolboxButtonType { Potrdi, Brisi, Novo, Nazaj, Naprej }
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Novo);
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Brisi);
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Potrdi);
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
OnTextChanged(txtOd.Text);
}
}
private OnToolboxClick(ToolboxButtonType button)
{
if (ToolboxClick != null)
{
ToolboxClick(this, new ToolboxClickEventArgs(button));
}
}
private OnTextChanged(string text)
{
if (ToolboxTextChanged!= null)
{
ToolboxTextChanged(this, new ToolboxTextChangedEventArgs(text));
}
}
public class ToolboxTextChangedEventArgs: EventArgs
{
public string Text { get; private set; }
public ToolboxClickEventArgs(string text) { Text = text; }
}
public class ToolboxClickEventArgs : EventArgs
{
public ToolboxButtonType Button { get; private set; }
public ToolboxClickEventArgs(ToolboxButtonType button) { Button = button; }
}
public event EventHandler<ToolboxClickEventArgs> ToolboxClick;
public event EventHandler<ToolboxTextChangedEventArgs> ToolboxTextChanged;
}
For example:
//Toolbar toolbar = new Toolbar();
toolbar.ToolboxTextChanged += (s, e) => { btn1.Text = e.Text; };
toolbar.ToolboxClick += (s, e) =>
{
swith(e.Button)
{
case ToolboxButtonType.Brisi: //
break;
default:
break;
}
};
You need to create event in MainForm class and generarate it in hand mode.
If you also draw your icons on canvas or smth, you need to handle onClick your MainForm, than calculate mouse click position, and generate event that you were create.
If it amount of buttons or smth that has their own onClick events, you can hang on this buttons event the action that will generate your even in main form.
Answer what approach you use, and I past some code.
ADDED:
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
public enum ToolBarCommands
{
Naprej, Nazaj, Novo
}
public Action<object, ToolBarCommands> MenuItemClick;
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
}
How to use it? easy:
// Define section
var toolbar = new Toolbar();
toolbar.MenuItemClick += MenuItemClickHandler;
...
MenuItemClickHandler(object sender, Toolbar.ToolBarCommands item)
{
switch(item)
{
case Toolbar.ToolBarCommands.Naprej:
// Naperej handler
break;
case Toolbar.ToolBarCommands.Nazaj:
// Nazaj handler
break;
case Toolbar.ToolBarCommands.Novo:
// Novo handler
break;
}
}
ADDED:
If names of your buttons same as enum members you can cut this part of code:
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
It will look's like one method (hang this method to all your menu element events)
private void pbItem_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
{
ToolBarCommands command;
var res = ToolBarCommands.TryParse(((Button)sender).Name, command)
if(res == true)
MenuItemClick(this, command);
}
}