/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { //need to get the layer from the custom-property of the map if (null == m_hookHelper) { return; } //get the mapControl hook object hook = null; if (m_hookHelper.Hook is IToolbarControl2) { hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy; } else { hook = m_hookHelper.Hook; } //get the custom property from which is supposed to be the layer to be saved object customProperty = null; IMapControl3 mapControl = null; if (hook is IMapControl3) { mapControl = (IMapControl3)hook; customProperty = mapControl.CustomProperty; } else { return; } OpenFileDialog ofdlg = new OpenFileDialog(); ofdlg.Multiselect = true; ofdlg.RestoreDirectory = true; ofdlg.Title = "Load Layer Files"; ofdlg.Filter = "Layer File|*.lyr|All Files|*.*"; DialogResult dr = ofdlg.ShowDialog(); if (DialogResult.OK == dr) { string[] files = ofdlg.FileNames; foreach (string file in files) { if (System.IO.File.Exists(file)) { mapControl.AddLayerFromFile(file, 0); } } } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { // TODO: Add SaveLayerFile.OnClick implementation ILayer layer = (ILayer)m_mapControl.CustomProperty; //ask the user to set a name for the new layer file SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Layer File|*.lyr|All Files|*.*"; saveFileDialog.Title = "Create Layer File"; saveFileDialog.RestoreDirectory = true; saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory, layer.Name + ".lyr"); //get the layer name from the user DialogResult dr = saveFileDialog.ShowDialog(); if (saveFileDialog.FileName != "" && dr == DialogResult.OK) { if (System.IO.File.Exists(saveFileDialog.FileName)) { //try to delete the existing file System.IO.File.Delete(saveFileDialog.FileName); } //create a new LayerFile instance ILayerFile layerFile = new LayerFileClass(); //create a new layer file layerFile.New(saveFileDialog.FileName); //attach the layer file with the actual layer layerFile.ReplaceContents(layer); //save the layer file layerFile.Save(); //ask the user whether he'd like to add the layer to the map if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { m_mapControl.AddLayerFromFile(saveFileDialog.FileName, 0); } } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { //need to get the layer from the custom-property of the map if (null == m_hookHelper) { return; } //get the mapControl hook object hook = null; if (m_hookHelper.Hook is IToolbarControl2) { hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy; } else { hook = m_hookHelper.Hook; } //get the custom property from which is supposed to be the layer to be saved object customProperty = null; IMapControl3 mapControl = null; if (hook is IMapControl3) { mapControl = (IMapControl3)hook; customProperty = mapControl.CustomProperty; } else { return; } if (null == customProperty || !(customProperty is ILayer)) { return; } //ask the user to set a name for the new layer file SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Layer File|*.lyr|All Files|*.*"; saveFileDialog.Title = "Create Layer File"; saveFileDialog.RestoreDirectory = true; saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory, ((ILayer)customProperty).Name + ".lyr"); //get the layer name from the user DialogResult dr = saveFileDialog.ShowDialog(); if (saveFileDialog.FileName != "" && dr == DialogResult.OK) { if (System.IO.File.Exists(saveFileDialog.FileName)) { //try to delete the existing file System.IO.File.Delete(saveFileDialog.FileName); } //create a new LayerFile instance ILayerFile layerFile = new LayerFileClass(); //create a new layer file layerFile.New(saveFileDialog.FileName); //attach the layer file with the actual layer layerFile.ReplaceContents((ILayer)customProperty); //save the layer file layerFile.Save(); //ask the user whether he'd like to add the layer to the map if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { mapControl.AddLayerFromFile(saveFileDialog.FileName, 0); } } }