How can I set the Zoom Level on my Bing Map?

1

I've got this shared event handler for 19 menu items, labeled "1" to "19", with corresponding tag values of 1 to 19:

// This event handler is shared by all 19 zoom level menu items
private void toolStripMenuItem_ZoomLevel1_Click(object sender, EventArgs e)
{
    //int zoomLevel = sender.Tag; // "object does not contain a definition for tag"
    //int zoomLevel = Convert.ToInt32(sender.Tag); // " "
    //int zoomLevel = (sender as toolStripMenuItem).Tag; // type or namespace   'toolStripMenuItem' could not be found
    //int zoomLevel = (sender as MenuItem).Tag; // "cannot implicitly convert object to     int"
    int zoomLevel = Convert.ToInt32((sender as MenuItem).Tag);
    this.userControl11.myMap.ZoomLevel = zoomLevel;
}

At runtime, though, when I select one of these menu items, I get a Null Reference: "Object reference not set to an instance of an object"

The menu item (the sender) obviously exists, and it has a tag value. What is null here?

UPDATE

Using the suggested code:

this.userControl11.myMap.ZoomLevel = (int)((ToolStripMenuItem)sender).Tag;

...I get, "Specified cast is not valid" at runtime.

The "rest of the story"/gory details are:

System.InvalidCastException HResult=0x80004002 Message=Specified cast is not valid. Source=MyMaps StackTrace: at MyMaps.Form1.toolStripMenuItem_ZoomLevel1_Click(Object sender, EventArgs e) in C:\Users\bclay\source\repos\MyMaps\MyMaps\Form1.cs:line 138 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at MyMaps.Program.Main() in C:\Users\bclay\source\repos\MyMaps\MyMaps\Program.cs:line 19

c#
.net
wpf
winforms
bing-maps
asked on Stack Overflow Jan 13, 2021 by B. Clay Shannon • edited Jan 14, 2021 by B. Clay Shannon

1 Answer

2

Based on the name of your event handler I assume you are handling click event of ToolStrioMenuItem, so you need to cast the sender as ToolStripMenuItem.

If you are not sure about the type, go to designer and click on the control/item that you are handling its click event and see the properties and check what's the type of the object at top of the property window in the dropdown, and use that type when casting sender:

//To make it a bit more readable:
var item = (ToolStripMenuItem)sender;
var tag = item.Tag;
var value = Convert.ToInt32(tag);
this.userControl11.myMap.ZoomLevel = value;

//Or in a single line:
this.userControl11.myMap.ZoomLevel = Convert.ToInt32(((ToolStripMenuItem)sender).Tag);

Here is the problem with rest of your attempts:

  • int zoomLevel = sender.Tag;

    The sender argument of the event handler, is of type of object and object doesn't have Tag property.

  • int zoomLevel = Convert.ToInt32(sender.Tag);

    Same as above.

  • int zoomLevel = (sender as toolStripMenuItem).Tag;

    Typo! toolStripMenuItem should be ToolStripMenuItem

  • int zoomLevel = (sender as MenuItem).Tag;

    ToolStripMenuItem doesn't drive from/isn't convertible to MenuItem so when you try cast it to MenuItem by using as operator, the result is null, and trying to get Tag of null, will cause an object null reference exception.

answered on Stack Overflow Jan 14, 2021 by Reza Aghaei • edited Jan 14, 2021 by Reza Aghaei

User contributions licensed under CC BY-SA 3.0