I want to have multiple cards with text and images or just red or blue color filled up. this is how the form should look like. So I am thinking about creating multiple picture boxes or labels But the problem is that the picture boxes are overlapping.
private void button1_Click(object sender, EventArgs e)
{
Createlabels();
}
private void Createlabels()
{
var n = 5;
PictureBox[] p = new PictureBox[n];
for (int i = 0; i < n; i++)
{
p[i] = new PictureBox();
p[i].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\red1.Png");
p[i].SizeMode = PictureBoxSizeMode.Zoom;
p[i].Left = i * 100;
this.Controls.Add(p[i]);
}
}
private void button2_Click(object sender, EventArgs e)
{
Createlabels2();
}
private void Createlabels2()
{
var n = 10;
PictureBox[] q = new PictureBox[n];
for (int j = 0; j < n; j++)
{
q[j] = new PictureBox();
q[j].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\blue.Png");
q[j].SizeMode = PictureBoxSizeMode.Zoom;
q[j].Left = j * 100;
this.Controls.Add(q[j]);
}
}
}
This is the code I did so far my output but it should look like this one
private void button1_Click(object sender, EventArgs e)
{
var n = 12;
Button[] p = new Button[n];
for (int i = 0; i < n; i++)
{
p[i] = new Button();
p[i].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\red1.Png");
p[i].Text = i + " Reserved";
p[i].Left = i * 100;
this.Controls.Add(p[i])
}
}
private void button2_Click(object sender, EventArgs e)
{
Createlabels2();
}
private void Createlabels2()
{
var n = 12;
Button[] q = new Button[n];
for (int j = 0; j < n; j++)
{
q[j] = new Button();
q[j].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\blue.Png");
//"ImagePanel" is a TableLayoutPanel
q[j].Text = j + 10 + " Booked";
q[j].Top = j * 100;
this.Controls.Add(q[j]);
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I am trying to make typing game for my assignment and I stumbled upon a problem. How do I increase the game speed by 5 every 100 points. I noticed that using timer1.Interval -=5; is wrong, so how do I do it right?
namespace project
{
public partial class Form1 : Form
{
int points=0;
Label[] L;
Random r = new Random();
const int N = 3;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
for (int i = 0; i < N; i++)
{
if (L[i].Text == Convert.ToString(e.KeyCode))
{
L[i].Top = 0;
L[i].Left = r.Next(0, panel1.Width - L[i].Width);
L[i].Text = Convert.ToString((char)r.Next(65, 90));
points += 10;
label4.Text = "Points " + points;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
L = new Label[N];
L[0] = label1;
L[1] = label2;
L[2] = label3;
for (int i = 0; i < N; i++)
{
L[i].AutoSize = false;
L[i].BorderStyle = BorderStyle.FixedSingle;
L[i].TextAlign = ContentAlignment.MiddleCenter;
L[i].Width = 25;
L[i].Height = 25;
L[i].Top = 0;
L[i].Left = r.Next(0, panel1.Width - L[i].Width);
L[i].Text = Convert.ToString((char)r.Next(65, 90));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < N; i++)
{
L[i].Top += 2;
if (L[i].Top + L[i].Height >= panel1.Height)
{
L[i].Top = 0;
L[i].Left = r.Next(0, panel1.Width - L[i].Width);
L[i].Text = Convert.ToString((char)r.Next(65, 90));
points -= 5;
label4.Text = "Points " + points;
}
if (points % 100==0)
{
timer1.Interval -=5;
}
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
}
if(points % 100 == 0)
timer1.Interval -= 5;
I am new in C# and trying to create zedgraph of lessor sensor.
First I create a global variable and write code for graph. My graph is working but after reached to the point of 100 of x axis it will overlap to old line. z1.GraphPane.CurveList.Clear(); command is not working. I tried listPointsOne.clear(); command also but that clear the line and doesn't show anything on graph. Please help me out with this.
My code is below :
string DatafromCOM;
double[] x = new double[100];
double[] y = new double[100];
int i;
PointPairList listPointsOne = new PointPairList();
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
while (serialPort1.BytesToRead > 0)
{
DatafromCOM = serialPort1.ReadLine();
double iData;
var ok = double.TryParse(txtKQ.Text, out iData);
if (DatafromCOM.Trim() != "" && ok)
{
i= (i + 1) % 100;
x[i] = i;
y[i] = iData;
listPointsOne.Add(i,iData);
}
}
}
catch { }
}
private void timer1_Tick(object sender, EventArgs e)
{
z1.GraphPane.CurveList.Clear();
z1.GraphPane.AddCurve(null, listPointsOne, Color.Red, SymbolType.None);
z1.AxisChange();
z1.Invalidate();
}
You should clear the curvlist
string DatafromCOM;
double[] x = new double[100];
double[] y = new double[100];
int i;
PointPairList listPointsOne = new PointPairList();
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
while (serialPort1.BytesToRead > 0)
{
DatafromCOM = serialPort1.ReadLine();
double iData;
var ok = double.TryParse(txtKQ.Text, out iData);
if (DatafromCOM.Trim() != "" && ok)
{
i= (i + 1) % 100;
x[i] = i;
y[i] = iData;
listPointsOne.Add(i,iData);
}
z1.GraphPane.CurveList.Clear(); // Change here
}
}
catch { }
}
private void timer1_Tick(object sender, EventArgs e)
{
z1.GraphPane.AddCurve(null, listPointsOne, Color.Red, SymbolType.None);
z1.AxisChange();
z1.Invalidate();
}
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`
I'm using a for-loop to add values into an array of PictureBox and binding a click event to each one. I'm looking for a way of getting the data of a PictureBox after clicking on it. Since it's an array, I was thinking of sending the value of the loop counter, which would identify which one was clicked.
My code looks like this:
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].ImageLocation = #FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
label1.Text = "here I need the value of the picboxes[i] image location";
}
It can seem stupid, but I thought of something like:
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click(i))
and
private void PictureBoxes_Click(object sender, EventArgs e, int i)
In short: when I click in a PictureBox created in a array via code, how do I get its values (inside the click event handler)?
EDIT!
Sorry for finding it only after making this question, but I've found this solution and it may apply to my case, right?
try do do this
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].Name = (i+1).ToString();
picboxes[i].ImageLocation = #FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
string j = p.Name;
label1.Text = j;
}
You can use the following (anoynomous method) lambda expression
picboxes[i].Click += (sender, eventArguments) => PictureBoxes_Click(sender, eventArguments, i);
Use Tag
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].Tag = (i+1).ToString();
picboxes[i].ImageLocation = #FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
string j = p.tag.tostring();
label1.Text = j;
}
The code below display the index of each button when clicked the buttons are in an array but all this is done in form load. But I want to do the same thing in the click event and not in the form load how would I go about on doing this:
Image:
Code:
namespace _2DArray
{
public partial class Form1 : Form
{
private Button[,] b;
public Form1()
{
InitializeComponent();
b = new Button[2, 2];
b = new Button[,] { {button1,button2 },
{button3, button4}};
}
public int x = 0;
public int y = 0;
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button bt in b)
{
bt.Click += new System.EventHandler(this.ClickedButton);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
b[i, j].Click += new System.EventHandler(this.ClickedButton);
b[i, j].Name = "X: " + i + " " + "Y: " + j;
}
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button: " + s.Name);
}
}
}
Answered like so:
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
int x = int.Parse(s.Name.Split()[1]);
int y = int.Parse(s.Name.Split()[3]);
MessageBox.Show("you have clicked button: " + x +" "+ y);
}