Unable to change text of textblock in Windows Phone 8.0 [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to set the value of a textblock to what hour it is, I have the following code:
timeHours.Text = DateTime.Now.Hour.ToString();
But the textblock doesn't show what hour it is, it doesn't show anything and stays as the original text, how do I solve this problem?

If you want to show the current hour in a text box, and have it update properly every 60 minutes then you need some sort of background process that changes the value that is displayed in the textbox. You can do this with a simple Timer object and an event.
using System.Timers;
class myclass
{
System.Timers.Timer timer;
public void initialise()
{
timer = new System.Timers.Timer(10000);
timer += new ElapsedEventHandler(_timer_Elapsed);
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
timeHours.Text = DateTime.Now.Hour.ToString();
}
}
This updates the hour every 10 seconds (10000 milliseconds), not very efficient but does the job.

Related

Calculating the average time between button clicks [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm doing this Windows Forms application in C# so when you click this button multiple times, it calculates the average clicks per second you do.
I just yesterday started learning C# and the only language im good at is Lua. In Lua this would be simple, just use a table as they're very dynamic and flexible. I just have no clue how to do this in C#, the MSDN Articles just confuse me.
How would I store the times between clicks? Arrays? I have no clue. This is the button click function I have so far
private void button1_Click(object sender, EventArgs e)
{
//DO STUFF
}
You could store the time of each click, then compute the average of the times between them:
private List<DateTime> clickTimes = new List<DateTime>();
private void button1_Click(object sender, EventArgs e)
{
this.clickTimes.Add(DateTime.Now);
if (this.clickTimes.Count > 2)
{
double averageSeconds = this.clickTimes.Zip(this.clickTimes.Skip(1), (a,b) => (b-a).TotalSeconds)).Average();
// Do something with the average seconds between each click here
}
}

Windows Form C# Button blink On and Off [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have this game in Windows Form that displays 9 buttons on the screen, you have to guess the number the computer thinks, if the number is correct the button should blink/flash on and off. I looked out but i couldn't find any relevant function to help me out. If someone can help me I would really appreciate. Thank you.
You can use the Timer class, which switches the background color of your buttons on the
Elapsed event.
Sample code:
// Create a timer with a ten second interval.
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
The OnTimeEvent function:
void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Change button color
}
Hope it helps :-)

C# Winform Auto Refresh: Every 5 Minutes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a C# WinForm application that I need to be refreshed every 5 minutes.
I have looked into Background Worker as well as, a Timer Class.
I was just wondering what is the best way to get main form refreshed so user updates can be displayed without having to close the application and reopening it.
that is called polling , you can use Timer control to do that.
Step 1: You need to Subscribe to the Timer Tick event.
Step 2: Set the Interval property of the Timer to 300000 milliseconds for raising the event for every 5 Minutes.
Step 3: In Tick Event Handler just do whatever you want to perform.
Step 4: you can Call the timer1.Stop() method whenever you want to stop the timer.
Note : if you don't stop the timer it becomes infinite.
if you want to stop the timer you can call timer1.Stop();
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=300000;//5 minutes
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
//update form with latest Data
}

Displaying a message box after a certain period of time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hie guys, i have no code at the moment but my problem is like this. i want to display a message box after a certain period of time after an event has occurred lets say after an enquiry has been made and this message box seeks to remind the user to check on the delivery status of the enquiry lets say after 5 minutes. Any ideas or alternatives?
First create a function to show your message, then you can use a timer.tick event to show your message at a given interval
static System.Windows.Threading.DispatcherTimer myTimer = new System.Windows.Threading.DispatcherTimer();
public void DoInquiry()
{
// do your inquiry stuff
////////////////////////
// Set Timer Interval
myTimer.Interval = = new TimeSpan(0,5,0); // 5 Minutes
// Set Timer Event
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Start timer
myTimer.Start();
}
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
ShowMessage("Please check on the status");
}
protected void ShowMessage(string Message)
{
System.Windows.MessageBox.Show(Message);
}
You would probably want to use a DispatcherTimer. You can use that to check when five minutes have passed then show a MessageBox when appropriate.

How to make a timer that reload your consoleapplication C#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a beginner in coding C#, and for my first program I want to have a timer that reloads my Console Application every 10 seconds. Is it possible? Can you write an example please?
Instead of "reloading" the application, you'd typically have a TImer trigger a specific routine within your application on a regular interval.
class Program
{
static void Main()
{
Console.WriteLine("Starting timer");
var timer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
timer.Elapsed += OnTimer;;
timer.Enabled = true;
Console.WriteLine("Press any key to shut down");
Console.ReadKey();
}
static void OnTimer(object source, ElapsedEventArgs e)
{
// This code will run every 10 seconds
Console.WriteLine("In timer");
}
}

Categories