I'm trying to make a basic memory game for now in C#, I'm using sender as PictureBox to determine min which picture box is selected.After that I have to check if the tags are equal, and here im2 and im1 loses its address. How can I save the addresses so they don't get loss?
public partial class Form1 : Form
{
int k = 1;
PictureBox im1, im2;
int r1, r2;
public Form1()
{
InitializeComponent();
}
private void Click(object sender, EventArgs e)
{
if (k == 1)
{
PictureBox im1 = sender as PictureBox;`enter code here`
r1 = Convert.ToInt16(im1.Tag);
string s = "slike\\sl";
s = s + r1.ToString() + ".jpg";
Image i = Image.FromFile(#s);
im1.Image = i;
k = 2;
}
else
{
PictureBox im2 = sender as PictureBox;
r2 = Convert.ToInt16(im2.Tag);
string s = "slike\\sl";
s = s + r2.ToString() + ".jpg";
Image i = Image.FromFile(#s);
im2.Image = i;
k = 0;
}
if(k==0) {
if (r1 == r2)
{
Image i = Image.FromFile(#"slike\\pogodjeno.jpg");
im1.Image = i;
im2.Image = i;
im1.Enabled = false;
im2.Enabled = false;
k = 1;
}
else
{
Image i = Image.FromFile(#"slike\\pozadina.jpg");
im1.Image = i;
im2.Image = i;
r1 = 0;
r2 = 0;
k = 1;
}
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
This doesn't seem right:
PictureBox im1, im2;
private void Click(object sender, EventArgs e)
{
PictureBox im1 = sender as PictureBox;`enter code here`
You probably want this:
private void Click(object sender, EventArgs e)
{
im1 = sender as PictureBox;`enter code here`
Related
I try to make my software display a green or red square depending on the ip address whether it is valid or not, of course all its in a FlowLayoutPanel (ca_imp) and before I create a square number based on the number of addresses I'll add in a second FlowLayoutPanel(listeImprimantes).
private void RdFichierXml()
{
int i = 0;
XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object
xmlDoc.Load("imprimante.xml"); // Load the XML document from the specified file
// Get elements
girlNom = xmlDoc.GetElementsByTagName("nom");
girlIp = xmlDoc.GetElementsByTagName("ip");
girlRemarques = xmlDoc.GetElementsByTagName("remarques");
// Display the results
for (i = 0; i < girlIp.Count; i++)
{
buttons(girlIp[i].InnerText, girlNom[i].InnerText, girlRemarques[i].InnerText);
buttons1(i, girlIp[i].InnerText);
}
}
private void buttons(string ip, string name, string remarque)
{
Panel Case = new Panel();
Case.Font = new System.Drawing.Font("Mont", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Case.Name = "Case";
Case.Size = new System.Drawing.Size(234, 49);
Case.Text = name + "\r\nIP : " + ip + "\r\nREMARQUE : " + remarque + "\r\n"; ;
Case.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
listeImprimantes.Controls.Add(Case);
}
private void buttons1(int i, string ip)
{
Panel Case1 = new Panel();
Case1.Name = "Case_color" + i;
Case1.Size = new System.Drawing.Size(16, 49);
Case1.TabIndex = i;
Case1.BringToFront();
Case1.BackColor = Color.Gray;
ca_imp.Controls.Add(Case1);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < girlIp.Count; i++)
{
/*if (check(girlIp[i].InnerText) == 1) //check is a funtion that return 1 if the ip is valid or 0 if not
{
?.BackColor = Color.Green;
}
else if (check(girlIp[i].InnerText) == 0)
{
?.BackColor = Color.Red;
}*/
}
}
Can i get some help for change the color with the backgroundWorker or other without freeze my window please...
There are many ways to do this, here's one:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < girlIp.Count; i++)
{
int result = check(girlIp[i].InnerText);
String cltName = "Case_color" + i;
Panel pnl = this.Controls.Find(ctlName, true).FirstOrDefault() as Panel;
if (pnl != null)
{
pnl.Invoke((MethodInvoker)delegate () {
pnl.BackColor = (result == 1) ? Color.Green : Color.Red;
});
}
}
}
private void button1_Click(object sender, EventArgs e)
{
PictureBox dynamicPicture1 = new PictureBox();
dynamicPicture1.Tag = i;
dynamicPicture1.Location = new Point(x, y);
dynamicPicture1.Name = "pictureBox" + i;
dynamicPicture1.Size = new System.Drawing.Size(30, 27);
dynamicPicture1.ImageLocation =
"C:\\Users\\Newfolder\\Downloads\\x`enter code here`ryrvc.jpg";
panel1.Controls.Add(dynamicPicture1);
}
Try this updated code.
private void button1_Click(object sender, EventArgs e)
{
int s = 4;
int x = 0;
int y = 0;
for (int i = 0; i < s; i++)
{
if (i == 0)
{
x = 38;
y = 60;
}
else
{
y += 50;
}
PictureBox dynamicPicture1 = new PictureBox();
dynamicPicture1.Tag = i;
dynamicPicture1.Location = new Point(x, y);
dynamicPicture1.Name = "pictureBox" + i;
dynamicPicture1.Size = new System.Drawing.Size(30, 27);
dynamicPicture1.ImageLocation = #"C:\Users\nxa00960\Downloads\abc.jpg";
panel1.Controls.Add(dynamicPicture1);
dynamicPicture1.Click += dynamicPicture1_Click;
}
}
void dynamicPicture1_Click(object sender, EventArgs e)
{
var pictureBox = sender as PictureBox;
switch (pictureBox.Name)
{
case "pictureBox0":
//do something
break;
case "pictureBox1":
//do something
break;
case "pictureBox2":
//do something
break;
case "pictureBox3":
//do something
break;
default:
break;
}
}
You should put the Method name of your event handler:
dynamicPicture1.Click += dynamicPicture1_Click; //note the name here
And define the event handler somewhere:
void dynamicPicture1_Click(object sender, EventArgs e) {
throw new NotImplementedException(); //default not implemented
}
The name of the event handler must match each other...
I created 5 PictureBox "Shapes" and I want them to move to the left automatically when the program is launched. So in the timer1_Tick method, I use "Shapes[i].Left -= 2", it's said that "Shapes" isn't in the actual context, so How can I make the Shapes[i] global from the "CreatePipes" method?
public partial class Form1 : Form
{
int i = 0;
int N = 5;
int yspeed;
int gravity = 2;
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
yspeed = -15;
}
}
private void Form1_Load(object sender, EventArgs e)
{
CreatePipes(1);
}
public void CreatePipes(object Number)
{
PictureBox[] Shapes = new PictureBox[N];
for (i = 0; i < N; i++)
{
Shapes[i] = new PictureBox();
Shapes[i].Name = "ItemNum_" + i.ToString();
Shapes[i].Location = new Point(300 + 120 * i, 250);
Shapes[i].Size = new Size(30, 1000 );
Shapes[i].BackColor = Color.Green;
Shapes[i].Visible = true;
this.Controls.Add(Shapes[i]);
}
}
private void bird_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
for (i = 0; i < N; i++)
{
Shapes[i].Left -= 2; //So the problem is here. Shapes[i] isn't in the actual context. But I don't know to to make it global from CreatePipes
}
yspeed += gravity;
bird.Top += yspeed;
}
}
}
You have to declare PictureBox[] Shapes above CreatePipes function, in Form1 class. Then in CreatePipes func, change PictureBox[] Shapes = new PictureBox[N]; to Shapes = new PictureBox[N];
In Form1 I have in the designer a pictureBox.
Then I added a timer in the tick event i did:
private void timer1_Tick(object sender, EventArgs e) {
if (savedall == true) {
for (int i = 0; i < filePaths.Length; i++) {
}
}
}
And the hscrollbar scroll event:
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) {
}
When the savedall is true then I want to be able to scroll between all the images in filePaths and display each image in the pictureBox.
You can prepare the list of images and a scrollbar imgScroller like this:
List<string> imageList = new List<string>();
imageList = Directory.GetFiles("d:\\", "*.jpg").ToList();
imgScroller.Minimum = 0;
imgScroller.Value = 0;
imgScroller.Maximum = imageList.Count - 1;
imgScroller.SmallChange = 1;
imgScroller.LargeChange = 1;
And the code the Scroll event like this:
private void imgScroller_Scroll(object sender, ScrollEventArgs e)
{
if (scrolledPB.Image != null) scrolledPB.Image.Dispose();
int index = (int)imgScroller.Value;
scrolledPB.Image = Image.FromFile(imageList[index]);
someLabel.Text = "Displaying image " + index + " of " + imageList.Count - 1;
}
Not sure what the timer is doing or if you already have the file paths..
I am trying to make a simple app that grabs all *.jpg files from a folder, and displays them in PictureBox.
However i'm trying to make Prev/Next buttons to show images one by one. Here's my code for the Next button so far:
string path = #"..\..\Resources\Wallpapers\";
int count = 0;
private void Form1_Load(object sender, EventArgs e)
{
DisplayNextFile(count);
}
private void next_Click(object sender, EventArgs e)
{
DisplayNextFile(count);
}
private void DisplayNextFile(int c)
{
var rand = new Random();
var files = Directory.GetFiles(path, "*.jpg");
var images = new Image[files.Length];
for (int i = c; i < files.Length; ++i)
{
images[i] = Image.FromFile(files[i]);
picBoxMainPreview.Image = images[i];
break;
}
count++;
if (count == files.Length)
count = 0;
}
It works fine, but how can I do it for the Previous button?
Remove count++; from DisplayNextFile.
private void next_Click(object sender, EventArgs e)
{
count = (++count) % files.Length;
DisplayNextFile(count);
}
private void previous_Click(object sender, EventArgs e)
{
count = (count - 1) >= 0 ? count - 1 ? files.Length - 1;
DisplayNextFile(count);
}
Replace the for loop with:
picBoxMainPreview.Image = Image.FromFile(c);