0

So here's my situation. I am designing a program that requires new Image objects to be created in real-time on a Canvas, when the user clicks a certain Button. Seeing as I don't know exactly how many of said Images any given user will create, I can't assign names in the code for each and every one of them. The Images need to be named "Image0", "Image1", "Image2", etc, depending on how many Images already exist on the page. Kinda like how Visual Studio itself works, where each time you drop a control onto the design view, it automatically appends a number to the name of the control.

Does anyone have a code snippet capable of performing this function?

3
  • Can you not use a collection (List<YourImageType>, for example)? Commented Feb 17, 2011 at 23:02
  • Perhaps. Would that still allow the Images to have/use events? How would I change properties of, or respond to user interaction with, Images in a List? Commented Feb 17, 2011 at 23:09
  • sure you can attach any event handler and set any property to the newly created control before adding it to the list :) Commented Feb 17, 2011 at 23:23

2 Answers 2

2

You don't have to explicitly name all controls. Just add them to the canvas "controls" list.

Create a windows app with a single button and a flowLayoutPanel.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        int i = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var button = new Button { Text = string.Format("Button {0}", i) };
            button.Click += new EventHandler(button_Click);
            flowLayoutPanel1.Controls.Add(button);
            i += 1;
        }

        void button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.Text = " clicked!";
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

YES! I adapted the code to an Image instead of a Button and it works perfectly! Thank you! Now, what do I do if I need to edit a property of that control? Like, if I wanted to change the text of the button later on?
0

I don't have a direct answer to your question, but from the sounds of it, and I could be wrong because I don't know what you're trying to do, you're viewing the problem from a newbie programmer perspective. You might want to consider using a collection of some kind (perhaps a list) and dynamically adding image objects to that. If you want a string name associated with each object, you could consider a dictionary of type Dictionary<string,Image>

Comments

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.