How to Turn On or Off a layer in map?

Hello, a very simple questions. Would someone share some code to turn on/off a layer in map by checking the checkedListbox item? I am using checkedListBox in VS 2015. Any other suggestions for a TOC of layers management?

Also, except for the Examples, where else can we find some code samples?

Thanks!

Lang

From a scripting language like VB6, you will see the map property as

AxMap1.LayerVisible(handle, bVisible)

While from a language like c++ or c#, it would be

AxMap1.put_LayerVisible(handle, bVisible)

or

AxMap1.set_LayerVisible(handle, bVisible)

Of course, you need to save the handle for each layer as you add them. Then, when catching the ‘ItemCheck’ property of the checkedListbox, use the value of the checked Item (I think it’s .Selected(Item)) to set the visible property, something like

AxMap1.set_LayerVisible(handle, lstLayers.Selected(Item))

I hope that helps.

1 Like

Dear Jerry,
Thank you so much for saving my day! It works very well. This property is not in axMap1 intellisense. the dot method doesn’t return it, only a set_LayerDynamicVisibility property.
Another question, You said: “you need to save the handle for each layer as you add them”. I think this is very important. How do you do it? I plan to use a dictionary to track layers added and removed. The layerHandle will not change after a layer is being added, right? So even I move a layer up and down, the handle stays the same? It’s only determined by the order of being added? If I added three layers, 0, 1, 2, then 1 is removed. Now I add another one, what will be the handle of the new one? 1 or 3?
Thank you again!
lang

Hello @lang

It’s strange that it is not in your intellisense. You do have access to the online documentation, correct?

So when you add a layer, the return value of the method is the layer handle. So you could perhaps collect the handles in a Dictionary, keyed by name or something.

The handles will never change within the current session. It is independent of position (if you move the layer up or down). And if you remove a layer, then re-add, it will try to use the empty slot. But you shouldn’t assume that or even have to think about that. Just save the handles as you add layers, knowing they will remain the same.

Regards.

@jerryfaust
Hi Jerry, appreciate the quick response a lot! I do have access the online documentation and I can see intelisense, but not all methods/properties are showing up.

Thanks a lot for the clear explanation of the handles. You are right, I just need to add the layer name as key and handle as value into a dictionary and retrieve the handle when needed, don’t need to worry about removing or not. When a layers is added the second time, just make it a name “Name_1” or something.
May I have your email? You may send one to mine: denglang36@yahoo.com
Regards,
Lang

This topic was automatically closed after 3 days. New replies are no longer allowed.