0

I am currently coding a file browser in C# and I've never used C# before. I'm trying to make a list of files that will become active (ie: change skin) when I click on one. The problem is that I have no idea how to do so.

Current implementation:
Added buttons for each file in the directory inside loop and change skin to look like labels.

Desired:
Buttons will change skin when clicked on.

I think I should be writing an event handler but I don't know how to add an event handler to a button that is declared inside a loop. Help please?

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by darkagn
0

Here is how you assign an event handler programatically (my example assigns an event handler to the Button.Click event):

public void SetupButton()
{
  Button myButton = new Button();
  // assign the event handler
  myButton.Click += new EventHandler(myButton_click);
}

// definition for the event handler code
// note that the signature for this method must match the Click delegate signature
private void myButton_click(object sender, EventArgs e)
{
   // here you put the code for performing a task when the button is clicked

}

Hope this helps :)

0

Of course, if you declare the button like darkagn did and then not assign it to the form, you'll never see it :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.