ok i have question, i made this code to play axmediaplayer base on item listed on listbox.
first i make this code to make a list using opendialog :
private string[] files, path;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files = openFileDialog1.SafeFileNames;
path = openFileDialog1.FileNames;
for (int i = 0; i < files.Length; i++) {
listBox1.Items.Add(files[i]);
}
}
}
and then it play the music when the listbox index changed (when the item on the list box cliked) using this code :
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
}
it works fine, and then i want player to automove to the next song base on item on my listbox. with using events PlayStateChange, so i make this code
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
if(listBox1.SelectedIndex < files.Length - 1)
{
listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
}
}
}
the selected index change, but the player doesn't auto play the next song. i must click the play button manually in order to play the list. can anyone help me up?
ok i found it, the solution is to add timer before playing the next song.
first im adding timer, that shoud be timer1. and then i change playstate event to something like this :
private void axWindowsMediaPlayer1_PlayStateChange(object sender, axWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
timer1.Interval = 100;
timer1.Enabled = true;
}
}
then on the timer i adding tick event, the tick event is something like this :
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < files.Length - 1)
{
listBox1.SelectedIndex++;
timer1.Enabled = false;
}
else
{
listBox1.SelectedIndex = 0;
timer1.Enabled = false;
}
}
now its work fine ^^
Below functionality worked for me:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
timer1.Interval = 100;
timer1.Start();
timer1.Enabled = true;
timer1.Tick += timer1_Tick;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
/// method to play video list items
myFuntiontoPlayVideo();
timer1.Enabled = false;
}
Related
Using C#, windows form.
I am trying to make the program automatically select next item on combo box every x seconds and once it reaches the last one, it goes back to the first one on list. i got pretty much everything minus auto combo box selection part. :(
help please.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//pictureBox1.Image = Image.FromFile(#"Z:\DSCF1661.jpg");
DirectoryInfo test = new DirectoryInfo(#"C:\temp");//Assuming Test is your Folder
FileInfo[] Files = test.GetFiles("*.pdf"); //Getting Text files
comboBox1.DataSource = Files;
comboBox1.DisplayMember = "Name";
timerset();
}
public void axSetting()
{
axAcroPDF1.setShowToolbar(false);
axAcroPDF1.setView("FitH");
axAcroPDF1.setPageMode("none");
axAcroPDF1.setLayoutMode("SinglePage");
axAcroPDF1.Show();
}
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
axAcroPDF1.LoadFile(#"C:\temp\" + comboBox1.Text);
axAcroPDF1.src = #"C:\temp\" + comboBox1.Text;
axSetting();
}
//private System.Windows.Forms.Timer timer1;
public void comboBoxSelect()
{
if (comboBox1.SelectedIndex < comboBox1.Count) // this part... :(
{
comboBox1.SelectedIndex += 1;
}
else
{
comboBox1.SelectedIndex = 0;
}
}
public void timerset()
{
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 5000; // in miliseconds
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
comboBoxSelect();
}
This should work:
if(combobox.SelectedIndex < (combobox.Items.Count -1))
{
combobox.SelectedIndex += 1;
}
else
{
combobox.SelectedIndex = 0;
}
How do I auto select a item in a ListBox then set it as text in a TextBox then wait 3 seconds and move onto the next line down and repeat?
private void button2_Click(object sender, EventArgs e)
{
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach (var item in listBox1.Items)
{
textBox2.Text = listBox1.Text;
}
}
The ListBox has a lot of useful properties:
private void timer1_Tick(object sender, EventArgs e)
{
textBox2.Text = listBox1.SelectedItem.ToString();
listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % listBox1.Items.Count;
}
% is the modulo operation and returns the remainder of the division. It ensures that always values between 0 and SelectedIndex - 1 are returned and makes the indexes repeat.
Also, if no item is selected, you will get a SelectedIndex of -1 and SelectedItem will be null. So be sure to either avoid these cases by setting appropriate initial conditions or add checks.
E.g.:
if (listBox1.Items.Count > 0) {
if (listBox1.SelectedIndex == -1) {
listBox1.SelectedIndex = 0;
}
... // code of above
}
Your timer1_Tick should be something like this:
public Form1()
{
InitializeComponent();
timer1.Interval = 3000;
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int i = rnd.Next(0, listBox1.Items.Count - 1);
textBox2.Text = listBox1.Items[i].ToString();
}
EDIT:
int i;
private void timer1_Tick(object sender, EventArgs e)
{
if (i > listBox1.Items.Count - 1)
{
i = 0;//Set this to repeat
return;
}
textBox2.Text = listBox1.Items[i].ToString();
i++;
}
And also set the timer's Interval to 3000.
I have 40 buttons that all do something slightly different when clicked, I would like to condense this down if I can. I also want to say, if one of the buttons is clicked, create a timestamp which can be accessed by the class.
Here is the code for 2 out of 40 of the buttons:
private void Btn1_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 5; i++)
{
CheckBox CheckBox = (this.FindName(string.Format("Check{0}", i)) as CheckBox);
if (CheckBox != null)
{
CheckBox.IsChecked = true;
}
}
}
private void BtnDisable1_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 5; i++)
{
CheckBox CheckBox1 = (this.FindName(string.Format("Check_D{0}", i)) as CheckBox);
if (CheckBox1 != null)
{
CheckBox1.IsChecked = false;
}
}
}
I think one way of doing it is putting it in an array and whenever one of the 40 buttons are clicked it looks in the array on what to do next? I'm not really sure, thank you!
You can make this simple using one method.
Answer is updated based on this discussion
private void DoWork(int checkboxGroup, bool enable)
{
int start = checkboxGroup * 4;
for (int i = start; i < start + 4; i++)
{
CheckBox CheckBox = this.FindName("CheckBox" + i) as CheckBox;
if (CheckBox != null)
{
CheckBox.IsChecked = enable;
}
}
}
private void Btn1_Click(object sender, RoutedEventArgs e)
{
DoWork(1 , true);
}
private void BtnDisable1_Click(object sender, RoutedEventArgs e)
{
DoWork(1 , false);
}
Because there are 40 methods like this you can use Expression bodied methods. You must have C#6 to use this feature.
private void Btn1_Click(object sender, RoutedEventArgs e) => DoWork(1 , true);
private void BtnDisable1_Click(object sender, RoutedEventArgs e) => DoWork(1 , false);
private void Btn2_Click(object sender, RoutedEventArgs e) => DoWork(2, true);
private void BtnDisable2_Click(object sender, RoutedEventArgs e) => DoWork(2, false);
// and so on
I am using a C# webBrowser control using the DocumentCompleted -
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
I am then navigating -
webBrowser1.Navigate("myUrl")
However if the request to that server hangs, i.e. the page does not complete after say 10 seconds, how could I implement the webBrowser1.Stop();?
I did try to implement a count, that if it got to 20 i.e. the webBrowser1_DocumentCompleted went into an infinite loop (the page would not complete) then stop however not sure if this is the most straightforward way of doing htis?
This might be in really bad practice so I apologize but you could use a boolean control with a timer to check whether or not the document has completed and if it hasn't, close the webBrowser.
First of all add a timer(assuming its called Timer1) to your form, setting interval to 1000 and create an int and bool control.
int timeLeft;
bool hasCompleted = false;
Run your URL as normal and start your timer
webBrowser1.Navigate("myUrl");
timeLeft = 10;
Timer1.Start();
And your timer should look like this;
private void timer1_Tick(object sender, EventArgs e)
{
if(timeLeft > 0) {
timeLeft = timeLeft - 1;
}
if(timeLeft = 0 && !hasCompleted)
{
timer1.Stop();
webBrowser1.Stop();
}
else{
timer1.Stop();
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
hasCompleted = true;
//your code
}
I have tried to achieve this using the timer.
I just added a timer and set the interval.
Here is the code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += timer1_Tick;
webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler( webBrowser1_DocumentCompleted);
LoadBrowser();
}
void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
webBrowser1.DocumentText = "Cancelled";
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (timer1.Enabled)
{
MessageBox.Show("Page Loaded succesfully");
}
}
private void LoadBrowser()
{
timer1.Enabled = true;
webBrowser1.Url = new Uri("http://www.microsoft.com");
}
}
I have 2 buttons in my win app.
Button1 do a task :
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
String[] a = textBox7.Text.Split('#');
progressBar1.Maximum = a.Length;
for (var i = 0; i <= a.GetUpperBound(0); i++)
{
ansh.Close();
progressBar1.Value++;
}
}
Button 2 do following
private void button2_Click(object sender, EventArgs e)
{
foreach (string item in listBox2.Items)
textBox7.Text += item.Contains("#") ? string.Format("{0}#", item.Split('#')[0]) : string.Empty;
}
I just want to use just one button for two events.
But I want the event of button2 to be called before event that was called by button1.
Means I want to use just one button instead of button 1 and 2.and when I click I want first thing to be done is getting listbox items in a textbox.
{
foreach (string item in listBox2.Items)
textBox7.Text += item.Contains("#") ? string.Format("{0}#", item.Split('#')[0]) : string.Empty;
}
and then the event of starting progress bar and closing connection x.
progressBar1.Value = 0;
String[] a = textBox7.Text.Split('#');
progressBar1.Maximum = a.Length;
for (var i = 0; i <= a.GetUpperBound(0); i++)
{
ansh.Close();
progressBar1.Value++;
}
You can use PerformClick method of button object
Button button1 = new Button(), button2 = new Button();
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
void button1_Click(object sender, EventArgs e)
{
/* .................... */
button2.PerformClick(); //Simulate click on button2
/* .................... */
}
void button2_Click(object sender, EventArgs e)
{
/* .................... */
}
I'd suggest removing the logic from behind the click events into separate methods.
private void MethodOne()
{
progressBar1.Value = 0;
String[] a = textBox7.Text.Split('#');
progressBar1.Maximum = a.Length;
for (var i = 0; i <= a.GetUpperBound(0); i++)
{
ansh.Close();
progressBar1.Value++;
}
}
private void MethodTwo()
{
foreach (string item in listBox2.Items)
textBox7.Text += item.Contains("#") ? string.Format("{0}#", item.Split('#')[0]) : string.Empty;
}
private void button1_Click(object sender, EventArgs e)
{
MethodTwo();
MethodOne();
}
private void button2_Click(object sender, EventArgs e)
{
MethodTwo();
}
In my experience, it's easier to maintain and test this way. Having different controls' events all calling each other makes it tougher to follow the logic.
You can trigger the click event of Button2 manually:
private void button1_Click(object sender, EventArgs e)
{
button2_Click(sender,e);
...
}
Just in case you neen events:
Button1.click += method1;
Button1.click += method2;
void method1(object sender, EventArgs e)
{
// do your stuff
}
void method2(object sender, EventArgs e)
{
// do your stuff
}