How do I access this?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 47
Reputation: ctrl-alt-del is an unknown quantity at this point 
Solved Threads: 0
ctrl-alt-del ctrl-alt-del is offline Offline
Light Poster

How do I access this?

 
0
  #1
Sep 19th, 2009
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:

  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:

  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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: How do I access this?

 
2
  #2
Sep 19th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: How do I access this?

 
1
  #3
Sep 19th, 2009
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:

  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. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC