2

I try to put text from 0 to 11 on 12 pictureboxes have a ball image, but get the text 11 on all.

How can I get the text on the balls from 0 to 11 ?

here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wfballs2
{
    public partial class Form1 : Form
    {
        String s;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            PictureBox[] Shapes = new PictureBox[12];
            for (int i = 0; i < 12; i++)
            {
                s = Convert.ToString(i);
                Shapes[i] = new PictureBox();
                Shapes[i].Name = "ball" + i.ToString();
                Shapes[i].Location = new Point(10 + 45 * i, 300);
                Shapes[i].Size = new Size(40, 40);
                Shapes[i].Image = Image.FromFile(@ "C:\Users\Eiko\Desktop\ball\ball.jpg");
                Shapes[i].SizeMode = PictureBoxSizeMode.CenterImage;
                Shapes[i].Visible = true;
                this.Controls.Add(Shapes[i]);
                Shapes[i].Paint += new PaintEventHandler((sender2, e2) =>
                {
                    e2.Graphics.DrawString(s, Font, Brushes.Black, 10, 13);
                });
            }
        }
    }
}

2 Answers 2

1

The variable s is an instance in your form, so its value is shared by the form and all the shapes you create.

Everytime one of your shape is painted, this code:

 e2.Graphics.DrawString(s, Font, Brushes.Black, 10, 13);

will run. That s is an instance variable of your Form. It will show for all shapes the last value that it was set to. In your case that happens in the last iteration of your for loop, hence the 11.

To fix this you have to capture the loop variable in a local variable before the anonymous eventhandler and then use that local var to do the Convert.ToString(val) in the event handler, like so:

 var val = i; // capture variable
 Shapes[i].Paint += new PaintEventHandler((sender2, e2) =>
 {
         // use captured variable here
         e2.Graphics.DrawString(Convert.ToString(val), Font, Brushes.Black, 10, 13);
 });

This will generate the string with the local variable based on val.

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to say that the text become 12 on all the balls
1
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wfballs2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            int k = 0;
            PictureBox[] Shapes = new PictureBox[12];
            for (int i = 0; i < 12; i++)
            {
                Shapes[i] = new PictureBox();
                Shapes[i].Name = "ball" + i.ToString();
                Shapes[i].Location = new Point(10 + 45 * i, 300);
                Shapes[i].Size = new Size(40, 40);
                Shapes[i].Image = Image.FromFile(@ "C:\Users\Eiko\Desktop\ball\ball.jpg");
                Shapes[i].SizeMode = PictureBoxSizeMode.CenterImage;
                Shapes[i].Visible = true;
                this.Controls.Add(Shapes[i]);
                Shapes[i].Paint += new PaintEventHandler((sender2, e2) =>
                {
                    e2.Graphics.DrawString(k.ToString(), Font, Brushes.Black, 10, 13);
                    k++;
                });
            }
        }
    }
}

1 Comment

I try this but I got the same regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.