15,910,787 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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
Member 11065163
10-Sep-14 11:07am
View
everything seem to be cool with d code but in d second to d last line...there is a problem with d 'ID' in { int.tryParse(button.ID.Replace("btn", ""), out id);}.....the erro message goes like dis "'System.Windows.Forms.Button' does not contain a definition for ID and no extension method ID accepting a first argument of type 'System.Windows.Forms.Button' could be found (are u missing a using directive or an assembly reference?)"
Member 11065163
10-Sep-14 9:07am
View
yes sir!
Show More