using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<Button> buttons = null;
        List<string> values = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            buttons = new List<Button>();
            buttons.Add(button1);
            buttons.Add(button2);
            buttons.Add(button3);
            buttons.Add(button4);
            buttons.Add(button5);

            values = new List<string>();
            values.Add("A");
            values.Add("B");
            values.Add("C");
            values.Add("D");
            values.Add("E");

            // temp list of boxes
            List<TextBox> boxes = new List<TextBox>();
            boxes.Add(textBox1);
            boxes.Add(textBox2);
            boxes.Add(textBox3);
            boxes.Add(textBox4);
            boxes.Add(textBox5);

            foreach (Button button in buttons)
            {
                button.Click += new EventHandler(button_Click);
            }

            foreach (TextBox textBox in boxes)
            {
                textBox.Click += new EventHandler(textBox_Click);
            }
        }

        void textBox_Click(object sender, EventArgs e)
        {
            this.ActiveTextBox = (TextBox)sender;
        }

        void button_Click(object sender, EventArgs e)
        {
            if (this.ActiveTextBox != null)
            {
                int index = buttons.IndexOf((Button)sender);
                string value = values[index];

                this.ActiveTextBox.Text = value;
            }
        }

        private TextBox _activeTextBox = null;
        private TextBox ActiveTextBox
        {
            get { return _activeTextBox; }
            set { _activeTextBox = value; }
        }
    }
}

How do i make this apply to dev buttons ?

Recommended Answers

All 6 Replies

bump

>bump
Well well well, you must be the lord and master of the universe to push all of the other threads down and give yours immediate priority. Did it not occur to you that bumping is rude? How about that those of us who could easily help you right now will choose to ignore you simply because of the bump? No? Well, now you have something to think about while you wait...again.

No i only bumped because i have been waiting STUMPED for 38 minutes. I am trying to think. MY BRAINS EXPLODING !!!!!!!

commented: I'll see your 38 minutes and raise you 23 days. -1

You've asked this question before. You said you figured it out before. What has changed?

As I said one of the other times, I gave you sample code for working with a generic list of Button objects. List<T>, where T can be any type you can come up with. ints, strings, Buttons, whatever. Do some research into generics and see what you can come up with.

As for the explicit adding of the buttons and the fact that you're working with a bazillion of them, don't use my code example for that. Go back to one of yours or GAME's or whomevers threads and look at examples Narue or I gave for obtaining the controls from the Form's control collection.

Further, please don't address thread titles to individual people. There are plenty of intelligent people on here that are eager to answer questions. Just post your questions.

ok. The thing is i was quoting Game at the time. But for some reason i dont knw where he is.

No i only bumped because i have been waiting STUMPED for 38 minutes. I am trying to think. MY BRAINS EXPLODING !!!!!!!

Please remember that this is a free community website. The solvers aren't paid to answer your queries and they may have jobs/studies/projects/lives of their own.

Be patient and take the time to work on the project yourself, you have been given all the information you need in answer to your other threads, it shouldnt take a huge leap to put them together.
We aren't going to write, intergrate and debug your code for you... you need to do whats necessary to adapt the code your given...and while your at it, try to read through it and figure out what its doign and why so that you'll better understand how it works next time you favce a similar problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.