Timer writes in log twice - c#

Everything works fine except for one thing. My program prints "player hit" 2 times then "mob hit" 2 times and then "player hit" 2 times again and so forth. I want it to type "player Hit" "mob Hit" "player Hit" "mob Hit" and so on. I can't see why it types it 2 times.
My code looks like this:
public partial class Form1 : Form
{
public Form1()
{
int Rase1 = 0;
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = false;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = false;
}
private void buttonChoseRase_Click(object sender, EventArgs e)
{
if (Rase1 == 0)
{
Rase1 = 1;
Rase Rase = new Race(this);
Rase.Show();
}
else if (Race1 >= 1)
{
buttonChoseRase.Enabled = false;
Rebirth Reb = new Rebirth(this);
Reb.Show();
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
textBoxCombatLog.Text = "";
buttonStart.Enabled = false;
buttonStop.Enabled = true;
timer1.Start();
timer2.Stop();
}
private void buttonStop_Click(object sender, EventArgs e)
{
buttonStart.Enabled = true;
buttonStop.Enabled = false;
timer1.Stop();
timer2.Stop();
}
private void LogWrite(string txt)
{
textBoxCombatLog.AppendText(txt + Environment.NewLine);
textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
}
private void timer1_Tick(object sender, EventArgs e)
{
LogWrite(TimeDate + "Player Hit \n");
timer1.Stop();
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
LogWrite(TimeDate + "Mob Hit \n");
timer2.Stop();
timer1.Start();
}
private string TimeDate
{
get { return "[" + DateTime.Now.ToString("HH:mm:ss") + "]" + ": "; }
}
}
Here is what is looks like running, and no matter what I do, everything shows up twice:

You've attached the event handler to the Tick event of the timer through designer in addition to through the code file itself. You need to remove one of the two. Either attach the event handler in code or attach it through the designer.

Related

How can I change the current file to another in Windows Media Player?

I am using Visual Studio .Net Framework to create a slideshow that operates on its own after clicking the "play" button. My current issue is that I do not know how to go about making the application go to the next file in the document after the video has ended. At the moment I have it setup for the user to select a time period for how long they want each image displayed, but I would rather have this feature automated.
This is my current code :
{
public partial class TVDisplay : Form
{
// Time f3 = new Time();
public TVDisplay()
{
//f3.Show();
InitializeComponent();
//both listview and listbox clear at initilize//
listBox1.Items.Clear();
listView1.Items.Clear();
//xml file from first application is read for department information, and that path and department are placed in textbox3 to be read by the listbox//
var xd = new XmlDocument();
string pathz = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filepath = pathz + #"\Assigned_Television.xml";
xd.Load(filepath);
// var list = xd.GetElementsByTagName("Television");
textBox3.Text = xd.GetElementsByTagName("Path")[0].InnerText;
}
protected override bool ProcessDialogKey(Keys keyData)
{
//when esacpe is pressed, the display screen is windowed instead of fullscreen//
if (Form.ModifierKeys == Keys.None && keyData == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
return true;
}
return base.ProcessDialogKey(keyData);
}
//Page load//
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.stretchToFit = true;
axWindowsMediaPlayer1.uiMode = "none"; //disables the play, pause, etc. buttons at the bottom of the windows media player//
listView1.Items.Clear(); //clears listview items//
Repopulate(); //calls repopulate function//
refreshtimer.Interval = 5 * 60 * 1000;
//timer for when images need to be displayed//
Timer tmr = new Timer();
int result = int.Parse(textBox2.Text);
tmr.Interval = result * 60 * 1001;
tmr.Tick += new EventHandler(timer1_Tick);
tmr.Start();
}
private void Repopulate()
{
foreach (var d in System.IO.Directory.GetFiles(textBox3.Text))
{
var dirName = new DirectoryInfo(d).Name;
listView1.Items.Add(dirName);
}
foreach (ListViewItem item in listView1.Items)
{
item.Selected = true;
listBox1.Items.Add(item.Text);
}
}
//hidden button that acts as a "play" button, taking url from textbox 1, playing the video, and looping it until it is time for the next image//
private void button1_Click(object sender, EventArgs e)
{
// MessageBox.Show(textBox4.Text);
axWindowsMediaPlayer1.URL = textBox4.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer1.settings.setMode("loop", true);
}
// timer that clicks button one//
private void timer1_Tick(object sender, EventArgs e)
{
button1.PerformClick();
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
}
//when the tvdisplay is shown, start button is clicked and button 1 (play button) is clicked//
private void TVDisplay_Shown(object sender, EventArgs e)
{
startbutton.PerformClick();
button1.PerformClick();
}
//hidden behind windows media player//
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Timer tmr = new Timer();
int result = int.Parse(textBox2.Text);
tmr.Interval = result * 60 * 1000;
//int result = int.Parse(textBox3.Text);
// tmr.Interval = 1 * 60 * 1000;
//tmr.Interval = 180000;
tmr.Tick += new EventHandler(timer2_Tick);
tmr.Start();
//textBox2.Clear();
var path = textBox3.Text;
textBox4.Text = path + listBox1.SelectedItem.ToString();
}
private void refreshtimer_Tick(object sender, EventArgs e)
{
listBox1.BeginUpdate();
int x = listBox1.SelectedIndex;
listBox1.Items.Clear();
listView1.Items.Clear();
Repopulate();
//selects the first index once files are repopulated//
listBox1.SelectedIndex = x;
listBox1.EndUpdate();
}
//hidden behind windows media display//
private void startbutton_Click(object sender, EventArgs e)
{
startbutton.Enabled = false;
timer3.Interval = 10000; //time in milliseconds
timer3.Tick += timer3_Tick;
timer3.Start();
//startbutton.Enabled = false;
if(listBox1.Items != null)
{
MessageBox.Show("There is no content entered for display.", "Error");
Application.Exit();
}
else {
listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % listBox1.Items.Count;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
startbutton.PerformClick();
}
private void timer3_Tick(object sender, EventArgs e)
{
startbutton.Enabled = true;
timer3.Stop();
}
}
}
Image/Video is displayed by Windows Media Player.

