Click here to Skip to main content
15,910,787 members

Comments by Member 11065163 (Top 7 by date)

Member 11065163 11-Sep-14 19:17pm View    
Deleted
Sorry don't mean to border u. I replaced the 'ID' with 'Name' as u instructed and now there is no error in the code...remember, On_Load the buttons appear green. So I run the code and it executes normal but when u click a button they remain green, there is no effect as regards the change of color.

I'm a novice pls bear with me!...I don't know where to call the single event handler u created or which function to call even....I tried calling the 'ChangeButtonColors' in the individual button click_Event handler but all to no avail. BELOW IS THE FULL CODE.....Thouhg i used TableLayoutPanel to display the buttons.

namespace TheMindBoggleGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (Button btn in Panel1.Controls) {
btn.BackColor = Color.Green;
}

}

private void ChangeButtonColors(object sender, EventArgs e)
{
var button = sender as Button;
ApplyColor(button);

//find adjacent buttons
int id = GetIntegerId(button);
int topId = id - 5;
var top = this.Controls.Find("btn" + topId, true).FirstOrDefault() as Button;
if (top != null) ApplyColor(top);

int bottomId = id + 5;
var bottom = this.Controls.Find("btn" + bottomId, true).FirstOrDefault() as Button;
if (bottom != null) ApplyColor(bottom);

int leftId = id - 1;
var left = this.Controls.Find("btn" + leftId, true).FirstOrDefault() as Button;
if (left != null) ApplyColor(left);

int rightId = id + 1;
var right = this.Controls.Find("btn" + rightId, true).FirstOrDefault() as Button;
if (right != null) ApplyColor(right);
}

private void ApplyColor(Button button) {
if (button.BackColor == Color.Red)
button.BackColor = Color.Green;
else
if (button.BackColor == Color.Green)
button.BackColor = Color.Red;
}

private int GetIntegerId(Button button) {
int id = 0;
int.TryParse(button.Name.Replace("btn", ""), out id);
return id;

}
}
}
Member 11065163 11-Sep-14 19:08pm View    
Deleted
Sorry don't mean to border u. I replaced the 'ID' with 'Name' as u instructed and now there is no error in the code...remember, On_Load the buttons appear green. So I run the code and it executes normal but when u click a button they remain green, there is no effect as regards the change of color.

I'm a novice pls bear with me!...I don't know where to call the single event handler u created or which function to call even....I tried calling the 'ChangeButtonColors' in the individual button click_Event handler but all to no avail. BELOW IS THE FULL CODE.....Thouhg i used TableLayoutPanel to display the buttons.

namespace TheMindBoggleGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (Button btn in Panel1.Controls) {
btn.BackColor = Color.Green;
}

}

private void ChangeButtonColors(object sender, EventArgs e)
{
var button = sender as Button;
ApplyColor(button);

//find adjacent buttons
int id = GetIntegerId(button);
int topId = id - 5;
var top = this.Controls.Find("btn" + topId, true).FirstOrDefault() as Button;
if (top != null) ApplyColor(top);

int bottomId = id + 5;
var bottom = this.Controls.Find("btn" + bottomId, true).FirstOrDefault() as Button;
if (bottom != null) ApplyColor(bottom);

int leftId = id - 1;
var left = this.Controls.Find("btn" + leftId, true).FirstOrDefault() as Button;
if (left != null) ApplyColor(left);

int rightId = id + 1;
var right = this.Controls.Find("btn" + rightId, true).FirstOrDefault() as Button;
if (right != null) ApplyColor(right);
}

private void ApplyColor(Button button) {
if (button.BackColor == Color.Red)
button.BackColor = Color.Green;
else
if (button.BackColor == Color.Green)
button.BackColor = Color.Red;
}

private int GetIntegerId(Button button) {
int id = 0;
int.TryParse(button.Name.Replace("btn", ""), out id);
return id;

}
}
}
Member 11065163 11-Sep-14 18:56pm View    
Sorry don't mean to border u. I replaced the 'ID' with 'Name' as u instructed and now there is no error in the code...remember, On_Load the buttons appear green. So I run the code and it executes normal but when u click a button they remain green, there is no effect as regards the change of color.

I'm a novice pls bear with me!...I don't know where to call the single event handler u created or which function to call even....I tried calling the 'ChangeButtonColors' in the individual button click_Event handler but all to no avail. BELOW IS THE FULL CODE.....Thouhg i used TableLayoutPanel to display the buttons.

namespace TheMindBoggleGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (Button btn in Panel1.Controls) {
btn.BackColor = Color.Green;
}

}

private void ChangeButtonColors(object sender, EventArgs e)
{
var button = sender as Button;
ApplyColor(button);

//find adjacent buttons
int id = GetIntegerId(button);
int topId = id - 5;
var top = this.Controls.Find("btn" + topId, true).FirstOrDefault() as Button;
if (top != null) ApplyColor(top);

int bottomId = id + 5;
var bottom = this.Controls.Find("btn" + bottomId, true).FirstOrDefault() as Button;
if (bottom != null) ApplyColor(bottom);

int leftId = id - 1;
var left = this.Controls.Find("btn" + leftId, true).FirstOrDefault() as Button;
if (left != null) ApplyColor(left);

int rightId = id + 1;
var right = this.Controls.Find("btn" + rightId, true).FirstOrDefault() as Button;
if (right != null) ApplyColor(right);
}

private void ApplyColor(Button button) {
if (button.BackColor == Color.Red)
button.BackColor = Color.Green;
else
if (button.BackColor == Color.Green)
button.BackColor = Color.Red;
}

private int GetIntegerId(Button button) {
int id = 0;
int.TryParse(button.Name.Replace("btn", ""), out id);
return id;

}
}
}
Member 11065163 10-Sep-14 19:34pm View    
I guess it will work....going to try it. Thank u!

I had tried something else and it worked but just that the approach is kinda very long and redundant
Member 11065163 10-Sep-14 11:13am View    
...and a result of that the code is not compilling