I'm using this code in order to make a marquee label that scrolls from right to left.
private int xPos = 651;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Width == xPos)
{
//repeat marquee
xPos = 651;
this.Label7.Location = new System.Drawing.Point(651, 334);
xPos--;
}
else
{
this.Label7.Location = new System.Drawing.Point(xPos, 334);
xPos--;
}
}
651 is the width of the form.
This code makes the label go from right to left, scroll off the form like it should, but doesn't restart at the right again.
I'm pretty sure you might have seen this : using Label control to create looping marquee text in c# winform
Read the first answer.
I assume this.Width is a positive value, therefore if you are doing repeated subtraction, you'll only ever achieve this value at the beginning.
Try if (xPos == 0) instead
that code for me make no sense to me
try this instead, tweak it like you want.
private bool goLeft;
private void timer1_Tick(object sender, EventArgs e)
{
if (Label7.Width + Label7.Left >= this.Width)
{
goLeft = true;
}
else if (Label7.Left < 0)
{
goLeft = false;
}
Label7.Left += goLeft ? -10 : 10;
}
While I agree that it would be easier to just use the marquee from the client side, that wasn't what he was asking...
You are starting xPos at this.Width and decrementing it so, you need to reset xPos when it reaches 0 not this.Width. xPos is only equal to this.Width for the first iteration of each run.
private int xPos = this.Width;
private void timer1_Tick(object sender, EventArgs e) {
if (xPos == 0) { xPos = this.Width; }
this.Label7.Location = new System.Drawing.Point(xPos, 334);
xPos--;
}
Related
My question is very simple.
My Label1 should move continuously from left to right.
(Start from the left part of the form and go to the right part of the form. When it reaches the right part of the Form, go to the left and so on.) I have 2 timers(one for right and one for left)
I have the following code, it starts at the left and goes to the right, but when it reaches the right of the Form, it doesn't return.
Can anyone help me?
enum Position
{
Left,Right
}
System.Windows.Forms.Timer timerfirst = new System.Windows.Forms.Timer();
private int _x;
private int _y;
private Position _objPosition;
public Form1()
{
InitializeComponent();
if (label1.Location == new Point(0,180))
{
_objPosition = Position.Right;
}
if (label1.Location == new Point(690,0))
{
_objPosition = Position.Left;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Label1.SetBounds(_x, _y, 0, 180);
}
private void tmrMoving_Tick(object sender, EventArgs e)
{
tmrMoving.Start();
if (_objPosition == Position.Right && _x < 710)
{
_x +=10;
}
if(_x == 710)
{
tmrMoving.Stop();
}
Invalidate();
}
private void tmrMoving2_Tick(object sender, EventArgs e)
{
tmrMoving2.Start();
if (_objPosition == Position.Right && _x > 690)
{
_x -= 10;
}
if(_x == 1)
{
tmrMoving2.Stop();
}
Invalidate();
}
Thanks.
I think you've overcomplicated things. Let's just have one timer, a step number of pixels that we sometimes flip negative, and some time later flip back to positive. We can move the label by repeatedly setting its Left:
public partial class Form1 : Form
{
private int _step = 10;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Right > this.Width) _step = -10;
if (label1.Left < 0) _step = 10;
label1.Left += _step;
}
}
You might want to fine tune it, but the basic motion is there
I am trying to create a game in Visual studios using C# where you have rows of sliding boxes that you must press a key in certain locations one after the other, kinda like stacker, before the time runs out.
I am using this as the basis but unsure how to adjust the speed among making it work in the first place since the code apparently runs with no issues but I only got it where the button just gives you a message box.
https://www.youtube.com/watch?v=O78n7apXjG8
Just asking for tips on where to go.
https://files.catbox.moe/x3wx22.zip
additional info for clarification
https://catbox.moe/c/fakpxe
// Project by 124
// Redid:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private int _ticks;
private object textBox1;
public Form1()
{
InitializeComponent();
timer1.Start();
}
//This should be the part that moves the button back and forth but it's not working
int Left = 140;
private void timer1_tick(object sender, EventArgs e)
{
Left += 10;
if (Left > 430)
{
Left = -138;
Left += Left;
if (Left > 140 )
{
} else
{
button1.Left = Left;
}
}
else
button1.Left = Left;
{
button1.Visible = false;
button2.Visible = true;
}
{
_ticks++;
this.Text = _ticks.ToString();
if (_ticks == 9)
{
this.Text = "Game Over";
timer1.Stop();
}
}
}
private void button1_Click(object sender, EventArgs e)
//since the movement button function is not working so is this one where it's supposed to give you the win but the win is there regardless
{
if (_ticks < 9) ;
{
MessageBox.Show("good job you beat the time");
MessageBox.Show("You are a winner");
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Restart();
Environment.Exit(0);
}
private void button3_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
}
Adjust the speed among making it work:
Left += Convert.ToInt32(label1.Text);
Control speed +1:
private void Button4_Click(object sender, EventArgs e) {
label1.Text = (Convert.ToInt32(label1.Text) + 1).ToString();
}
Control speed -1:
private void Button5_Click(object sender, EventArgs e) {
label1.Text = (Convert.ToInt32(label1.Text) - 1).ToString();
}
Updated:
Write at the top:
You can control the speed and position of the button by modifying the values of'V0' and'a'.
//current position
Left += 20* _ticks+(a* _ticks* _ticks)/2;//s = v0·t + a·t²/2
Set the initial time value outside the timer
private double _ticks = 0;
Set the initial position to 0.
double Left = 0;
Increase by 1 every second. Because internal is 0.2, add 0.2 every time.
_ticks+=0.2;
Modified timer1_tick:
private void timer1_tick(object sender, EventArgs e) {
//Set acceleration
double a = 0.5;
//Increase by 1 every second. Because internal is 0.2, add 0.2 every time.
_ticks += 0.2;
this.Text = _ticks.ToString();
//Time to stop at 9s
if (_ticks == 9) {
this.Text = "Game Over";
timer1.Stop();
MessageBox.Show("Game Over");
button1.Visible = false;
button2.Visible = true;
}
//current position
Left += 20 * _ticks + (a * _ticks * _ticks) / 2;//s = v0·t + a·t²/2
//When the total distance exceeds the set width on the interface, perform operations such as subtraction until the current position is less than the set value.
rtn:
if (Left > 300) {
Left -= 300;
if (Left > 300) {
goto rtn;//Use goto: Perform an iterative operation.
} else {
button1.Left = Convert.ToInt32(Left);//Cast double data type to int
}
} else
button1.Left = Convert.ToInt32(Left);
}
Modified button_Click: Don’t add ‘;’ after ‘if’, it will be useless.
private void button1_Click(object sender, EventArgs e) {
if (_ticks < 9) //Don’t add ‘;’ after ‘if’, it will be useless.
{
timer1.Stop();
button1.Visible = false;
button2.Visible = true;
MessageBox.Show("good job you beat the time");
MessageBox.Show("You are a winner");
}
}
Output:
Im trying to get a smooth expanding and collapsing animation for my form. My current animation is really jittery and non consistent. Heres a Gif of the Animation. Is there another way to do this that doesnt freeze the form?
private void ShowHideToggle_CheckStateChanged(object sender, EventArgs e)
{
if (ShowHideToggle.Checked) //checked = expand form
{
ShowHideToggle.Text = "<";
while (Width < originalWidth)
{
Width++;
Application.DoEvents();
}
}
else
{
ShowHideToggle.Text = ">";
while(Width > 24)
{
Width--;
Application.DoEvents();
}
}
}
Create a Timer:
Timer t = new Timer();
t.Interval = 14;
t.Tick += delegate
{
if (ShowHideToggle.Checked)
{
if (this.Width > 30) // Set Form.MinimumSize to this otherwise the Timer will keep going, so it will permanently try to decrease the size.
this.Width -= 10;
else
t.Stop();
}
else
{
if (this.Width < 300)
this.Width += 10;
else
t.Stop();
}
};
And change your code to:
private void ShowHideToggle_CheckStateChanged(object sender, EventArgs e)
{
t.Start();
}
Ok so please keep answers very direct and i must say i am very new to C#, i don't know a lot of stuff. Without further adieu my problem.
I am trying to move a picture box horizontally across the screen on a timer.The timer must go infinitely. I have tried all i currently know in C# and searched around quite a lot but nothing answered my exact question which is what i need because of my lesser knowledge of C#. For the last two weeks i worked on graphics mostly and the rest of that was trying to get this to work, So i have no code in my game. This is because for anything to work i need this part to be working. My game is 2D topdown. Any and all help is appreciated.
Thank you for taking the time to read.
Edit
No more answers needed, Thank you Odrai for the answer, it helped me a lot.
Use pictureBox.Location = new Point(x, y) or set pictureBox.Left/Top/Right. You can define x and y as variabels and initialize them with a default value. Increment x on timer tick.
Sample 1:
public partial class Form1 : Form
{
private Random _random
public Form1()
{
InitializeComponent();
_random = new Random();
}
private void timer1_Tick(object sender, EventArgs e)
{
int x = _random.Next(0, 500);
int y = _random.Next(0, 500);
pictureBox1.Top += y;
pictureBox1.Left += x;
}
}
Sample 2:
private void timer1_Tick(object sender, EventArgs e)
{
this.SuspendLayout();
pictureBox.Location = new Point(picust.Location.X + 10, picust.Location.Y);
this.ResumeLayout();
}
Add two buttons with title LEFT and RIGHT to a form and write the following code.
It might give you an idea, how to do simple moving animations.
public partial class Form1 : Form
{
int difference = 0;
Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 15;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
pictureBox1.Left += difference;
}
private void btnLeft_Click(object sender, EventArgs e)
{
difference = -2;
}
private void btnRight_Click(object sender, EventArgs e)
{
difference = 2;
}
}
Try This Code it will work :
private void timer1_Tick(object sender, EventArgs e)
{
int width = this.Width; // get the width of Form.
if(pictureBox1.Location.X > width - pictureBox1.Width) //to check condition if pic box is touch the boundroy of form width
{
pictureBox1.Location = new Point(1, pictureBox1.Location.Y); // pic box is set to the new point. here 1 is indicate of X coordinate.
}
else
{
pictureBox1.Location = new Point(pictureBox1.Location.X + 100, pictureBox1.Location.Y); // to move picture box from x coordinate by 100 Point.
}
}
//Try This //
picturebox1.Location = 0,0;
I have been create Marquee text using Label control her is sample code
public partial class FrmMarqueeText : Form
{
private int xPos = 0, YPos = 0;
public FrmMarqueeText()
{
InitializeComponent();
}
private void FrmMarqueeText_Load(object sender, EventArgs e)
{
lblText.Text = "Hello this is marquee text";
xPos = lblText.Location.X;
YPos = lblText.Location.Y;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (xPos == 0)
{
this.lblText.Location = new System.Drawing.Point(this.Width, YPos);
xPos = this.Width;
}
else
{
this.lblText.Location = new System.Drawing.Point(xPos, YPos);
xPos -= 2;
}
}
but when the first time was finished, it didn't continues work .Please help me!
In timer1_Tick change
if (xPos == 0)
to
if (xPos <= 0)
Otherwise it won't work if this.Width is odd.
I think you need to check if xPos <= 0. Because if xPos = 1 after xPos -= 2 your xPos will be equal to -1