Fix GetUnderlyingControl call from ContextMenuStrip

https://projectpokemon.org/home/forums/topic/55096-unhandled-exception-error-pkmdb/?tab=comments#comment-249235
had recently moved the contextmenu to the designer, event handler has
sender as the ContextMenuStrip

use recursion -> while loop so that the input can be any of the 3 cases;
it'll drill down until it gets a picturebox or none.
make generic for giggles, update usages so Array.IndexOf uses
picturebox[] instead of object[]
This commit is contained in:
Kurt
2019-09-24 18:14:15 -07:00
parent 33649d4e02
commit f47382e34f
8 changed files with 36 additions and 19 deletions

View File

@@ -131,8 +131,7 @@ private void MenuOpening(object sender, CancelEventArgs e)
private static SlotViewInfo<PictureBox> GetSenderInfo(ref object sender)
{
sender = WinFormsUtil.GetUnderlyingControl(sender);
var pb = (PictureBox)sender;
var pb = WinFormsUtil.GetUnderlyingControl<PictureBox>(sender);
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
var loc = view.GetSlotData(pb);
return new SlotViewInfo<PictureBox> {Slot = loc, View = view};

View File

@@ -70,7 +70,7 @@ public ISlotInfo GetSlotData(PictureBox view)
public ISlotInfo GetSlotData(int slot) => SlotOffsets[slot];
public IList<PictureBox> SlotPictureBoxes => slots;
public int GetSlot(PictureBox sender) => slots.IndexOf(WinFormsUtil.GetUnderlyingControl(sender) as PictureBox);
public int GetSlot(PictureBox sender) => slots.IndexOf(WinFormsUtil.GetUnderlyingControl<PictureBox>(sender));
public int GetSlotOffset(int slot) => SlotOffsets[slot].Offset;
public int ViewIndex { get; set; } = -1;