C# button animation - c#

i have a simple two button:
button1
button2
i want when click on button1 the button2 start a move to right,
i write this code:
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 100;
}
private void timer1_Tick(object sender, EventArgs e)
{
button1.Left += 20;
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
//timer1.Interval = 0;
}
i want when the button exit on my form border on right side,timer has stop the animation.

I think this is what you looking for:
private void button2_Click(object sender, EventArgs e)
{
timer1.Interval = 100;
//start the timer
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
button1.Left += 20;
//check position of button. When it is outside the width of form stop the timer.
if(button1.Left >= this.Width)
{
timer1.Stop();
}
}

Related

How to run two media players synchronously on a form?

I want to run two media players in one form. The same video will be played on both media players and will be played at the same time. There should be no seconds difference between the two media players. When I press the "play" button, both media players should play at the same time.
When the "play" button is pressed, both media players start to play the same video. But there is a time difference of almost one second between the two videos. How can I remove this time difference? Please help.
My code ;
public Form1()
{
InitializeComponent();
}
private void LoadButton_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
axWindowsMediaPlayer2.URL = openFileDialog1.FileName;
}
}
private void PlayButton_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer2.Ctlcontrols.play();
}
private void Form1_Load(object sender, EventArgs e)
{
t = new Timer();
t.Interval = 100;
t.Tick += new EventHandler(t_Tick);
axWindowsMediaPlayer1.OpenStateChange += new AxWMPLib._WMPOCXEvents_OpenStateChangeEventHandler(axWindowsMediaPlayer1_OpenStateChange);
}
void t_Tick(object sender, EventArgs e)
{
trackBar1.Value = (int)this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
private void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
{
if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
{
trackBar1.Value = 0;
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
t.Start();
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = trackBar1.Value;
axWindowsMediaPlayer2.Ctlcontrols.currentPosition = trackBar1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}

so I was trying to make a code that changes the writings font(arial,times new roman etc.) in the textbox every 750 miliseconds but I couldn't do it

private void OnTimer(object sender, EventArgs e)
{
int i = 0;
i += 1;
}
I tried to make the timer
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
FontFamily[] fontlar = FontFamily.Families;
fontlar.GetUpperBound(i);
Timer timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
I put the code in form1 but I tried it in textbox as well it didn't work
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
button
I guess you are new to this.
Your Timer does nothing
Your Timer is local, it should be public
You didn't create your Textbox
I think this would be your answer:
Timer timer1;
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
private void OnTimer(object sender, EventArgs e)
{
i += 1;
FontFamily[] fontlar = FontFamily.Families;
//fontlar.GetUpperBound(i);
textBox1.Font = new Font(fontlar[i], 16.0f);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
Don't forget to create a textbox on your form.

what is the way to close the form and open it again

i try to close the from and open it again with this code bout it didn't close the form i found it in the background and open another one for it
private void Graph_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 60000;//1 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
this.Close();
Graph1 graph = new Graph1();
graph.Show();
}
start refresh is what i looking for
All you have to do is to change RefreshMyForm() to Refresh(); and clear function RefreshMyForm().
private void Graph_Load(object sender, EventArgs e)
{
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
label1.Text = DateTime.Now.ToString("HH:mm:ss");
timer1.Interval = 60000;//1 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
timer1.Start();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString("HH:mm:ss");
Refresh(); // OR Invalidate(); OR Update();
}
Here label1 is simple watch to see how form refreshed every minute

How to run a progress bar on a timer - c#

I want to run a progress bar on a form through the use of a timer.
I have tried multiple ways and have not been able to get it to work.
I hope someone here can help me with this.
private void SplashScreen_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 1000;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
you are assigning event_handler like
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
and you are changing the progressBar value in
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
change event handler to
splashScreenTimer.Tick += new EventHandler(timer_Tick);
or move codes to the other event handler timer1_Tick which should be in your form
For running the progressBar full in 4 seconds you can do like this
private void Form1_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 30;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer_Tick);
}
int waitingTime = 0;
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value < 100)
{
progressBar.Value++;
}
else
{
if (waitingTime++ > 35)
this.Close();
}
}

c# timer not stopping

Timer starts with button1;
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1;
timer1.Enabled = true;
}
Timer function should work until i click STOP button
private void timer1_Tick(object sender, EventArgs e)
{
Keyboard.KeyDown(Keys.D9);
Thread.Sleep(1);
Keyboard.KeyUp(Keys.D9);
Thread.Sleep(1);
Keyboard.KeyDown(Keys.D1);
Thread.Sleep(1);
Keyboard.KeyUp(Keys.D1);
}
Button2 is "stop" button but timer is not stopping until i close application , sometimes is stopping if i click more than five or ten but sometimes it never stops
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
MessageBox.Show("timer stop");
}

Categories