14 Replies - 776 Views - Last Post: 15 July 2010 - 04:30 PM Rate Topic: -----

#1 nnicnac17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 11:29 AM

I apologoze, I really couldn't think of a good topic name. Lets say I have 8 panels named:

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

Is This A Good Question/Topic? 0
  • +

Replies To: Changing BackColor of different Items with the same Function

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,433
  • Joined: 29-May 08

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 11:36 AM

Why not create a collections containing references to those panels?
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 11:38 AM

If all your functions are the same except for the name, then just use one function and use the Object parameter you received to tell which panel called it.



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

Was This Post Helpful? 1
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 11:48 AM

Or your method could simply accept the Panel as a parameter...

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

Was This Post Helpful? 1
  • +
  • -

#5 nnicnac17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 11:57 AM

Thank you tlhIn'toq and eclipsed4utoo. I have it working now, here is what I did:

? = 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

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,752
  • Joined: 08-June 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:22 PM

The point was to avoid having to create 8 methods for one action. One event handler can handle the events from many controls. Notice that in your "function" (which I would suggest renaming) you take an int "num" but never use it? That's because you don't need it. You already have the reference to the panel with "sender".

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

Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:31 PM

NNICNAC17: Maybe this will help clear things up. Did you know that the parameter "object sender" being sent to a method is the actual object (the control such as a panel) that called the method?

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"
Was This Post Helpful? 0
  • +
  • -

#8 nnicnac17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:37 PM

Ok, now in my program i have to do kind of the same thing as above, but I can't use the "sender" method. I have a boolean array, full of 8 true or false statements. I want to be able to read through that array and change another set of panels based on whether the boolean is true or false. So if I read index 0 of the boolean array and it returns true, then I want the BackColor of the 1st Panel(pnl1) to go green. if it is false, then gray. But my panel's aren't going to be the ones triggering the event. I am sorry if it is hard to understand.

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

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,752
  • Joined: 08-June 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:44 PM

Ok, so at the beginning of your program, make a List<Panel>, add all your panels to it, and then do operations to the proper one based on its index:

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;
  }
}

Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:44 PM

Just to clarify the problem, I think you're trying to make a game of Lights Out correct?
Was This Post Helpful? 0
  • +
  • -

#11 nnicnac17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:52 PM

Thanks InsertAlias, I will put that into my program and let you know how it goes. That code looks great!

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
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:59 PM

View Postnnicnac17, on 15 July 2010 - 11:52 AM, said:

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


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.
Was This Post Helpful? 1
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,752
  • Joined: 08-June 10

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 12:59 PM

Just make sure your boolean array and your panel list are parallel (i.e. they both contain the same number of elements, and the bool at index i corresponds to the panel at index i), and you should be good.
Was This Post Helpful? 0
  • +
  • -

#14 nnicnac17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 01:12 PM

Looks like I got it. The only thing I changed was I used a Panel[] array instead of a list. I might come back and change this later, but the list was giving me lots of errors(I've never used one before).

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
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Changing BackColor of different Items with the same Function

Posted 15 July 2010 - 04:30 PM

I agree with . I would definitely look into doing a UserControl. It will make your life much easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1