I have a MapView UI control ( version 14 ) for Windows Forms project from ThinkGeo. I am unable to change the controls background ( or any other property of the control )
The MapView class is reference to WPF component hosted in WindowsForms using .NET8. The WPF support in project properties is enabled. I did not find a way how to access the control properties.
...
...
using ThinkGeo.Core;
using ThinkGeo.UI.WinForms;
...
...
namespace MapApp
{
public partial class Maps : Form
{
private MapView mapView;
...
...
public Maps()
{
InitializeComponent();
this.SuspendLayout();
this.DoubleBuffered = true;
mapView = new ThinkGeo.UI.WinForms.MapView();
mapView.Location = new System.Drawing.Point(0, 0);
mapView.MapResizeMode =
ThinkGeo.Core.MapResizeMode.PreserveScaleAndCenter;
mapView.MaximumScale = 1000000D;
mapView.MinimumScale = 100D;
mapView.Name = "mapView";
mapView.RestrictExtent = null;
mapView.RotatedAngle = 0F;
mapView.Size = new System.Drawing.Size(100, 100);
mapView.TabIndex = 0;
mapView.MapTools.PanZoomBar.IsEnabled = false;
mapView.MapTools.Logo.IsEnabled = false;
//mapView.MapTools.MouseCoordinate.IsEnabled = true;
//mapView.MapTools.ScaleLine.IsEnabled = true;
mapView.Dock = DockStyle.Fill;
mapView.MapUnit = GeographyUnit.Meter;
}
}
...
...
I was trying during the runtime debug and traverse the MapView object. I know I could get the Windows Window handle and traverse the objects and set some properties that way; I’m trying to avoid this as I hope there is a simpler more straight forward way.
The map has a BackgroundOverlay
which has a BackgroundBrush
property.
Give the following code a try:
mapView.BackgroundOverlay.BackgroundBrush = GeoBrushes.White;
The correct answer is the one from “salesthinkgeocom”.
At the time of writing this some additional consideration were necessary to take in account. The first part of the code is for ilustration of the state when it causes error (setting the MapView in constructor) and the second part is flowlessly working state, setting the MapView control in form_load() or on demand by user, for example button_click().
// ==============================================================
// Example of error causing code - SECTION START
// ==============================================================
...
...
using ThinkGeo.Core;
using ThinkGeo.UI.WinForms;
...
...
namespace MapApp
{
public partial class Maps : Form
{
private MapView mapView;
...
...
public Maps()
{
InitializeComponent();
this.SuspendLayout();
this.DoubleBuffered = true;
// Create mapView
mapView = new ThinkGeo.UI.WinForms.MapView();
// Place control on the form / container
this.tabControlMain.TabPages["tabPageMap"].Controls.Add(mapView);
// Set control properties
mapView.Location = new System.Drawing.Point(0, 0);
mapView.MapResizeMode =
ThinkGeo.Core.MapResizeMode.PreserveScaleAndCenter;
mapView.MaximumScale = 1000000D;
mapView.MinimumScale = 100D;
mapView.Name = "mapView";
mapView.RestrictExtent = null;
mapView.RotatedAngle = 0F;
mapView.Size = new System.Drawing.Size(100, 100);
mapView.TabIndex = 0;
mapView.Dock = DockStyle.Fill;
mapView.MapUnit = GeographyUnit.Meter;
// Setting background ( and some other ) properties of the mapView in container
// -----------------------------------------------------------------------------
// NOTE: setting the mapView control property here ( in constructor ) will fail - cause an error
// ==> System.NullReferenceException: 'Object reference not set to an instance of an object.'
// ==> ThinkGeo.UI.WinForms.MapView.BackgroundOverlay.get returned null.
// The control is designed in a way that it must be visible or active in user interface in order to set the property
// In addition, note the code above - it is placed in a tab control
// mapView.BackgroundOverlay.BackgroundBrush = GeoBrushes.White;
this.ResumeLayout(false);
}
}
...
...
// ==============================================================
// Example of error causing code - SECTION END
// ==============================================================
// ==============================================================
// Example of working code - SECTION START
// ==============================================================
...
...
// Form Load
private void Maps_Load(object sender, EventArgs e)
{
// Init and setting MapView control is best in form_load() ( or on user demand for example a button_click()... )
this.SuspendLayout();
mapView = new ThinkGeo.UI.WinForms.MapView();
// Make the "parent" container active / visible / enabled
this.tabControlMain.TabPages["tabPageMap"].Controls.Add(mapView);
this.tabControlMain.SelectedTab = tabPageMap;
mapView.Location = new System.Drawing.Point(0, 0);
mapView.MapResizeMode = ThinkGeo.Core.MapResizeMode.PreserveScaleAndCenter;
mapView.MaximumScale = 1000000D;
mapView.MinimumScale = 100D;
mapView.Name = "mapView";
mapView.RestrictExtent = null;
mapView.RotatedAngle = 0F;
mapView.Size = new System.Drawing.Size(100, 100);
mapView.TabIndex = 0;
mapView.Dock = DockStyle.Fill;
mapView.MapUnit = GeographyUnit.Meter;
// SUCCESS
mapView.BackgroundOverlay.BackgroundBrush = GeoBrushes.White;
// You can deactivate / make invisible / disable the "parent" container after
// all desired MapView properties are set and reactivate when needed
this.tabControlMain.SelectedTab = tabPageSettings;
this.ResumeLayout(false);
this.PerformLayout();
}
// ==============================================================
// Example of working code - SECTION END
// ==============================================================
In additon I have noticed that simmilar behavior apply to other properties, methods or events. What I mean by that is – if the MapView control is not visible / active / enabled then certain properties, methods or events are not accessible at that time / in that state of the control.