how to update a textlabel when a variable is changed - c#

im kinda new to programming, and i was trying to recreate candybox (some interactive website)
in windows form application. i have a variable named candy, and a timer that every second adds one to the variable candy, i have tried putting the label.text = "candy: " + candy.ToString(); in that method and it doesnt seem to work, any one able to help?
code:
public partial class Form1 : Form
{
public int candy { get; set; }
public int lollipop { get; set; }
public Form1(int value, int lollipop)
{
this.candy = value;
this.lollipop = lollipop;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "candy: 0";
button2.Enabled = false;
button2.Visible = false;
label2.Visible = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerHandler);
timer.Start();
}
private void button1_Click(object sender, EventArgs e)
{
candy += 2;
UpdateCandy(candy, lollipop);
label2.Text = "lollipops" + lollipop.ToString();
if (candy > 9)
{
button2.Enabled = true;
button2.Visible = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (candy <= 10)
{
button2.Enabled = false;
}
candy -= 10;
lollipop += 1;
UpdateCandy(candy, lollipop);
label2.Text = "lollipops" + lollipop.ToString();
label2.Visible = true;
}
System.Timers.Timer timer = new System.Timers.Timer(1000);
private void TimerHandler(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime start;
TimeSpan elapsed = TimeSpan.MaxValue;
while (elapsed.TotalSeconds > 1.0)
{
start = DateTime.Now;
candy += 1;
elapsed = DateTime.Now - start;
}
timer.Interval = 1000 - elapsed.TotalMilliseconds;
timer.Start();
}
public void Form1_Shown(object sender, EventArgs e)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerHandler);
timer.Start();
}
private void UpdateCandy(int candy, int lollipop)
{
label1.Text = "candy: " + candy.ToString();
label2.Text = "lollipops" + lollipop.ToString();
}
}
and im using visual studio 2012 (if this information is of any help)

Related

How do you select next combo box selection automatically in x second?

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

Timer Countdown with progress bar C#

I want to sync my progress bar in my project with the timer countdown.
This is what I has now:
namespace Timer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
int counter = 80;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.Increment(1);
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}
}
}
I want the progress bar to finish when the timer to ends.
You need to specify the progressBar1 to have its .Step and Maximum properties match the Interval and counter variable. For example:
private int counter = 80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
// The max is the total number of iterations on the
// timer tick by the number interval.
progressBar1.Max = counter * 1000;
progressBar1.Step = 1000;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
// Perform one step...
progressBar1.PerformStep();
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}

WinForms sometimes appear twice and sometimes once

