1

I am working on Arc Objects using C# and I have to make a button add in, such that if I click button one time then layout background color changes to red and then if I click again then button add in change the background color from red to blue in Arc Map. I have successfully changed layout background color to red and blue but I want that color keep changing on each click from red to blue and then from blue to green.

Here is my code:

using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Desktop.AddIns;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace lecture5test
{
    public class Button1 : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public Button1()
        {

        }
        protected override void OnClick()
        {
            //
            //  TODO: Sample code showing how to access button host
            //

            ArcMap.Application.CurrentTool = null;

            RgbColor r = new RgbColor();
            IRgbColor iRgb = (IRgbColor)r;
            iRgb.Red = 255;
            iRgb.Green = 0;
            iRgb.Blue = 0;
            IColor ic = (IColor)r;
            // Page iPl = ArcMap.Document.PageLayout;
            IPageLayout iPl = ArcMap.Document.PageLayout;
            IPage pPage = (IPage)iPl.Page;

            RgbColor b = new RgbColor();
            IRgbColor bRgb = (IRgbColor)b;
            bRgb.Red = 0;
            bRgb.Green = 0;
            bRgb.Blue = 255;
            IColor ib = (IColor)b;

            RgbColor g = new RgbColor();
            IRgbColor gRgb = (IRgbColor)g;
            gRgb.Red = 0;
            gRgb.Green = 255;
            gRgb.Blue = 0;
            IColor ig = (IColor)g;

            pPage.BackgroundColor = ic;
            pPage.BackgroundColor = ib;
            pPage.BackgroundColor = ig;


        }

        private void EnableDynamicDisplay()
        {
            throw new NotImplementedException();
        }
        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;

        }

    }

}
2

Red -> Blue -> Green -> Red etc.

Just wrap your pPage.BackgroundColor = in if/else if/else something like

if (pPage.BackgroundColor.RGB == ic.RGB)
{ 
    pPage.BackgroundColor = ib;
}
else if (pPage.BackgroundColor.RGB == ib.RGB)
{
    pPage.BackgroundColor = ig;
}
else
{
    pPage.BackgroundColor = ic;
}
  • No result. Result showing only Red color but background is not changing on each click. it just showing red color. – Muhammad Sarfraz Apr 25 '16 at 4:13
  • No result. Result showing only Red color but background is not changing on each click. it just showing red color. I want that background color change itself on my each. e.g. first click on add-in then color change to red then second click on add-in then color change to blue and for third click the color change to green and so on. – Muhammad Sarfraz Apr 25 '16 at 4:24
  • I have updated the code in my answer to check if (pPage.BackgroundColor.RGB == ic.RGB). This works for me – Midavalo Apr 25 '16 at 20:06
1

This is a good case for a global variable:

namespace lecture5test
{
    public class Button1 : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        private int ClickCount = 0; // global to keep track of clicks

        public Button1()
        {

then in your OnClick event:

protected override void OnClick()
{
    ClickCount ++; // go to next click colour
    if (ClickCount > 3)
    {
        ClickCount = 1; // out of range, go back to first
    }

// lines of code

if (ClickCount == 1)
{
    pPage.BackgroundColor = ic;
}
else if (ClickCount == 2)
{
    pPage.BackgroundColor = ib;
}
else if (ClickCount == 3)
{
    pPage.BackgroundColor = ig;
}

This will allow you to step through the colours with a click.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.