My Timer doesn't work as expected, why is the pictureBox moving faster after each call of the Timer interval.
I want the pictureBox to move frequent over the Form after someone pressed the D button. The functions add and remove Event works fine so that can't be the case:
Timer loop;
Int32 posX, posY;
public Form1()
{
InitializeComponent();
loop = new Timer();
}
private void Form1_Load(object sender, EventArgs e)
{
sprit.Enabled = false;
loop.Interval = 10;
posX = this.sprit.Location.X;
posY = this.sprit.Location.Y;
}
private void nextStep(object sender, EventArgs e)
{
posX++;
this.sprit.Location = new Point(posX, posY);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode) {
case Keys.D:
addEvent(nextStep);
loop.Start();
break;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.D:
removeEvent(nextStep);
loop.Stop();
break;
}
}
Form1_KeyDown will be called repeatedly as long as you hold the key, so you keep calling addEvent(nextStep).
Try calling addEvent(nextStep) only once on Form1_KeyDown
Related
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)
{
}
}
}
Is there any way to detect if ListView or CollectionView is scrolling by user and not from ScrollTo method?
I am using ScrollTo as the example below:
colViewCategories.ScrollTo(categoryItem, null, ScrollToPosition.Center, true);
Or if i can disable Scrolled Event till ScrollTo Method will stop to scroll.
I finally found my answer.
bool scrollAnimationIsRaised=false;
int previousindex = 0;
private System.Timers.Timer tmr = new System.Timers.Timer();
private void InitTimer
{
tmr.Interval = 500; //waiting 500ms after scrollTo has Stop for not having conflicts
tmr.Elapsed += Tmr_Elapsed;
}
private void ButtonClick(object sender, EventArgs e)
{
scrollAnimationIsRaised = true;
listview.ScrollTo(50);
}
private void ListviewItems_Scrolled(object sender, ItemsViewScrolledEventArgs e)
{
if(scrollAnimationIsRaised)
{
//ScrollToEvent Is Fired
if (e.LastVisibleItemIndex == previousindex)
tmr.Start();
previousindex = e.LastVisibleItemIndex;
}
else
{
//ScrollTo event has finished
}
}
private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
tmr.Stop();
scrollAnimationIsRaised = false;
}
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();
}
}
Code:
private void sprites_updater_Tick(object sender, EventArgs e)
{
s++;
int x = player.Location.X;
int y = player.Location.Y;
if (s == 1)
if (ModifierKeys.HasFlag(Keys.A))
{
player.Location = new Point(x - 5, y);
}
s = 0;
sprites_updater.Start();
}
So while using timer code, I wrote the same thing above (ModifierKeys.HasFlag(Keys.A)) but it didnĀ“t work. Why?!
BTW, is there any way to show a 3d camera perspective inside a WinForms Panel WITHOUT USING XNA, WPF or any other stuff (only .NET)?!
The best way to get keyboard strokes and processing them later is to catch the Keyboards events in the form using both KeyDown, KeyUp and flags:
bool isAPressed;
...
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Key.A:
isAPressed = true;
break;
case Key.XXXX:
...
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Key.A:
isAPressed = false;
break;
case Key.XXXX:
...
}
}
Then you can use this information in your timer :
private void sprites_updater_Tick(object sender, EventArgs e)
{
s++;
int x = player.Location.X;
int y = player.Location.Y;
if (s == 1)
if (isAPressed)
{
player.Location = new Point(x - 5, y);
}
s = 0;
sprites_updater.Start();
}
This is particularily interresting to handle player moves this way (arrows).
I want to change the radiobutton location and make it move up while i am clicking button
tried this
private void up_MouseDown(object sender, MouseEventArgs e)
{
while(P.Location.Y>0)
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
P is a radiobutton
I want it to keep moving up while I'm pressing, but it's just jumping up to the up of the form.
it's working good in debugging but it's really moving fast
I want to slow the movement of the radiobutton and make it visible
Actually you are starting a while loop that will not exit until your RadioButton is at the top of your Form wether you are still pressing the Button or not. You can slow it down by putting a Thread.Sleep in your loop that way it is slowed down visible.
private void up_MouseDown(object sender, MouseEventArgs e)
{
while (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
System.Threading.Thread.Sleep(10);
}
}
If you want to have better control I would use a Timer. In this example the Interval is set to 10.
private void up_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
private void up_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
}
You can use timer. Add a timer from the toolbox, say its name was timer1, then add following method:
private void P_MouseUp(object sender, MouseEventArgs e) {
timer1.Enabled=false;
}
private void P_MouseDown(object sender, MouseEventArgs e) {
timer1.Enabled=true;
}
private void timer1_Tick(object sender, EventArgs e) {
if(P.Location.Y>0)
P.Location=new System.Drawing.Point(P.Location.X, P.Location.Y-1);
}
You can change the interval of timer1 in properties window. I guess you write this for fun; so, have fun!