I am confuse at this one, the windows form called "Contact" sometimes called twice after the "Welcome Screen" and sometimes it ("Contact") is called once only. I am pretty sure that I called the form once only.
Here is the code that I am using:
The form below "WelcomeScreen" is the first one called when run the program:
public partial class WelcomeScreen : Form
{
int timeLeft = 5;
Timer _timer = new Timer();
BackgroundWorker _backgroundWorker = new BackgroundWorker();
public WelcomeScreen()
{
InitializeComponent();
_backgroundWorker.WorkerReportsProgress = true;
_backgroundWorker.DoWork += BackgroundWorker_DoWork;
_backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
_timer.Interval = 1000;
_timer.Tick += Timer_Tick;
}
void WelcomeScreen_Load(object sender, EventArgs e)
{
_backgroundWorker.RunWorkerAsync();
}
void Timer_Tick(object sender, EventArgs e)
{
timeLeft--;
if (timeLeft <= 0)
{
_timer.Stop();
this.Hide();
Contact _contact = new Contact();
_contact.ShowDialog();
this.Close();
}
}
void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (e.ProgressPercentage <= 300)
{
_timer.Start();
this.label3.Text = "Completed ( " + timeLeft + " ) ";
this.label4.Text = string.Empty;
}
}
void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 300; i++)
{
_backgroundWorker.ReportProgress(i);
System.Threading.Thread.Sleep(200);
}
}
The form below "Contact" is called after the "WelcomeScreen":
public partial class Contact : Form
{
const int CP_NOCLOSE_BUTTON = 0x200;
public Contact()
{
InitializeComponent();
}
void Contact_Load(object sender, EventArgs e)
{
SystemManager.SoundEffect();
}
void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
I appreciate your answer!
Thank you
It looks like your Timer is causing the issue, by firing multiple times...
You have this condition inside your timer code:
if (timeLeft <= 0)
And the line before is timeLeft--. After timeLeft becomes 0, it will continue getting smaller (-1, -2, etc) and the form will be shown each time.
A quick fix is to either change the condition to timeLeft == 0 or change the type of timeLeft to a uint. Of course, these are both hacks. The correct fix would be to fix your code to stop the timer from firing when required.

Multiple panel when click on button occurs in C#

I have created a timer, now I want whenever I click on "Add Timer" button a new panel with same existing panel property appears on the form below to existing panel. How can I do this?
The code I have so far:
public partial class Form1 : Form
{
int countdown;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
countdown = int.Parse(textBox1.Text);
TimeSpan ts = TimeSpan.FromSeconds(countdown);
label4.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", ts.Hours, ts.Minutes, ts.Seconds);
ts = ts.Subtract(new TimeSpan(0, 0, 1));
textBox1.Text = ts.TotalSeconds.ToString();
label4.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", ts.Hours, ts.Minutes, ts.Seconds);
if (ts.TotalSeconds == 0)
{
//timer1.Stop();
label4.ForeColor = System.Drawing.Color.Red;
MessageBox.Show("Times Up!");
}
}
private void button3_Click(object sender, EventArgs e)
{
if(button3.Text=="Start")
{
timer1.Enabled = true;
button3.Text = "Reset";
timer1.Start();
}
else
{
timer1.Enabled = false;
button3.Text = "Start";
timer1.Stop();
textBox1.Text = "00";
label4.Text = "00:00:00";
}
}
private void button4_Click(object sender, EventArgs e)
{
if(button4.Text=="Pause")
{
timer1.Stop();
button4.Text = "Resume";
}
else
{
button4.Text = "Pause";
timer1.Start();
}
}

c# counting clicks

I have a timer and in 30 minutes I want to count clicks and show it in a textbox. but how? here is timer code:
decimal sure = 10;
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
sure--;
label3.Text = sure.ToString();
if (sure == 0)
{
timer1.Stop();
MessageBox.Show("Süre doldu");
}
}
Declare your clickCounter at global, and raise your counter++ in Mouse Click Event.
If you do it more specific, you can use Background Worker, to track time.
and use Application.DoEvents() to write remaining to to textBox
Put a button, 2 labels, and a timer. rename labels with lblClickCount and lblRemainingTime
private int clickCounter = 0;
private void button1_Click(object sender, EventArgs e)
{
clickCounter++;
lblClickCount.Text = clickCounter.ToString();
}
decimal sure = 10;
private void timer1_Tick(object sender, EventArgs e)
{
sure--;
lblRemainingTime.Text = sure.ToString();
Application.DoEvents();
if (sure == 0)
{
timer1.Stop();
MessageBox.Show("Süre doldu. Toplam tiklama sayisi:" + clickCounter.ToString());
}
}
If you wanted to reuse buttoN1 to count the clicks but not Start new timer you can add a if around the code you want to protect.
bool hasTimerStarted = false;
int numberOfClicks = 0;
private void button1_Click(object sender, EventArgs e)
{
if(!hasTimerStarted)
{
button1.Enabled = true;
timer1.Start();
hasTimerStarted = true;
}
++numberOfClicks;
}
When the timer expires you reset the count and if the timer has started.
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
label3.Text = elapsedTime;
labelClicks.Text = "User clicked " + clicksNo.toString() + "nt times..";
if (stopWatch.ElapsedMilliseconds >= this.minutes * 60 * 1000)
{
timer1.Stop();
MessageBox.Show("Time elapsed.");
hasTimerStarted = false;
numberOfClicks = 0;
}
}

Categories

Resources