Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:35:31+00:00 2026-05-25T22:35:31+00:00

I am creating a custom control and it is a button. It may has

  • 0

I am creating a custom control and it is a button. It may has a type and a specified image according to its type. Its type may be:

public enum ButtonType
{
    PAUSE,
    PLAY
}

Now I can change its appearance and Image with a method:

public ButtonType buttonType;
public void ChangeButtonType(ButtonType type)
{
    // change button image
    if (type == ButtonType.PAUSE)
        button1.Image = CustomButtonLibrary.Properties.Resources.PauseButton;
    else if (type == ButtonType.PLAY)
        button1.Image = CustomButtonLibrary.Properties.Resources.PlayButton;

    buttonType = type;
}

OK, this method doesn’t seems so good, for example maybe later I wish to have another type STOP for example for this button, I want just add its image to resources and add it to ButtonType enum, without changing this method.

How can I implement this method to be compatible with future changes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:35:32+00:00Added an answer on May 25, 2026 at 10:35 pm

    One thing you can do is turn ButtonType into a base class (or an interface, if you prefer):

    public abstract class ButtonType
    {
        public abstract Image GetImage();
    }
    

    Then each of your types becomes a subclass:

    public class PauseButtonType : ButtonType
    {
        public Image GetImage()
        {
            return CustomButtonLibrary.Properties.Resources.PauseButton;
        }
    }
    
    public class PlayButtonType : ButtonType
    {
        public Image GetImage()
        {
            return CustomButtonLibrary.Properties.Resources.PlayButton;
        }
    }
    

    Your image changing method then becomes:

    private ButtonType buttonType; // public variables usually aren't a good idea
    public void ChangeButtonType(ButtonType type)
    {
        button1.Image = type.GetImage();
        buttonType = type;
    }
    

    This way when you want to add another type, you add another ButtonType subclass and pass it to your ChangeButtonType method.


    Since this method is on your custom button class, I would probably take this a bit further and encapsulate style/appearance in a class:

    public class ButtonStyle
    {
        // might have other, non-abstract properties like standard width, height, color
        public abstract Image GetImage();
    }
    
    // similar subclasses to above
    

    And then on the button itself:

    public void SetStyle(ButtonStyle style)
    {
        this.Image = style.GetImage();
        // other properties, if needed
    }
    

    You could set up button behaviours (i.e. actions they perform when they’re clicked) in a similar way with a ButtonAction base class and assigning specific actions like Stop and Play when you want to change the button’s purpose and style.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a WPF custom control, a Button with an Image and Text
I am creating a custom WPF control that let's say for simplicity sake has
I am creating a simple custom control extending from toggle button that allows the
Im making a custom button called ShardButton which uses a Style to control its
In WPF, we are creating custom controls that inherit from button with completely drawn-from-scratch
I'm creating a custom control, part of which is using the Graphics class to
I'm creating a custom control in wxWidgets that displays a menu as part of
I am creating a custom control. I want the template for this control to
I am creating a custom control. The contents of the control will differ a
I'm creating a .net custom control and it should be able to load multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.