I am trying to give a label as flashlight effect. I have created a function called flashlight. Here is some part of the code
public void Flashlight(Label lable)
{
Random rand = new Random();
int one = rand.Next(0, 255);
int two = rand.Next(0, 255);
int three = rand.Next(0, 255);
int four = rand.Next(0, 255);
lable.ForeColor = Color.FromArgb(one, two, three, four);
}
public Form1()
{
InitializeComponent();
pictureBox2.Size = new Size(82, 82);
pictureBox3.Size = new Size(82, 82);
pictureBox3.Enabled = false;
timer2.Tick += (sender, args) => timer2_Tick(sender, args, label1);
}
private void timer2_Tick(object sender, EventArgs e,Label l)
{
Flashlight(l);
}
Can anyone tell me what I am missing here ? this is the part of code where I am stuck in. I am getting error
CS0123 No overload for 'timer2_Tick' matches delegate 'EventHandler'
Search for timer2_Tick in somewhere else in your code, it should be inside Form1.Designer.cs and then delete it, that's because you've already declared it by double clicking on your timer. So you need to delete the following line after you've found it:
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
As a shorter way, to find this line, after you've rebuilt your project and then in the Error List window double click on error which says:
CS0123 No overload for 'timer2_Tick' matches delegate 'EventHandler'
And then delete that line.
Related
In UWP i would like to give my button a random location on my screen when it is clicked on.
My code looks like this:
private void DeclareHere()
{
Random randomGenerator = new Random();
int iRndX = randomGenerator.Next(1, 640);
int iRndY = randomGenerator.Next(1, 360);
btnRandom.Margin.Left.Equals(iRndX);
btnRandom.Margin.Top.Equals(iRndY);
}
private void btnRandom_Click(object sender, RoutedEventArgs e)
{
DeclareHere();
}
What is it that i'm missing? Would help if you could explain how you accomplished the solution to this.
Equals is just a comparison operator.
ex:
bool isEqual = btnRandom.Margin.Left.Equals(iRndY);
You need to assign margin values
So, you should change this:
btnRandom.Margin.Left.Equals(iRndX);
btnRandom.Margin.Top.Equals(iRndY);
with this:
btnRandom.Margin = new Thickness { Left = iRndX, Top = iRndY};
can someone tell me why I can't run my program? I'm trying to create doughts and crosses in windows form applications I tried changing the code and stuff but I've tried everything but I think something is wrong with my function at the bottom. By now, I wanted to program to run, generate 9 buttons and when I click on them an "X" or "O" would appear depending whose turn is it.
PS. I haven't added the win condition function yet I wanted to test if the program is working as it should.
Thanks in advance.
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 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(sender,index);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender,int i)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
gameButtons[i] = 'X';
}
else
{
((Button)sender).Text = "O";
gameButtons[i] = 'O';
}
cross = !cross;
}
}
}
Edit: The first problem was solved, thanks a lot to everyone :) But now I'm struggling to find the win condition. I've used this code but I'm getting a compiler error that I don't seem to understand how to fix it. This is the code I've made up:
private void threeInARow(int a, int b, int c, object sender)
{
if (gameButtons[a]==gameButtons[b] && gameButtons[a]==gameButtons[c])
{
if (gameButtons[a]='X')
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
The error is on my my first if it says "cannot implicitly convert type char to system.windows.forms.button"
Firstly, you have some compile errors:
// These two lines both throw the error:
// Cannot implicitly convert type 'char' to 'System.Windows.Forms.Button'
gameButtons[i] = 'X';
gameButtons[i] = 'O';
This is because you're trying to set a button to a character. This is not necessary, since you've already changed the text of the button, and you can remove these lines.
Next, you get a runtime exception when clicking on a button, on this line:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
This is because the sender is the Form1 object, and not the button. The reason for this is that, in your assignment of the event to the button Click, you are passing sender instead of sender1 to the event, and, since the assignment of this event is happening in the Form.Load event, sender is the Form1. So you need to change the assignment to pass sender1 instead:
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
The next problem you're having (since you've modified the code in the original question) is in the threeInARow method here:
if (gameButtons[a] = 'X') // Error: Cannot implicitly convert type 'char' to 'Button'
The reason for this is that gameButtons is an array of Button objects, so gameButtons[a] represents a Button, and you can't assign the character 'X' to a Button (they are two different types). Since you've already assigned a value to the Text property of each button (which is of string type), you can just use that instead.
Also, you are using a single = sign, which is an assignment. You want to do a comparison, which uses a double == sign. So, putting these together, you will get:
if (gameButtons[a].Text == "X")
You have added similar problematic code to your buttonHasBeenPressed method, which you should just remove since we can compare the Text properties and don't need this additional assignement:
gameButtons[i] = 'X'; // Remove these invalid assignments
That takes care of the compile error, but you still have another problem in your comparisons with this line:
if (gameButtons[a] == gameButtons[b] && . . .
This line is asking if the Button reference in the array at index a is pointing to the exact same object as the Button reference in the array at index b. This will never be the case because you (correctly) initialized your array with 9 unique buttons.
What you really want to do is compare the Text property of each button, like so:
if (gameButtons[a].Text == gameButtons[b].Text && . . .
Lastly, you have included an Object parameter named sender to your method that you aren't using, so you might as well remove that (or do something with it in your method).
So, putting this all together you have:
private void threeInARow(int a, int b, int c)
{
if (gameButtons[a].Text == gameButtons[b].Text && gameButtons[a].Text == gameButtons[c].Text)
{
if (gameButtons[a].Text == "X")
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
}
See code below :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 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 += new EventHandler(buttonHasBeenPressed);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender, EventArgs e)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
//gameButtons[i].Text = "X";
}
else
{
((Button)sender).Text = "O";
//gameButtons[i].Text = 'O';
}
cross = !cross;
}
}
}
}
My program creates 5 different labels with a cube form and they just drop down. When I press on them, they come invisible. I want to check if all of them are invisible, but don't know how to do so. Tried going through this site, found a solution with bool, but it just doesn't work my way. Also when my labels appear,you can see only 4 of them.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label [] kubeliai = new Label [5];
int poz = 100;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i] = new Label();
Controls.Add(kubeliai[i]);
Random pos = new Random();
kubeliai[i].Top = 50;
kubeliai[i].Left = poz;
poz += pos.Next(50, 200);
kubeliai[i].BackColor = Color.Red;
kubeliai[i].Height = 20;
kubeliai[i].Width = 20;
kubeliai[i].Click += new EventHandler(kubelio_clickas);
}
Timer kritimo_laikrodis = new Timer();
kritimo_laikrodis.Interval = 10;
kritimo_laikrodis.Tick += new EventHandler(laikrodis);
kritimo_laikrodis.Enabled = true;
}
void kubelio_clickas (object sender, EventArgs e)
{
((Label)sender).Visible = false;
}
void laikrodis (object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i].Top += 1;
if (kubeliai.All.Visible == false) // this is an error
{
kubeliai[i].Visible = true;
kubeliai[i].Top = 50;
Random pos = new Random();
poz += pos.Next(50, 200);
}
}
}
Using Linq you can check if all are invisible in this way
var areAllInvisible = kubeliai.All(l => l.Visible == false);
if (areAllInvisible)
{
// do something
}
when my labels appear you can see only 4 of them.
That's because the way you are picking random numbers is picking the same numbers each time and you are therefore placing your labels on top of each other. Read the first paragraph of the Random() documentation:
Different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers.
Use new Random() once in your class definition like this:
Label [] kubeliai = new Label [5];
Random pos = new Random();
And remove it everywhere else in your program.
I'm trying to change randomly the location of a button when the mouse is hover it. To do that, I'm using the following source code :
private int modifX()
{
int rdmx;
int x_max = this.Width;
Random rdm = new Random();
rdmx = rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
The problem is that my button's position is always on a straight line like that
How can I fix it? I tried to use bt_win.Location.X = modifX(); on the mouseEnter event But it seems that I can't handle Location.X or Location.Y
I don't really get what I'm doing wrong, anyone got an idea and could explain me what I'm doing wrong?
You need to use the same instance of the Random class.
When you create two instance of the Random class closely, they can share the same seed. So the same number will be generated.
private Random _rdm = new Random();
private int modifX()
{
int x_max = this.Width;
int rdmx = _rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
I'm currently trying to add a specific action to an array of labels but the variables are kept only between the {}.
_arr[i].Click += (a, b) => {
z++;
numarfinal = Convert.ToString(z);
MessageBox.Show(Convert.ToString(numarfinal));
};
Outside {} neither variable z nor numarfinal has changed but the MessageBox.Show reports that their parameters have changed.
Here is the whole function:
Label[] _arr = new Label[20];
Label[] _dinamic = new Label[20];
private static Random _r = new Random();
string numarfinal ;
private void button1_Click(object sender, EventArgs e)
{
int z=0;
int limita = Convert.ToInt16(textBox1.Text);
limita = int.Parse(textBox1.Text);
if (limita > 20)
textBox1.Text = "Do sth";
int randomnumber = _r.Next(20);
for(int i=0;i<limita;i++)
{
do
{
randomnumber = _r.Next(20);
} while (randomnumber==0);
_arr[i] = new Label();
_arr[i].Click += (a, b) =>
{
z++;
numarfinal= Convert.ToString(z);
MessageBox.Show(Convert.ToString(numarfinal));
};
_arr[i ].Text = Convert.ToString(randomnumber);
_arr[i ].Size = new Size(50,50);
_arr[i ].Location = new Point(55*i,60);
testlabel.Text = Convert.ToString(numarfinal); // the label value remain nothing (numarfinal's initial value)
this.Controls.Add(_arr[i]);
}
The problem is that you set testlabel.Text once, while the value of numarfinal is still null. When one of the labels is clicked and its event handler is executed, the value of numarfinal is changed, but the value of testlabel.Text isn't.
One way to fix that is to simply set testlabel.Text in the event handler lambda:
_arr[i].Click += (a, b) =>
{
z++;
numarfinal = Convert.ToString(z);
testlabel.Text = numarfinal;
MessageBox.Show(Convert.ToString(numarfinal));
};
The z value is being changed in the click events of the labels. If you want to see the change in your testlabel you should change its text "in the click events". If you want to change numarfinal every time you add a label you should put your code out of click events and if you want numarfinal change every time you click on a label your code should be were it already is.
Clicking button1 will never cause the line numarfinal = Convert.ToString(z); to be reached because your are only attaching the delegate there. When you assign the delegate with (a, b) => {...}, the code within the {} is not called until the respective event is actually raised (i.e., clicking the respective label).