ArcObjects Library Reference  

ColorRamps

About the Retrieve a color ramp from the SymbologyControl Sample

[C#]

ColorRamps.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS;
namespace ColorRamps
{

	public class Form1 : System.Windows.Forms.Form
	{
		private ESRI.ArcGIS.Controls.AxToolbarControl axToolbarControl1;
		private ESRI.ArcGIS.Controls.AxPageLayoutControl axPageLayoutControl1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.Button button1;
		private ESRI.ArcGIS.Controls.AxTOCControl axTOCControl1;
		private ESRI.ArcGIS.Controls.AxLicenseControl axLicenseControl1;

		private System.ComponentModel.Container components = null;

		public Form1()
		{

			InitializeComponent();

		}


		protected override void Dispose( bool disposing )
		{
			//Release COM objects
			ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();

			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.axToolbarControl1 = new ESRI.ArcGIS.Controls.AxToolbarControl();
			this.axPageLayoutControl1 = new ESRI.ArcGIS.Controls.AxPageLayoutControl();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.button1 = new System.Windows.Forms.Button();
			this.axTOCControl1 = new ESRI.ArcGIS.Controls.AxTOCControl();
			this.axLicenseControl1 = new ESRI.ArcGIS.Controls.AxLicenseControl();
			((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).BeginInit();
			this.SuspendLayout();
			// 
			// axToolbarControl1
			// 
			this.axToolbarControl1.Location = new System.Drawing.Point(8, 8);
			this.axToolbarControl1.Name = "axToolbarControl1";
			this.axToolbarControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axToolbarControl1.OcxState")));
			this.axToolbarControl1.Size = new System.Drawing.Size(392, 28);
			this.axToolbarControl1.TabIndex = 0;
			// 
			// axPageLayoutControl1
			// 
			this.axPageLayoutControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.axPageLayoutControl1.Location = new System.Drawing.Point(192, 40);
			this.axPageLayoutControl1.Name = "axPageLayoutControl1";
			this.axPageLayoutControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPageLayoutControl1.OcxState")));
			this.axPageLayoutControl1.Size = new System.Drawing.Size(464, 384);
			this.axPageLayoutControl1.TabIndex = 1;
			this.axPageLayoutControl1.OnPageLayoutReplaced += new ESRI.ArcGIS.Controls.IPageLayoutControlEvents_Ax_OnPageLayoutReplacedEventHandler(this.axPageLayoutControl1_OnPageLayoutReplaced);
			// 
			// comboBox1
			// 
			this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.comboBox1.Location = new System.Drawing.Point(408, 8);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(121, 21);
			this.comboBox1.TabIndex = 3;
			this.comboBox1.Text = "comboBox1";
			// 
			// button1
			// 
			this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.button1.Location = new System.Drawing.Point(536, 8);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(120, 23);
			this.button1.TabIndex = 4;
			this.button1.Text = "Change Color Ramp";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// axTOCControl1
			// 
			this.axTOCControl1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this.axTOCControl1.Location = new System.Drawing.Point(8, 40);
			this.axTOCControl1.Name = "axTOCControl1";
			this.axTOCControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTOCControl1.OcxState")));
			this.axTOCControl1.Size = new System.Drawing.Size(176, 384);
			this.axTOCControl1.TabIndex = 5;
			// 
			// axLicenseControl1
			// 
			this.axLicenseControl1.Enabled = true;
			this.axLicenseControl1.Location = new System.Drawing.Point(176, 216);
			this.axLicenseControl1.Name = "axLicenseControl1";
			this.axLicenseControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axLicenseControl1.OcxState")));
			this.axLicenseControl1.Size = new System.Drawing.Size(32, 32);
			this.axLicenseControl1.TabIndex = 6;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(664, 430);
			this.Controls.Add(this.axLicenseControl1);
			this.Controls.Add(this.axTOCControl1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.axPageLayoutControl1);
			this.Controls.Add(this.axToolbarControl1);
			this.Name = "Form1";
			this.Text = "Color Ramps";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

        [STAThread]
        static void Main()
        {
            if (!RuntimeManager.Bind(ProductCode.Engine))
            {
                if (!RuntimeManager.Bind(ProductCode.Desktop))
                {
                    MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.");
                    return;
                }
            }
            Application.Run(new Form1());
        }

		private void Form1_Load(object sender, System.EventArgs e)
		{
			//Set buddy control
			axToolbarControl1.SetBuddyControl(axPageLayoutControl1);
			axTOCControl1.SetBuddyControl(axPageLayoutControl1);

			//Add ToolbarControl items
			axToolbarControl1.AddItem("esriControls.ControlsOpenDocCommand");
			axToolbarControl1.AddItem("esriControls.ControlsSaveAsDocCommand");
			axToolbarControl1.AddItem("esriControls.ControlsPageZoomInTool");
			axToolbarControl1.AddItem("esriControls.ControlsPageZoomOutTool");
			axToolbarControl1.AddItem("esriControls.ControlsPageZoomWholePageCommand");
			axToolbarControl1.AddItem("esriControls.ControlsMapZoomInTool");
			axToolbarControl1.AddItem("esriControls.ControlsMapZoomOutTool");
			axToolbarControl1.AddItem("esriControls.ControlsMapPanTool");
			axToolbarControl1.AddItem("esriControls.ControlsMapFullExtentCommand");
			axToolbarControl1.AddItem("esriControls.ControlsMapIdentifyTool");

			//Disable controls
			button1.Enabled = false;
			comboBox1.Enabled = false;
		}

		private void axPageLayoutControl1_OnPageLayoutReplaced(object sender, ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnPageLayoutReplacedEvent e)
		{
      
			//Clear the combo box
			comboBox1.Items.Clear();

			//Get IGeoFeatureLayers CLSID
			UID uid = new UIDClass();
			uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
			//Get IGeoFeatureLayers from the focus map 
			IEnumLayer layers = axPageLayoutControl1.ActiveView.FocusMap.get_Layers(uid, true);
			if (layers == null) return; 

			//Reset enumeration and loop through layers
			layers.Reset();
			IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer) layers.Next();
			while (geoFeatureLayer != null)
			{
				//If layer contains polygon features add to combo box
				if (geoFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
				{
                    if ((geoFeatureLayer is IGroupLayer) == false) comboBox1.Items.Add(geoFeatureLayer.Name);
				}
				geoFeatureLayer = (IGeoFeatureLayer) layers.Next();
			}
			comboBox1.SelectedIndex = 0;

			//Enable controls
			button1.Enabled = true;
			comboBox1.Enabled = true;
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//Get the layer selected in the combo box
			IGeoFeatureLayer geofeaturelayer = null;
			IMap map = axPageLayoutControl1.ActiveView.FocusMap;
			for (int i=0; i<= map.LayerCount-1; i++)
			{
				if (map.get_Layer(i).Name == comboBox1.SelectedItem.ToString())
				{
					geofeaturelayer = (IGeoFeatureLayer) map.get_Layer(i);
					break;
				}
			}
			if (geofeaturelayer == null) return;

			//Create ClassBreaks form 
			Form2 classBreaksForm = new  Form2();

			//Get a ClassBreakRenderer that uses the selected ColorRamp
			IClassBreaksRenderer classBreaksRenderer = classBreaksForm.GetClassBreaksRenderer(geofeaturelayer);
			if (classBreaksRenderer == null) return;

			//Set the new renderer 
			geofeaturelayer.Renderer = (IFeatureRenderer) classBreaksRenderer;

			//Trigger contents changed event for TOCControl
			axPageLayoutControl1.ActiveView.ContentsChanged();
			//Refresh the display 
			axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, geofeaturelayer, null);

			//Dispose of the form
			classBreaksForm.Dispose();
		}

	}
}

[Visual Basic .NET]

ColorRamps.vb

Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()
        'Load runtime 
        If Not RuntimeManager.Bind(ProductCode.Engine) Then
            If Not RuntimeManager.Bind(ProductCode.Desktop) Then
                MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.")
                System.Environment.Exit(1) ' Force exit or other indication in the application
            End If
        End If
        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        'Release COM objects 
        ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown()

        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)

    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents AxPageLayoutControl1 As ESRI.ArcGIS.Controls.AxPageLayoutControl
    Friend WithEvents AxToolbarControl1 As ESRI.ArcGIS.Controls.AxToolbarControl
    Friend WithEvents AxTOCControl1 As ESRI.ArcGIS.Controls.AxTOCControl
    Friend WithEvents AxLicenseControl1 As ESRI.ArcGIS.Controls.AxLicenseControl
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.AxPageLayoutControl1 = New ESRI.ArcGIS.Controls.AxPageLayoutControl
        Me.AxToolbarControl1 = New ESRI.ArcGIS.Controls.AxToolbarControl
        Me.AxTOCControl1 = New ESRI.ArcGIS.Controls.AxTOCControl
        Me.AxLicenseControl1 = New ESRI.ArcGIS.Controls.AxLicenseControl
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.Button1 = New System.Windows.Forms.Button
        CType(Me.AxPageLayoutControl1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxToolbarControl1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxTOCControl1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxLicenseControl1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'AxPageLayoutControl1
        '
        Me.AxPageLayoutControl1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.AxPageLayoutControl1.Location = New System.Drawing.Point(200, 40)
        Me.AxPageLayoutControl1.Name = "AxPageLayoutControl1"
        Me.AxPageLayoutControl1.OcxState = CType(resources.GetObject("AxPageLayoutControl1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxPageLayoutControl1.Size = New System.Drawing.Size(472, 376)
        Me.AxPageLayoutControl1.TabIndex = 0
        '
        'AxToolbarControl1
        '
        Me.AxToolbarControl1.Location = New System.Drawing.Point(8, 8)
        Me.AxToolbarControl1.Name = "AxToolbarControl1"
        Me.AxToolbarControl1.OcxState = CType(resources.GetObject("AxToolbarControl1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxToolbarControl1.Size = New System.Drawing.Size(408, 28)
        Me.AxToolbarControl1.TabIndex = 1
        '
        'AxTOCControl1
        '
        Me.AxTOCControl1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.AxTOCControl1.Location = New System.Drawing.Point(8, 40)
        Me.AxTOCControl1.Name = "AxTOCControl1"
        Me.AxTOCControl1.OcxState = CType(resources.GetObject("AxTOCControl1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxTOCControl1.Size = New System.Drawing.Size(184, 376)
        Me.AxTOCControl1.TabIndex = 2
        '
        'AxLicenseControl1
        '
        Me.AxLicenseControl1.Enabled = True
        Me.AxLicenseControl1.Location = New System.Drawing.Point(184, 136)
        Me.AxLicenseControl1.Name = "AxLicenseControl1"
        Me.AxLicenseControl1.OcxState = CType(resources.GetObject("AxLicenseControl1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxLicenseControl1.Size = New System.Drawing.Size(32, 32)
        Me.AxLicenseControl1.TabIndex = 3
        '
        'ComboBox1
        '
        Me.ComboBox1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ComboBox1.Location = New System.Drawing.Point(424, 8)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
        Me.ComboBox1.TabIndex = 4
        Me.ComboBox1.Text = "ComboBox1"
        '
        'Button1
        '
        Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Button1.Location = New System.Drawing.Point(552, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(120, 23)
        Me.Button1.TabIndex = 5
        Me.Button1.Text = "Change Color Ramp"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(680, 422)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ComboBox1)
        Me.Controls.Add(Me.AxLicenseControl1)
        Me.Controls.Add(Me.AxTOCControl1)
        Me.Controls.Add(Me.AxToolbarControl1)
        Me.Controls.Add(Me.AxPageLayoutControl1)
        Me.Name = "Form1"
        Me.Text = "Color Ramps"
        CType(Me.AxPageLayoutControl1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxToolbarControl1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxTOCControl1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxLicenseControl1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Set buddy control
        AxToolbarControl1.SetBuddyControl(AxPageLayoutControl1)
        AxTOCControl1.SetBuddyControl(AxPageLayoutControl1)

        'Add ToolbarControl items
        AxToolbarControl1.AddItem("esriControls.ControlsOpenDocCommand")
        AxToolbarControl1.AddItem("esriControls.ControlsSaveAsDocCommand")
        AxToolbarControl1.AddItem("esriControls.ControlsPageZoomInTool")
        AxToolbarControl1.AddItem("esriControls.ControlsPageZoomOutTool")
        AxToolbarControl1.AddItem("esriControls.ControlsPageZoomWholePageCommand")
        AxToolbarControl1.AddItem("esriControls.ControlsMapZoomInTool")
        AxToolbarControl1.AddItem("esriControls.ControlsMapZoomOutTool")
        AxToolbarControl1.AddItem("esriControls.ControlsMapPanTool")
        AxToolbarControl1.AddItem("esriControls.ControlsMapFullExtentCommand")
        AxToolbarControl1.AddItem("esriControls.ControlsMapIdentifyTool")

        'Disable controls
        Button1.Enabled = False
        ComboBox1.Enabled = False

    End Sub

    Private Sub AxPageLayoutControl1_OnPageLayoutReplaced(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnPageLayoutReplacedEvent) Handles AxPageLayoutControl1.OnPageLayoutReplaced

        'Clear the combo box
        ComboBox1.Items.Clear()

        'Get IGeoFeatureLayers CLSID
        Dim uid As UID = New UIDClass
        uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"
        'Get IGeoFeatureLayers from the focus map
        Dim layers As IEnumLayer
        layers = AxPageLayoutControl1.ActiveView.FocusMap.Layers(uid, True)
        If layers Is Nothing Then Exit Sub

        'Reset enumeration and loop through layers
        layers.Reset()
        Dim geoFeatureLayer As IGeoFeatureLayer = layers.Next()
        While Not geoFeatureLayer Is Nothing
            'If layer contains polygon features add to combo box
            If geoFeatureLayer.FeatureClass.ShapeType = esriGeometryType.esriGeometryPolygon Then
                If Not TypeOf geoFeatureLayer Is IGroupLayer Then
                    ComboBox1.Items.Add(geoFeatureLayer.Name)
                End If
            End If
            geoFeatureLayer = layers.Next()
        End While
        ComboBox1.SelectedIndex = 0

        'Enable controls
        Button1.Enabled = True
        ComboBox1.Enabled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Get the layer selected in the combo box
        Dim i As Integer, map As IMap, geofeaturelayer As IGeoFeatureLayer = Nothing
        map = AxPageLayoutControl1.ActiveView.FocusMap
        For i = 0 To map.LayerCount - 1
            If map.Layer(i).Name = ComboBox1.SelectedItem Then
                geofeaturelayer = map.Layer(i)
                Exit For
            End If
        Next i
        If geofeaturelayer Is Nothing Then Exit Sub

        'Create ClassBreaks form 
        Dim classBreaksForm As Form2 = New Form2

        'Get a ClassBreakRenderer that uses the selected ColorRamp
        Dim classBreaksRenderer As IClassBreaksRenderer
        classBreaksRenderer = classBreaksForm.GetClassBreaksRenderer(geofeaturelayer)
        If classBreaksRenderer Is Nothing Then Exit Sub

        'Set the new renderer 
        geofeaturelayer.Renderer = classBreaksRenderer

        'Trigger contents changed event for TOCControl
        AxPageLayoutControl1.ActiveView.ContentsChanged()
        'Refresh the display 
        AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, geofeaturelayer, Nothing)

        'Dispose of the form
        classBreaksForm.Dispose()

    End Sub

End Class