Hi,

i have a little problem, i dynamic generate a buttons in two columns

for(i=0;i<=10;i++){
               Button prz = new Button();
                prz.Location = new Point(12,40+i*25);
                prz.Name = "But"+(p-i)+"up";
                prz.Image = Image.FromFile(picture_from_file_1);
                prz.ClientSize = new Size(20,20);
                prz.Click += new System.EventHandler(this.Button_Click_Code);                
                Controls.Add(prz);

                Button prz1 = new Button();
                prz1.Location = new Point(45, 40 + i * 25);
                prz1.Name = "But" + (p - i) + "down";
                prz1.Image = Image.FromFile(picture_from_file_2);
                prz1.ClientSize = new Size(20, 20);
                prz1.Click += new System.EventHandler(this.Button_Click_Code);
                Controls.Add(prz1);
}

and

private void Button_Click_Code(object sender, EventArgs e)
{
       Button myBut = (Button)sender;

            switch (myBut.Name)
            {
                case "But0up":
                break;

                case "But0down":
                break;

                case "But1up":
                break;

                case "But1down":
                break;

                case "But2up":
                break;

                case "But2down":
                break;

               ...
               ...
               ...

                case "But10up":
                break;

                case "But10down":
                break;
            }
}

how can i disable for example But2down when i click But2up or But1up when i click But1down?

thx 4 help

Hi welcome here at DANIWEB!
When you give a Button a name you can use that name as a variable.
So : this.But2down.Enabled = false; would disable the button you named "But2down"

@ddanbe, but name of button i generate dynamic, and i can't do this.But2down.Enabled = false;

maybe we can make a table with name f.e.

string[] Butup = { "But0up","But1up" .......};
string[] Butdown = { "But0down","But1down"........ };

and make, something like this

switch (myBut.Name)
{
         ...
         ...
         ...


         case "But0up":
              ButDown[0].enable = false;
         break;
         ...
         ...
         ...

}

?? but it doesn't work

You could also take an approach like this to avoid a huge switch statement:

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 daniweb
{
  public partial class frmDynamicBtn : Form
  {
    private Dictionary<string, ButtonPair> dict;

    public frmDynamicBtn()
    {
      InitializeComponent();
    }

    private void frmDynamicBtn_Load(object sender, EventArgs e)
    {
      dict = new Dictionary<string, ButtonPair>();
      int p = 2;
      int i;
      for (i = 0; i <= 10; i++)
      {
        Button prz = new Button();
        prz.Location = new Point(12, 40 + i * 25);
        prz.Name = "But" + (p - i) + "up";
        //prz.Image = Image.FromFile(picture_from_file_1);
        prz.Text = "X";
        prz.ClientSize = new Size(20, 20);
        prz.Click += new EventHandler(prz_Click);
        Controls.Add(prz);

        Button prz1 = new Button();
        prz1.Location = new Point(45, 40 + i * 25);
        prz1.Name = "But" + (p - i) + "down";
        //prz1.Image = Image.FromFile(picture_from_file_2);
        prz1.Text = "Y";
        prz1.ClientSize = new Size(20, 20);
        prz1.Click += new EventHandler(prz_Click);
        //new line
        prz.Enabled = false;
        Controls.Add(prz1);


        ButtonPair bp = new ButtonPair(prz, prz1);
        dict.Add(prz.Name, bp);
        dict.Add(prz1.Name, bp);
      }
    }

    void prz_Click(object sender, EventArgs e)
    {
      string name = (sender as Button).Name;
      ButtonPair bp;
      if (!dict.TryGetValue(name, out bp))
        throw new Exception("Something is wrong...");
      bp.Down.Enabled = !bp.Down.Enabled;
      bp.Up.Enabled = !bp.Up.Enabled;
    }
  }
  internal class ButtonPair
  {
    private Button _up;
    private Button _down;
    public Button Up { get { return _up; } }
    public Button Down { get { return _down; } }
    private ButtonPair()
    {
    }
    internal ButtonPair(Button up, Button down)
      : this()
    {
      this._up = up;
      this._down = down;
    }
  }
}

As Danny mentioned the name is probably the best way for tracking a control. You could also use the tag property to avoid a dictionary but I still prefer using the name.

Using a tag approach:

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 daniweb
{
  public partial class frmDynamicBtn2 : Form
  {
    public frmDynamicBtn2()
    {
      InitializeComponent();
    }

    private void frmDynamicBtn2_Load(object sender, EventArgs e)
    {
      int p = 2;
      int i;
      for (i = 0; i <= 10; i++)
      {
        Button prz = new Button();
        prz.Location = new Point(12, 40 + i * 25);
        prz.Name = "But" + (p - i) + "up";
        //prz.Image = Image.FromFile(picture_from_file_1);
        prz.Text = "X";
        prz.ClientSize = new Size(20, 20);
        prz.Click += new EventHandler(prz_Click);
        Controls.Add(prz);

        Button prz1 = new Button();
        prz1.Location = new Point(45, 40 + i * 25);
        prz1.Name = "But" + (p - i) + "down";
        //prz1.Image = Image.FromFile(picture_from_file_2);
        prz1.Text = "Y";
        prz1.ClientSize = new Size(20, 20);
        prz1.Click += new EventHandler(prz_Click);
        //new line
        prz.Enabled = false;
        Controls.Add(prz1);


        ButtonPair bp = new ButtonPair(prz, prz1);
        prz.Tag = bp;
        prz1.Tag = bp;
      }
    }

    void prz_Click(object sender, EventArgs e)
    {
      ButtonPair bp = (ButtonPair)((Button)sender).Tag;
      bp.Down.Enabled = !bp.Down.Enabled;
      bp.Up.Enabled = !bp.Up.Enabled;
    }

  }
}

>> but name of button i generate dynamic, and i can't do this.But2down.Enabled = false;
Right nertos , just forget what I said...:$ I had a blonde moment I guess...

@sknake :D great, thanks a million:)

Here is another way that loops through the forms control collection to find the given control by it's name and passing in the form's control collection ( this.Controls ) since you already added it to the collection ( this.Controls.Add ):

public static bool EnableControl(System.Windows.Forms.Control.ControlCollection controls, string name, bool enable) 
            {
                foreach (Control c in controls)
                {
                    if (c.Name == name)
                    {
                        c.Enabled = enable;
                        return true; // found it...
                    }

                    // search child controls too...
                    if (EnableControl(c.Controls, name, enable))
                        return true;
                }
                return false; // control not found...
            }

In the above, you would call the method using your constructed button name: eg, "But2up"... using button name also the same for next example...

Another way is direct access (via control's Name ) if not a child control...:

this.Controls[name].Enabled = false;

If speed is a concern/problem, you should stick with sknake's example as the Dictionary approach will contain a much smaller list than that of the Form's control collection depending on the number of other controls you have within the form (in the collection).;)

I have a NumericUpDown which i generate dynamic, and after click the button a want a show all the number with this N.U.D.

private void numericUpDown1_ValueChanged(object sender, EventArgs e){

for (int i = 2; i <= l; i++)
            {
                NumericUpDown nup = new NumericUpDown();
                nup.Name = "nup" + (l - i);
                Controls.Add(nup);
}

}

private void button1_Click(object sender, EventArgs e)
{
            for(int i=1;i<=numer_of_all_NUD;i++){
                //    MessageBox.Show(nup.Value.ToString()); <- ????
            }
}

what is the shortest way to do this

:)

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.