14 Replies - 642 Views - Last Post: 15 July 2010 - 04:30 PM
#1
Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 11:29 AM
pnl1
pnl2
pnl3
and so on. When each of those panels are clicked I send them all to the same function, but with a parameter like:
function(1) for Panel 1
function(2) for Panel 2
and the function is like:
private void function(int num)
at that "function"(simplified for the purpose of reading)I want to change the backcolor of whatever pnl that triggered that function. So I pretty much need to create a name like:
("pnl" + num).BackColor = Color.Green
or whatever Color I want. I tried thing like
Panel mine
mine.Name = ("pnl" + 1)
mine.BackColor = Color.Green
But I can't seem to get anything to work. I figure I am just missing something small, But I haven't been coding in C# that long. Any help would be great, Thanks
Replies To: Changing BackColor of different Items with the same Function
#2
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 11:36 AM
#3
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 11:38 AM
myFunction(object sender, eventargs e)
{
Color OriginalColor = ((Panel)sender).backcolor;
((Panel)sender).backcolor = Color.Green;
// Do all my fancy processing
((Panel)sender).backcolor = OriginalColor; // Put the color back to what it was before we started processing.
}
You can see a more advanced use of this concept in this tutorial for making a virtual 10-key pad.
If you had 100 panels you wouldn't have 100 identical functions, would you?
Same in the tutorial. Just because you have 16 buttons you don't have to have 16 methods to handle their clicks if they are all going to do the same thing.
http://www.dreaminco...ister-part-one/
This post has been edited by tlhIn'toq: 15 July 2010 - 11:41 AM
#4
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 11:48 AM
private void SetBackColor(Control control, Color color )
{
control.BackColor = color;
}
then in your event...
private void Panel_Click(object sender, EventArgs e)
{
Panel panel = sender as Panel;
SetBackColor(panel, Color.Green);
}
This would allow you to set the BackColor of any control. Just pass the control to the method.
This post has been edited by eclipsed4utoo: 15 July 2010 - 11:50 AM
#5
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 11:57 AM
? = any number between 1 - 8 (I use this number in the function, I just don't show it here)
at the pnl?_Click event:
Private void pnl?_Click(Object sender, EventArgs e) { function(?, sender);}
at the function:
private void function(int num, object sender){
if(condition){
((Panel)sender).BackColor = Color.Green;
}
else{
((Panel)sender).BackColor = Color.Gray;
}
}
This seems to have done it. Thank you for the help.
This post has been edited by nnicnac17: 15 July 2010 - 11:59 AM
#6
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:22 PM
You could just use one method:
Private void pnl_Click(Object sender, EventArgs e)
{
Panel p = sender as Panel;
if(condition){
p.BackColor = Color.Green;
else
}
And set that method as the handler for each of your panels.
Edit: I just realized I basically retyped the code example that tlhIn'toq already posted.
This post has been edited by insertAlias: 15 July 2010 - 12:24 PM
#7
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:31 PM
In this method defintion:
myFunction(object sender, eventargs e)
'sender' is the actual panel that called "myFunction"
If panel5 called the function, then sender *is* panel5. We just need to cast it from an 'object' back into a panel so we can access all the specific properties of a panel.
((panel)sender) or sender as panel
Instead of you trying to keep track of which panel called the method, let the application do that.
For every panel you have, point all of their .Click events to the one-and-only
myFunction(object sender, eventargs e)
The method will know which panel called it because that panel will be sent as the "sender"
#8
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:37 PM
Added:
InsertAlias: Did you see the line I put in my last post...
"? = any number between 1 - 8 (I use this number in the function, I just don't show it here)"
I use the number in a different way in my function, It just wasn't relevant to what I was doing. I also know that "function" isn't a good name. I just simplified the code because once again...names weren't relevant.
This post has been edited by nnicnac17: 15 July 2010 - 12:40 PM
#9
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:44 PM
private List<Panel> panelList;
private bool[] boolList;
public Form1()
{
InitializeComponent();
panelList = new List<Panel>();
panelList.Add(pnl1);
panelList.Add(pnl2);
...
//also set your bool list
}
private void SomeEvent_Click(object sender, EventArgs e)
{
SetPanels();
}
private void SetPanels()
{
for(int i=0; i<boolList.Length; i++)
{
if(boolList[i]
panelList[i].BackColor = Color.Green;
else
panelList[i].BackColor = Color.Gray;
}
}
#10
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:44 PM
#11
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:52 PM
Actually stapia, I am using an ICP DAS I/O card and a opto 22 relay rack to control sprinkler valves and photo eye's with C# code. I am trying to have the program check the relays and see if the photo eye is switched on. Then update the panel colors to let me know whether it is on or not. But your guess was close..LoL
#12
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:59 PM
nnicnac17, on 15 July 2010 - 11:52 AM, said:
This sounds like a great project for making a UserControl. Instead of all this work going into the core of the application, and lots of work to monitor lots of duplicate-ish controls, you make one UserControl that takes your click event, checks the relay number (a property), and displays an indicator color.
Then you just drag-n-drop 8 or 800 of these controls from your Visual Studio toolbox onto your form, and set their 'Relay number' property. Just like you drag-n-drop several textboxes, or panels, or buttons.
#13
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 12:59 PM
#14
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 01:12 PM
That is a good idea tlhIn'toq, maybe when this program becomes more than just a prototype, I will transfer them into user controls. Right now I only have 8 input modules, and 8 output modules, but when i go to like 5 sets of 8 modules, I will probably want to simplify the code. Thanks for your help
#15
Re: Changing BackColor of different Items with the same Function
Posted 15 July 2010 - 04:30 PM
|
|

New Topic/Question
Reply




MultiQuote





|