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 6355593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:47:46+00:00 2026-05-24T22:47:46+00:00

I’m having a problem with C# buttons in Windows Forms. I’ve create a number

  • 0

I’m having a problem with C# buttons in Windows Forms.

I’ve create a number of buttons programmatically and add them to a form afterwards.

Interestingly, every modification to those buttons (location and size) except for the modification of the BackColor is readily executed. Only the button’s color remains unchanged.

The code looks something like this:

public class SimpleSortAlgDisplayer : ISortAlgDisplayer
{

    #region ISortAlgDisplayer Member

    void ISortAlgDisplayer.Init(int[] Data)
    {
        this.DataLength = Data.Length;
        this.DispWin = new CurrentSortStateWin();
        this.DispWin.Show();
        this.DispWin.Size = new Size(60 + (10 * this.DataLength), 120);

        this.myArrayElements = new Button[this.DataLength];
        for (int i = 0; i < this.DataLength; i++)
        {
            this.myArrayElements[i] = new Button();
            //begin of series of invoked actions

            this.myArrayElements[i].Size=new Size(5,(int)(((80)*(double)Data[i])/1000));
            this.myArrayElements[i].Location = new Point(30 + (i * 10), 90-(this.myArrayElements[i].Size.Height));
            this.myArrayElements[i].Enabled = true;
            this.myArrayElements[i].BackColor = Color.MidnightBlue;
            this.myArrayElements[i].UseVisualStyleBackColor = true;
            this.DispWin.Controls.Add(this.myArrayElements[i]);
            this.myArrayElements[i].Refresh();

        }
    }

Ideas anyone?

A similar question was asked here but the answers to it were not very helpful:

  • Trying to use Invoke gives me the run-time error that DispWin is not yet created.
  • Setting UseVisualStyleBackColor to false changes nothing.
  • Setting BackColor and ForeColor or Showing DispWin only after adding and formatting the Buttons also had no effect.

Where am I going wrong?

  • 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-24T22:47:47+00:00Added an answer on May 24, 2026 at 10:47 pm

    You are trying to set up the color, but then you override it saying UseVisualStyleBackColor = true

    if you want to use your custom color, you need to set UseVisualStyleBackColor to false or the color will only be applied to the button upon mouse over.

    a simple test uploaded to GitHub

    public partial class mainForm : Form
    {
        Random randonGen = new Random();
    
        public mainForm()
        {
            InitializeComponent();
        }
    
        private void mainForm_Load(object sender, EventArgs e)
        {
            populate();
        }
    
        private void populate()
        {
            Control[] buttonsLeft = createButtons().ToArray();
            Control[] buttonsRight = createButtons().ToArray();
    
            pRight.Controls.AddRange(buttonsRight);
            pLeft.Controls.AddRange(buttonsLeft);
        }
    
        private List<Button> createButtons()
        {
            List<Button> buttons = new List<Button>();
    
            for (int i = 1; i <= 5; i++)
            {
    
                buttons.Add(
                    new Button()
                    {
                        Size = new Size(200, 35),
                        Enabled = true,
                        BackColor = GetColor(),
                        ForeColor = GetColor(),
                        UseVisualStyleBackColor = false,
                        Left = 20,
                        Top = (i * 40),
                        Text = String.Concat("Button ", i)
                    });
            }
    
            return buttons;
        }
    
        private Color GetColor()
        {
            return Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255));
        }
    }
    

    result

    enter image description here

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.