c# - Determine sender when second form opens? -
i have been learning c# , wpf on recent project of mine , have run issue determining sender when form opens. have main form listbox bound list of objects. used following code handle user double clicking line in list box:
<listbox.itemcontainerstyle> <style targettype="{x:type listboxitem}" basedon="{staticresource {x:type listboxitem}}"> <eventsetter event="mousedoubleclick" handler="listboxitem_mousedoubleclick"/> </style> </listbox.itemcontainerstyle>
where have run issue want launch second form (named "addproject") when user double clicks, launch same form , button on main form. need determine (the double click or button click) launched secondary form can change appropriately.
in main form, can use window_loaded method, , read sender name, when try use same in secondary form, doesn't work. works in main form, not secondary:
private void window_loaded(object sender, routedeventargs e) { messagebox.show(sender.tostring()); }
when button clicked, following code run open addproject form:
private void btnaddproject_click(object sender, routedeventargs e) { addproject frm = new addproject(); frm.showdialog(); }
i know determining sender common issue on stack-overflow, couldn't find answer how determine sender has opened second form. figured run in window_load method of second form, doesn't seem work. or link discussion answer question appreciated!
presumably have this:
private void listboxitem_mousedoubleclick(object sender, routedeventargs e) { addproject frm = new addproject(); frm.showdialog(); }
and of course you've explicitly stated have this:
private void btnaddproject_click(object sender, routedeventargs e) { addproject frm = new addproject(); frm.showdialog(); }
why can't change addproject
class constructor, , pass appropriate flag? this:
class addproject { private bool _frombutton; public addproject(bool frombutton) { _frombutton = frombutton; initializecomponent(); } }
then methods this:
private void listboxitem_mousedoubleclick(object sender, routedeventargs e) { addproject frm = new addproject(false); frm.showdialog(); } private void btnaddproject_click(object sender, routedeventargs e) { addproject frm = new addproject(true); frm.showdialog(); }
Comments
Post a Comment