943,867 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 424
  • C# RSS
Sep 19th, 2009
0

How do I access this?

Expand Post »
Hi all!

As you probably have guessed, I have a problem. And as such I would like to use your collective knowledge so I can continue.

I have a form that has a crapload of buttons on it. so, because I was being lazy, I created myself a ButtonGenerator. Here is the code for that:

C# Syntax (Toggle Plain Text)
  1. public void ButtonGenerator(string ButtonName, string ButtonText, int ButtonHeight, int ButtonWidth, int ButtonTop, int ButtonLeft)
  2. {
  3. Button NewButton = new Button();
  4. NewButton.Name = ButtonName;
  5. NewButton.Text = ButtonText;
  6. NewButton.Height = ButtonHeight;
  7. NewButton.Width = ButtonWidth;
  8. NewButton.Top = ButtonTop;
  9. NewButton.Left = ButtonLeft;
  10. this.Controls.Add(NewButton);
  11. }

so I can use something like this:

C# Syntax (Toggle Plain Text)
  1. ButtonGenerator("ButtonQ", "Q", 20, 20, 60, 20);

However...

Now I require my program to sort of highlight a button. This can be done by changing the backgroundcolor of it, or setting the Selected property to true, or whatever way, I don't really care as long as it is clear which button is about to do it's thing.

But how do I access the buttons? I hoped that by adding a Name property I would be able to acces it somehow. I either I can't figure out how, or that is not how it works.

So... How does it work?


Thanks in advance for any answers provided!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
ctrl-alt-del is offline Offline
65 posts
since Jul 2008
Sep 19th, 2009
2

Re: How do I access this?

C# Syntax (Toggle Plain Text)
  1. Button B = this.Controls[buttonname];

That should work for what you are looking for.
or you could add each control to a list and access it that way, I can think of a lot of ways, but this is the simplest.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Sep 19th, 2009
1

Re: How do I access this?

Here is a not so simple way to do it. Ideally using the name as Diamonddrake provided would be the best so you know what button you are referencing. To find all of the buttons you could use this approach:

C# Syntax (Toggle Plain Text)
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. Button[] btns = (Button[])FindControls(this, typeof(Button));
  4. foreach (Button btn in btns)
  5. btn.BackColor = Color.Red;
  6. }
  7.  
  8. private static Array FindControls(Control ctrl, Type t)
  9. {
  10. System.Collections.ArrayList result = new System.Collections.ArrayList();
  11. foreach (Control c in ctrl.Controls)
  12. {
  13. if ((c.GetType() == t) || c.GetType().IsSubclassOf(t))
  14. result.Add(c);
  15. foreach (Control childCtrl in c.Controls)
  16. result.AddRange(FindControls(childCtrl, t));
  17. }
  18. return result.ToArray(t);
  19. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Can a DataSet be used as a database?
Next Thread in C# Forum Timeline: File Search for Application Protection





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC