I'm setting up a windowsform that pings all my IP's at work and changes the button.BackColor accordingly.
Ping pingClassxx = new Ping();
PingReply pingxx = pingClassxx.Send("192.168.xx.xx");
if (pingxx.Status == IPStatus.Success)
{
button1.BackColor = Color.Green;
button1.Text = "SQL Server | Online";
}
else
{
button1.BackColor = Color.Red;
button1.Text = "SQL Server | Offline";
}
Now I'm trying to do this to multiple machines so I went like this:
private void timer0_Tick(object sender, EventArgs e)
{
timer1.Stop();
int iCount0 = 0;
string[] arr = new string[8];
arr[0] = "192.168.x.xx"; //button0
arr[1] = "192.168.x.yy"; //button1
arr[2] = "192.168.x.zz"; //button2
arr[3] = "192.168.x.xy"; //button3
arr[4] = "192.168.x.xz"; //button4
arr[5] = "192.168.x.yx"; //button5
arr[6] = "192.168.yy.yz"; //button6
arr[7] = "192.168.x.ww"; //button7
for (int I = 0; I < arr.Length ; I++)
{
// Ping
string s = arr[I];
Console.WriteLine(s);
Ping pingClassx = new Ping();
PingReply pingx = pingClassx.Send(arr[I]);
if (pingx.Status == IPStatus.Success)
{
[What do I do here?].BackColor = Color.Green;
}
else
{
[What do I do here?]BackColor = Color.Red;
}
// Loading Bar
iCount2 += 1;
progressBar0.Value = iCount2 * 100 / 8;
label0.Text = iCount2 + "/8";
}
timer1.Start();
}
How can I loop this?
I could just repeat the code but I am sure there is a fancier way to do it.
If you want to Change the Color only Of Your Form Buttons then try This :
foreach(Button c in Form.Controls){
if (pingx.Status == IPStatus.Success)
{
c.BackColor = Color.Green;
}
else
{
c.BackColor = Color.Red;
}
}
Related
This is my code so far,im trying to make a c# connect four game but i cant seem to get the win checker to work! I'd like for my game to be able to check for four in a row, horizontally, vertically and diagonally and show a message telling you the winner. I have checked and everything else works as it should.
namespace ConnectFour
{
public partial class Form1 : Form
{
Button[] gameButtons = new Button[42]; //array of buttons for markers(red and blue)
bool blue = true; //blue is set to true if the next marker is to be a blue
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Connect 4";
this.BackColor = Color.BlanchedAlmond;
this.Width = 500;
this.Height = 500;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 7) * 50;
int y = 50 + (i / 7) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender, int i)
{
if (((Button)sender).BackColor == Color.BlanchedAlmond)
{
if (blue == true)
{
((Button)sender).BackColor = Color.Red;
}
else
{
((Button)sender).BackColor = Color.Blue;
}
blue = !blue;
}
}
private void fourInARow(int a, int b, int c,int d)
{
if (gameButtons[a].BackColor == gameButtons[b].BackColor && gameButtons[a].BackColor == gameButtons[c].BackColor && gameButtons[a].BackColor==gameButtons[d].BackColor)
{
if (gameButtons[a].BackColor == Color.Blue)
{
MessageBox.Show("the winner is player 1");
}
else
{
MessageBox.Show("the winner is player 2");
}
}
}
}
Why do you use Index?
int index = I;
will stay at "0" - because its an initializer and only called once in for-loop.
But it makes no sense for me at all to have an index var.
I'm using c# windows form in Visual studio 2017 version
I've made a high low game for practice and it works perfect but what I'm trying to accomplish is once someone hits 10 guesses it starts the application over again.
This was my last resort coming here and asking but i have no one else to turn to. I've tried all kinds of things to make it work when hitting 10 guesses to restart the app but nothing works and i get errors.
here's the start button and the guess button
variables I'm using
static int intRandomNumber;
static int intNumGuesses;
static int intBestLowScore;
static int intGuessedNum;
static int difference = 0;
Start Button
{
//Random Numbers//
Random rnRandomNumber = new Random();
intRandomNumber = rnRandomNumber.Next(0, 1000);
lblRandomNumber.Text = intRandomNumber.ToString();
txtGuess.Enabled = true;
btnGuess.Enabled = true;
btnStart.Enabled = false;
lblAnswer.Text = "".ToString();
intBestLowScore = intNumGuesses;
intNumGuesses = 0;
lblNumGuesses.Text = "0";
lblBestScore.Text = intBestLowScore.ToString();
lblAnswer.BackColor = Color.White;
txtGuess.Focus();
SoundPlayer audio = new SoundPlayer(High_Low_Game.Properties.Resources.Cheering);
audio.Stop();
}
Guess Button
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
lblAnswer.Text = "To High";
lblAnswer.ForeColor = Color.Red;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else if (intRandomNumber - intGuessedNum > difference)
{
lblAnswer.Text = "To Low";
lblAnswer.ForeColor = Color.Blue;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else
{
lblAnswer.Text = "You Guessed it.";
lblAnswer.ForeColor = Color.Black;
lblAnswer.BackColor = Color.Green;
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
SoundPlayer audio = new SoundPlayer(High_Low_Game.Properties.Resources.Cheering);
audio.Play();
}
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
You're missing an if statement
Guess Button:
private void btnGuess_Click(object sender, EventArgs e)
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
//This is what you're looking for-v
if(intNumGuesses==10)
{
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
intNumGuesses=0;
}
//This is what you're looking for-^
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
lblAnswer.Text = "To High";
lblAnswer.ForeColor = Color.Red;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else if (intRandomNumber - intGuessedNum > difference)
{
lblAnswer.Text = "To Low";
lblAnswer.ForeColor = Color.Blue;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else
{
lblAnswer.Text = "You Guessed it.";
lblAnswer.ForeColor = Color.Black;
lblAnswer.BackColor = Color.Green;
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
}
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
In your 'guess-button' handler do the following:
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
bool guessed = false;
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
...
}
else if (intRandomNumber - intGuessedNum > difference)
{
...
}
else
{
...
guessed = true;
}
if ((intNumGuesses == 10}&&(!guessed))
{
// Show Message "max. nr. of guesses reached'
// call method to clear values from textBoxes & enable Start-button
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
What about this?
{
intNumGuesses++;
if(intNumGuesses >= 10)
{
StartButton(); // pseudo call, replace with whatever the name and parameters of your method really are
return;
}
lblNumGuesses.Text = intNumGuesses.ToString();
// ...
}
I would recommend to write an Init()method, that you call either from your StartButton() method or from your GuessButton() method when necessary
I want to access game objects (sprites) in my code in an Array but I have no idea how to do that, I am new to C#.
This is my testing code, but I am not getting any values:
void Start()
{
Sprite[] countries = Resources.LoadAll<Sprite>("MAPS");
sr = GetComponent<SpriteRenderer>();
names = new string[countries.Length];
for (int i = 0; i < names.Length; i++)
{
names[i] = countries[i].name;
//0 = red
//1 = green
if (UnityEngine.Random.Range(0, 2) == 0)
{
this.GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
this.GetComponent<SpriteRenderer>().color = Color.green;
}
}
}
Instead of using this keyword:
if (UnityEngine.Random.Range(0, 2) == 0)
{
this.GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
this.GetComponent<SpriteRenderer>().color = Color.green;
}
try to use countries[i]:
if (UnityEngine.Random.Range(0, 2) == 0)
{
countries[i].GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
countries[i].GetComponent<SpriteRenderer>().color = Color.green;
}
I am making a card guessing game.there are 100 cards place in 10rows and 10 columns each card with a number and user have to find a number he is thinking of. i want to devise an algorithm to determine whether a given number is written on one of the cards by turning up less than 20 cards.I hav created the buttons dynamically, now im having hard time making a logic to search through them.This is my code.
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
int rememberlast = 0, move = 0;
object savelastobject;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int sizee = 50, where = 0;
this.Height = 0;
this.Width = 0;
var generatedNum = new List<int>();
var random = new Random();
while (generatedNum.Count < 100)
{
var tempo = random.Next(0, 100);
if (generatedNum.Contains(tempo)) continue;
generatedNum.Add(tempo);
}
char[] text2 = text.ToCharArray();
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
Button ta = new Button();
ta.Name = x.ToString()+y.ToString();
ta.Width = sizee;
ta.Height = sizee;
ta.Tag = generatedNum[where];
where++;
ta.BackColor = Color.Red;
ta.Location = new Point(70 * y, 70 * x);
Controls.Add(ta);
ta.Click += new System.EventHandler(this.button_Click);
this.Width += 90 * x / 13;
this.Height += 90 * x / 8;
}
}
}
private void button_Click(object sender, EventArgs e)
{
Control me = (Control)sender;
rememberlast = int.Parse(me.Tag.ToString());
savelastobject = me;
me.Text = me.Tag.ToString();
me.Enabled = true;
me.BackColor = Color.Gray;
move++;
label2.Text = move.ToString();
me.Enabled = true;
me.Text = me.Tag.ToString();
me.BackColor = Color.Gray;
me.Refresh();
Thread.Sleep(1000);
if (move == 20)
{
MessageBox.Show("Total Moves Consumed");
Application.Restart();
}
if (me.Tag.ToString() == textBox1.Text)
{
//Control him = (Control)savelastobject;
//him.BackColor = Color.LightBlue;
me.BackColor = Color.LightBlue;
MessageBox.Show("You Win");
Application.Restart();
}
}
}
}
How can i show gridview cell value one by one after ping the single ip address and then ping the other ip and show its status in gridview cell and then go to third and so on.
here is my code.
count = dgvStatus.Rows.Count;
for (int i = 0; i < count; i++)
{
string Name = dgvStatus.Rows[i].Cells[0].Text;
string IP = dgvStatus.Rows[i].Cells[1].Text;
try
{
Ping ping = new Ping();
PingReply reply = ping.Send(IP, 5000);
if (reply.Status == IPStatus.Success)
{
result = "UP";
Label status = (Label)dgvStatus.Rows[i].FindControl("lblstatus");
status.Text = result;
Panel panel = (Panel)dgvStatus.Rows[i].FindControl("Panel1");
panel.BackColor = Color.Green;
}
else
{
result = "DOWN";
Label status = (Label)dgvStatus.Rows[i].FindControl("lblstatus");
status.Text = result;
Panel panel = (Panel)dgvStatus.Rows[i].FindControl("Panel1");
panel.BackColor = Color.Red;
}
}
catch (PingException)
{
result = "UNREACHABLE";
Label status = (Label)dgvStatus.Rows[i].FindControl("lblstatus");
status.Text = result;
Panel panel = (Panel)dgvStatus.Rows[i].FindControl("Panel1");
panel.BackColor = Color.Red;
}`