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;
Related
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]);
}
}
}
I have a button to start the Timer1, the Timer1 will print the current execution time every 100ms, but when I do something calculation, the timer1_Tick() will be paused until finished that calculation, I think the calculation bother the Timer1 so that timer1_Tick() is dropped(or blocked this thread).
In fact, the calculation is very complex, maybe take 40 seconds, I just need to show the execution time every 100ms to tell user how close to the end of this function, would you please tell me how to do this work??
double a = 0;
public Form1()
{
InitializeComponent();
timer1.Interval = 100;
}
private void timer1_Tick(object sender, EventArgs e)
{
a += 0.1;
label1.Text = a.ToString();
}
private void UserStartTimer_Click(object sender, EventArgs e)
{
a = 0.0;
timer1.Enabled = true;
}
private void UserCalcSomething_Click(object sender, EventArgs e)
{
double s = 0;
for (int i = 0; i < 100000; i++)
{
for (int j = 0; j < 10000; j++)
{
s = i + j;
}
}
}
private void UserStopTimer_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
Just execute the calculation in another thread or task:
private void UserCalcSomething_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => {
double s = 0;
for (int i = 0; i < 100000; i++)
{
for (int j = 0; j < 10000; j++)
{
s = i + j;
}
}
}
}
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 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];
I have a small program that hold 4 button in a 2D Array what I want to do is display its 'X' and 'Y' coordinates of the Array in a message box (when clicked)
I have tried a number of ways some don't work and some work but I cant get it to show the 'X' and 'Y' values
The image below shows what I have so far:
And This is the code i have come up with:
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}};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button bt in b)
{
bt.Click += new System.EventHandler(this.ClickedButton);
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s);
}
}
}
Here is the answer to your question if i read it right. You are trying to get the X and Y coordinates of the button right?
Here is the code for a button click:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(button1.Location.ToString());
}
try assigning some sort of pointer like give name of the button to keep track of it coordinates
private void Form1_Load(object sender, EventArgs e)
{
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 =i+" "+j;
}
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s.Name);
}
Use this code
private void Form1_Load(object sender, EventArgs e) {
for (int x = 0; x < 2; x++) {
for (int y = 0; x < 2; y++) {
b[x, y].Tag = new Point(x, y);
b[x, y].Click += new System.EventHandler(this.ClickedButton);
}
}
}
private void ClickedButton(object sender, EventArgs e) {
Button s = (Button) sender;
MessageBox.Show("you have clicked button:" + s.Tag.ToString());
}
then clicking on button1 will show the message "you have clicked button:{X = 0, Y = 0}" etc
Tag is a property that each control has, it's description is "User-defined data associated with the object" so you can set it to whatever object you like.
I know this is probably a bit late for the op but hopefully it will help someone else.