Timer.Tick will not start the new EventHandler

Ive got this timer that will count for 10 seconds. I want a progressbar to show that, how long it will take etc, it might be 10 seconds now but it could be dynamic in the future.
private void button1_Click_1(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
labelCapture.Text = " ";
buttonCapture.Enabled = false;
labelCapture.Text = "Measuring for 10 seconds...";
timerCapture.Interval = 10000;
timerCapture.Enabled = true;
UseWaitCursor = true;
timerCapture.Start();
Program.ModalForm.progressBarFormModal.Maximum = 10;
timerCapture.Tick += new EventHandler(timerCapture_Tick);
capture = true;
myFormModal.ShowDialog(this); // Where I open the ModalForm
}
and my event is as follows
void timerCapture_Tick(object sender, EventArgs e)
{
if (Program.ModalForm.progressBarFormModal.Value != 10)
{
Program.ModalForm.progressBarFormModal.Value++;
}
else
{
timerCapture.Stop();
}
}
how the modal Form gets closed
private void TickToggle(object sender, EventArgs e)
{
capture = false;
timerCapture.Stop();
UseWaitCursor = false;
timerCapture.Enabled = false;
myFormModal.Close(); // Close the modal form after timer is done
}
Does anyone seen an error that I might have overlooked?

How to show the label for couple of seconds and show another label in form

