| | |
How do I access this?
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 47
Reputation:
Solved Threads: 0
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:
so I can use something like this:
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!
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)
public void ButtonGenerator(string ButtonName, string ButtonText, int ButtonHeight, int ButtonWidth, int ButtonTop, int ButtonLeft) { Button NewButton = new Button(); NewButton.Name = ButtonName; NewButton.Text = ButtonText; NewButton.Height = ButtonHeight; NewButton.Width = ButtonWidth; NewButton.Top = ButtonTop; NewButton.Left = ButtonLeft; this.Controls.Add(NewButton); }
so I can use something like this:
C# Syntax (Toggle Plain Text)
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!
C# Syntax (Toggle Plain Text)
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.
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)
private void button3_Click(object sender, EventArgs e) { Button[] btns = (Button[])FindControls(this, typeof(Button)); foreach (Button btn in btns) btn.BackColor = Color.Red; } private static Array FindControls(Control ctrl, Type t) { System.Collections.ArrayList result = new System.Collections.ArrayList(); foreach (Control c in ctrl.Controls) { if ((c.GetType() == t) || c.GetType().IsSubclassOf(t)) result.Add(c); foreach (Control childCtrl in c.Controls) result.AddRange(FindControls(childCtrl, t)); } return result.ToArray(t); }
![]() |
Similar Threads
- Can not access microsoft.com or any other anti-virus sites avg.com trendmicro.com etc (Viruses, Spyware and other Nasties)
- Add a new record to access database in VB 2005 (VB.NET)
- VB: Connect to Access database via ODBC datasource name (Visual Basic 4 / 5 / 6)
- Writing to an Access Database (Visual Basic 4 / 5 / 6)
- Writing to an Access Database using Java... (Java)
Other Threads in the C# Forum
- Previous Thread: Can a DataSet be used as a database?
- Next Thread: File Search for Application Protection
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast buttons c# check checkbox client column combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development display draganddrop drawing encryption enum equation event excel file form format formbox forms formupdate function gdi+ httpwebrequest image index input install java label linux list listbox mandelbrot math mouseclick mysql networking operator packaging parse path photoshop picturebox pixelinversion post powerpacks programming radians regex remote remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio webbrowser wfa windows winforms wpf xml






