How to know which button was clicked in winforms c#? -
okay little bit tricky explain.
i'm working on basic time table form.
i have 7 buttons, named btnmontime
, btntuetime
, on till btnsuntime
based on days of week. on each button click, pop window (winform) opens lets user select time through datetimepicker
control. time parsed string , stored. there accept button on popup when pressed, popup closes , label beside particular day stating time posted.
now know how 1 particular day, thing have 1 single function doing label creating. how know time button clicked place @ right place?
this code come with:
private void btnaccept_click(object sender, eventargs e) { formpopup.time = timepicker.value.toshorttimestring(); //label1.text = formpopup.time; label newlabel = new label(); newlabel.text = formpopup.time; newlabel.location = new system.drawing.point(205 + (100 * formtimetable.cmontime), 78); formtimetable.cmontime++; newlabel.size = new system.drawing.size(100, 25); newlabel.forecolor = system.drawing.color.white; thisparent.controls.add(newlabel); this.close(); }
this accept button click handler places label @ right place. whereas variable cmontime
keeps track of how many times particular time button pressed.
public static int cmontime = 0; private void btnmontime_click(object sender, eventargs e) { formpopup f2 = new formpopup(); f2.thisparent = this; f2.show(); }
and happening inside monday's time button click handler.
but how can know day's time button clicked proper placement of timestamp label?
like if tuesday's time button clicked, timestamp should displayed beside time button tuesday.
i tried clear possible.
thanks in advance.
you can button clicked casting sender parameter button control. use button's location parameter formpopup constructor
private void btnmontime_click(object sender, eventargs e) { var button = (button)sender; formpopup f2 = new formpopup(button.location); f2.thisparent = this; f2.show(); }
formpopup
point _buttonlocation; public frmpopup(point buttonlocation) { _buttonlocation = buttonlocation; }
then use button's location set label's location
private void btnaccept_click(object sender, eventargs e) { formpopup.time = timepicker.value.toshorttimestring(); label newlabel = new label(); newlabel.text = formpopup.time; newlabel.location = new point(_buttonlocation.x + 100, _buttonlocation.y); formtimetable.cmontime++; newlabel.size = new system.drawing.size(100, 25); newlabel.forecolor = system.drawing.color.white; thisparent.controls.add(newlabel); this.close(); }
Comments
Post a Comment