I have a mini form3 https://imageshack.com/i/p1zxB6Lqp which shows a gif image running for 4 sec. So i need to show 4 different
labels in same position..
For example
label 1 - `Connecting to smtp server..`
label 2 - `Fetching recipients..`
label 3 - `Attaching necessary G-code files..`
label 4 - `Please wait sending..`
How can i show all these labels one after another in same position.. so it looking more professional
for sending mail.
My code snippet:-
Form1
private void button2_Click(object sender, EventArgs e)
{
//mail inforamtion
_f3.ShowDialog(); // - - >> is the form i wanted with all labels
smtp.Send(msg);
MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
Environment.Exit(0);
}
Form3:
Timer formCloser = new Timer();
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
Please help me out.. how can i add label in my form..
Found the answer.. I kept two timers for each function,
timer1 to run the mini form only for 5 seconds.
and timer two to run only for 1 sec. If any body has a better code to share with please..
Most welcome!!
Here is my code:
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
if (StopTime == 1)
{
label1.Text = " Connecting to smtp server..";
}
if (StopTime == 2)
{
label1.Text = " Fetching recipients..";
}
if (StopTime == 3)
{
label1.Text = " Attaching G-code files..";
}
if (StopTime == 4)
{
label1.Text = " Done!!";
StopTime = 0;
timer2.Stop();
}
}

How to stop timer if a Listbox is empty or items are less than 0 in winform applications

I am making a windows app.
A button1 which gets the items in listBox1 from server at the start.
A button2 which starts the timer1.
A timer1 which removes items from listBox1 .
A progressBar1 which shows the progress of this process.
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type='get' to='" + textBox1.Text + "#conference.jabber.com' id='qip_1026'><query xmlns='http://jabber.org/protocol/muc#admin'><item affiliation='outcast' /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
timer1.Start();
timer1.Interval = 4000;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
The above code works well till there is one item left in listBox1.
The error is:
System.ArgumentOutOfRangeException was unhandled
Message=InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
It raises an error when listBox1 reaches 0. I want to stop the timer when listbox1 is empty or gets no items or 0 items.
The problem is in this code:
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
So what is happening is that you are using count to check >0 then calling jabber to do the work, It the call becomes slow- you will see multiple timers getting fired back. So a big queue will be collected there. You need to modify the code a bit here using lock to hold the list and allow jabber to do its work:
private void timer1_Tick(object sender, EventArgs e)
{
lock (listBox1)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text +
"#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" +
listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
}
Lock will also ensure that the items are removed correctly.
To save the file as per comment below :
public class ChatHistoryManager
{
private readonly RichTextBox richTextBox;
private Timer timer = new Timer();
public ChatHistoryManager(RichTextBox richTextBox)
{
this.richTextBox = richTextBox;
this.InitializeTimer();
}
public string Location { get; set; }
private void InitializeTimer()
{
this.timer.Tick += timer_Tick;
this.timer.Enabled = true;
this.timer.Interval = (int) TimeSpan.FromHours(1).TotalMilliseconds;
}
void timer_Tick(object sender, EventArgs e)
{
this.SaveFile();
}
public void SaveFile()
{
//Save the file to the location
this.richTextBox.SaveFile(this.Location,RichTextBoxStreamType.RichText);
}
public void Stop()
{
this.timer.Stop();
}
}
Now we need to set in form like this:
private void Form1_Load(object sender, EventArgs e)
{
ChatHistoryManager chatHistoryManager = new ChatHistoryManager(this.richTextBox1);
chatHistoryManager.Location = #"C:\Development\test.txt";
}
try this
int count = City.Items.Count - 1;
for (int i = count; i > 0; i--){
City.Items.RemoveAt(i);
}
Here is what worked for me.
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type="get" to="" + textBox1.Text + "#conference.jabber.com" id="qip_1026"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="outcast" /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
// Set the timer interval to four seconds
timer1.Interval = 4000;
// Start the timer
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Disable the timer while we are working in this procedure so
// it doesn't tick while we are working in this procedure
timer1.Enabled = False;
// Send only if there are items in the ListBox
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type="set" to="" + textBox7.Text + "#conference.jabber.com"><query xmlns="http://jabber.org/protocol/muc#admin"><item jid="" + listBox1.Items[0].ToString() + "" affiliation="none" /></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
// Re-enable only if there are items left in the ListBox
if (listBox1.Items.Count > 0)
{
timer1.Enabled = True;
}
}

Countdown after clicking a button that goes from 10 to 0 setting text on a label C#

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();
}
}
}

Categories