i want to play automatically next song in my player. I have use windows media player object.
this is my code.
private void timer2_Tick(object sender, EventArgs e)
{
if (songList.SelectedIndex < files.Length - 1)
{
songList.SelectedIndex++;
timer2.Enabled = false;
}
else
{
songList.SelectedIndex = 0;
timer2.Enabled = false;
}
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
timer2.Interval = 100;
timer2.Enabled = true;
}
}
but it's not working i have chacked timer code it is working but i think axWindowsMediaPlayer1_PlayStateChange event not working and in the designer code when i wrote this line for axWindowsMediaPlayer
this.axWindowsMediaPlayer1.PlayStateChange += new System.EventHandler(this.axWindowsMediaPlayer1_PlayStateChange);
it shows this error:
No overload for axWindowsMediaPlayer1_PlayStateChange matches delegate System.EventHandler
is there any solution?
Take a look at the documentation for PlayStateChange. It gives you a clear example.
Rather than using what you have, simply use this event assignment instead:
axWindowsMediaPlayer1.PlayStateChange +=
new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(axWindowsMediaPlayer1_PlayStateChange);
Related
I'm trying to develop an application to convert text to morse code and vice versa. I just managed to do the first phase completely which means when you type a character you will see the encoded type of that character.
but in the second phase I got some problem:
here is my code:(hint:sw=first stopwatch,flagsw=second stopwatch,datas=dataset,dbc=databaseconverter,listofcode=string of '.' and '-')
private void txtletters_KeyDown(object sender, KeyEventArgs e)
{
txtletters.BackColor = Color.Yellow;
sw.Start();
if (flagsw.ElapsedMilliseconds > 400)
{
datas = dbc.srchfortext(listofcode);
lbltext.DataBindings.Clear();
lbltext.DataBindings.Add("text", datas, "t.letter");
txtletters.Text += lbltext.Text;
listofcode = "";
}
flagsw.Reset();
}
private void txtletters_KeyUp(object sender, KeyEventArgs e)
{
txtletters.BackColor = Color.White;
sw.Stop();
if (sw.ElapsedMilliseconds < 250)
listofcode += ".";
else
listofcode += "-";
sw.Reset();
flagsw.Start();
}
I just managed to do the work somehow but as the code shown:
when you press any key first timer will start and first timer determine if it is . or -
when you release it second timer will start (with that timer I want to know if the string of '.','-' should be closed and send to database to return the specified character...the problem in here is that the application won't end the timer and return the char unless I preform a keydown again and that means I'm not gonna see the char i typed unless I press another key(just don't tell me it's because that the second timer is in keydown, I had to do that cause I didn't have any other choice...But at least I know the Idea but don't know how to implement it...I just need somebody to help me implement it...)
I need that second timer works in background when a keydown occurred it will reset and when a keyup occurred(means that key released)it will start again. whenever second timer(flagsw.ElapsedMilliseconds > 400)got bigger than that time it will do the job and clear the string for next use.
First I have to thanks to Chris...with your answer I got the idea and found the way...
It's now fully implemented and works here is my code if anybody else wants to use...(it's just the decoder part of the morse project)
namespace Morse_Code
{
public partial class frmdecdotmode : Form
{
Boolean flag_isdown = false;
Stopwatch sw = new Stopwatch();
Timer morse_timer = new Timer();
string listofcode;
DataSet datas = new DataSet();
DataBaseController dbc = new DataBaseController();
public frmdecdotmode()
{
InitializeComponent();
}
private void frmdecdotmode_FormClosing(object sender, FormClosingEventArgs e)
{
MainMenu mm = new MainMenu();
mm.Show();
this.Hide();
}
private void txtletters_KeyDown(object sender, KeyEventArgs e)
{
flag_isdown = true;
txtletters.BackColor = Color.Yellow;
sw.Start();
morse_timer.Stop();
}
private void txtletters_KeyUp(object sender, KeyEventArgs e)
{
flag_isdown = false;
txtletters.BackColor = Color.White;
sw.Stop();
if (sw.ElapsedMilliseconds < 250)
listofcode += ".";
else
listofcode += "-";
sw.Reset();
morse_timer.Start();
}
private void frmdecdotmode_Load(object sender, EventArgs e)
{
morse_timer.Interval = 1000;
morse_timer.Enabled = true;
morse_timer.Tick += morse_timer_Tick;
}
private void morse_timer_Tick(object sender, EventArgs e)
{
if (flag_isdown == false && listofcode != null)
{
datas = dbc.srchfortext(listofcode);
lbltext.DataBindings.Clear();
lbltext.DataBindings.Add("text", datas, "t.letter");
txtletters.Text += lbltext.Text;
listofcode = "";
}
}
}
}
Thanks to every body who helped me do this...
Ya Ali(a.s)
int sn = 0;
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Konfigürasyon Yükleniyor.";
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (sn == 3)
{
label1.Text = "Ayarlar Alınıyor";
}
if (sn == 5)
{
label1.Text = "Program Başlatılıyor";
}
sn++;
timer1.Stop();
}
When I open the form I want to change the label when I select the text range.
I assume that event handler is attached in designer to this timer1.
As far as I can understand, this label is never set, because you stop Timer after it hits first time.
In this case variable sn = 0 so non of if condition from your event handler is met.
I think to solve problem you sould remove this timer1.Stop() from event handler.
You probably want
private void timer1_Tick(object sender, EventArgs e) {
if (sn == 3)
label1.Text = "Ayarlar Alınıyor";
else if (sn == 5) {
label1.Text = "Program Başlatılıyor";
timer1.Stop(); // <- stop timer here on the 5th second, not on the 1st one
}
sn++;
}
Am new to C# and i need your help on this, I want to display one character at a time in a textbox this is my code
private void timer1_Tick(object sender, EventArgs e)
{
int i = 0; //why does this don't increment when it ticks again?
string str = "Herman Lukindo";
textBox1.Text += str[i];
i++;
}
private void button1_Click(object sender, EventArgs e)
{
if(timer1.Enabled == false )
{
timer1.Enabled = true;
button1.Text = "Stop";
}
else if(timer1 .Enabled == true )
{
timer1.Enabled = false;
button1.Text = "Start";
}
}
why does this don't increment when it ticks again?
Because your variable i is local to your event. You need to define it at class level.
int i = 0; //at class level
private void timer1_Tick(object sender, EventArgs e)
{
string str = "Herman Lukindo";
textBox1.Text += str[i];
i++;
}
On exit of your event, variable i becomes out of scope and looses its value. On the next event it is considered a new local variable with the initialized value of 0.
Next, you should also look for cross threaded exception. Since your TextBox is not getting updated on the UI thread.
The issue with you code is that you are assigning i = 0 with every tick, so it will always be 0 everytime it is used. I would suggest using a class level variable for this.
However, using a variable at class level means you are going to need to reset to 0 at some point, probably each time you start the timer.
A further point is that you are going to want to validate the tick event to ensure you don't try to access an index that doesn't exist (IndexOutOfRangeException). For this I would recommend automatically stopping the timer once the last letter has been printed.
With all that in mind, here is my suggested code:
int i = 0;// Create i at class level to ensure the value is maintain between tick events.
private void timer1_Tick(object sender, EventArgs e)
{
string str = "Herman Lukindo";
// Check to see if we have reached the end of the string. If so, then stop the timer.
if(i >= str.Length)
{
StopTimer();
}
else
{
textBox1.Text += str[i];
i++;
}
}
private void button1_Click(object sender, EventArgs e)
{
// If timer is running then stop it.
if(timer1.Enabled)
{
StopTimer();
}
// Otherwise (timer not running) start it.
else
{
StartTimer();
}
}
void StartTimer()
{
i = 0;// Reset counter to 0 ready for next time.
textBox1.Text = "";// Reset the text box ready for next time.
timer1.Enabled = true;
button1.Text = "Stop";
}
void StopTimer()
{
timer1.Enabled = false;
button1.Text = "Start";
}
My problem is very simple but I can't figure it out, so I need your help.
The problem is that I have a button and a label in a form, I simply want to click the button and see the label countdown from 10 to 0 and after that happens the form closes, that simple, can someone help me with this?
BTW, my real app is a form that shows video in real time from my webcam and the idea is to click the button, see the count down and when it finishes the appp saves the current frame as an image.
Thanks in advice!
It sounds like you probably just need three things:
A counter in your class as an instance variable
A timer (System.Windows.Forms.Timer or a DispatcherTimer depending on what UI framework you're using)
A method handling the timer's Tick even which decrements the counter, updates the UI, and stops the timer + takes a snapshot if the counter reaches 0
You can do all of this without any other threads.
Using WindowsFormsApplication u can do it like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = false; // Wait for start
timer1.Interval = 1000; // Second
i = 10; // Set CountDown Maximum
label1.Text = "CountDown: " + i; // Show
button1.Text = "Start";
}
public int i;
private void button1_Click(object sender, EventArgs e)
{
// Switch Timer On/Off
if (timer1.Enabled == true)
{ timer1.Enabled = false; button1.Text = "Start"; }
else if (timer1.Enabled == false)
{ timer1.Enabled = true; button1.Text = "Stop"; }
}
private void timer1_Tick(object sender, EventArgs e)
{
if (i > 0)
{
i = i - 1;
label1.Text = "CountDown: " + i;
}
else
{ timer1.Enabled = false; button1.Text = "Start"; }
}
}
You only need a label, a button and a timer.
use this code. put one timer,label and button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
}
private static int i = 10;
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "10";
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = (i--).ToString();
if (i < 0)
{
timer1.Stop();
}
}
}
Looking for some help on a problem Im having
sorry if this question has already been asked, I can not find anything similar.
The idea is when a picturebox is clicked changed the image to ON.
If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as OFF.
However if the picturebox is clicked ON and then held for 2 seconds and then returns i need the picturebox state to remain ON.
Here is what I have tried so far.
I believe for this to work correctly I need to stop MouseUp event from occuring.
Is there a way I can stop MouseUp when Tick occurs?
Is there a easier / better way to do this?
Any help would be appreciated.
private void time_HoldDownInternal_Tick(object sender, EventArgs e)
{
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = true;
}
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
time_HoldDownInternal.Enabled = true;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
//MessageBox.Show("mouse up");
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
}
private void pb_pictureBoxTest_Click(object sender, EventArgs e)
{
if (mainMenuVariables.mousedown == true)
{
if (mainMenuVariables.pictureBox == false)
{
mainMenuVariables.pictureBox = true;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
return;
}
if (mainMenuVariables.pictureBox == true)
{
mainMenuVariables.pictureBox = false;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
return;
}
}
if (mainMenuVariables.mousedown == false)
{
//nothing
}
}
Rather than starting a timer, just record the current time on mouse down. Then in mouse up, check if it has been 2 seconds. e.g:
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
mainMenuVariables.mousedowntime = DateTime.Now;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;
if ( clickDuration > TimeSpan.FromSeconds(2))
{
// Do 'hold' logic (e.g. open dialog, etc)
}
else
{
// Do normal click logic (e.g. toggle 'On'/'Off' image)
}
}