3

I want to design changing time on maskedtextbox in my application like windows where time changes on every second. I have set maskedtexbox1 as below:

maskedTextBox1.Text = DateTime.Now.ToShortTimeString();

which is showing current system short time but it’s not changing on every second like windows. How to do?

I'm on Visual Studio 2005, and .NET is below 3.5.

cspolton
  • 4,495
  • 4
  • 26
  • 34
Mahesh Wagh
  • 151
  • 1
  • 13
  • If the answer that @KonradViltersten gave you is to your satisfaction, grade it up **and** mark the answer as good (by clicking the green check thingy). That way you'll help the next person to find the best answer. And if we haven't solved your problem, please tell us using the comments what's missing still. Contribute, cooperate and have a happy coding! :) –  Nov 04 '12 at 10:06
  • @AndreasJohansson, thanks your advise will more useful for me in future also. thanks buddy – Mahesh Wagh Nov 04 '12 at 10:39
  • No problems. Feel free to grade up if you like an answer (or grade down if you dislike it - **always** telling a person why, like this: -1 because targetted wrong .NET version). And **ALWAYS** grade an answer as right (like you did now) or tell us what's missing still. Happy coding! –  Nov 04 '12 at 11:38

3 Answers3

1

I guess you'll need to setup a Timer which updates your maskedTextBox1 every second.

For how to do that, please see: Add timer to a Windows Forms application

Cheers. Keith.

Community
  • 1
  • 1
corlettk
  • 13,288
  • 7
  • 38
  • 52
1

You can use System.Windows.Forms.Timer to update textbox value every second for example:

var timer = new Timer();
timer.Interval = 1000;

timer.Tick += delegate
            {
                textBox1.Text = DateTime.Now.ToLongTimeString();
            };

timer.Start();
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • Good code. But I wonder if it helps the poor OP. He want to know how to set a ticker off. I'm not sure the delegate structure (nice as it may bem I agree) is the best way to help him. When I was a noob and people helped me on a level too high, I often felt **more** confused afterwards, haha. But maybe I'm just slow... :) –  Nov 04 '12 at 09:33
  • @Cuong Le, I am using vs-2005 the timer event statement does not match – Mahesh Wagh Nov 04 '12 at 09:39
  • @MaheshWagh: could you update your question to mention you are using .NET 2.0? – cuongle Nov 04 '12 at 09:43
  • @MaheshWagh It's not that you've got VS05. It's your .NET that is **waaay** below 4 (or was it 3.5 that came with labda expression). Cuong Les suggestion is good but he thinks that everybody is at the joyful liberty of upgrading his/her IDE whenever they want. Also, you **should** upgrade your IDE to **at least** VS10, if you can. Otherwise, you **might** want to put in the questions that you're on an old version of the framework. –  Nov 04 '12 at 09:45
  • I haven't downvoted you but if I'm to make a guess as for the reason, it was probably because the lambda expressions **may** look scary. Or maybe someone just pasted your code and run it against wrong version of .NET, assuming then that the code is **almost** working. I'll grade you back up in a few hours, when my grading powers are back. – Konrad Viltersten Nov 04 '12 at 09:52
  • Also, I think it's a really bad style, border-line bullying, to downgrade without a word. It's rude, offensive and hurtful. After all, everybody tries to contribute, right? – Konrad Viltersten Nov 04 '12 at 09:53
  • @user: yes, comment from why downvoting is valuable to improve the answer – cuongle Nov 05 '12 at 03:19
1

I'd use the timer and fire an event every second to update the time.

  1. Create a timer (an instance of class Timer in the package System.Windows.Forms).
  2. Set its frequency to 1 second (i.e. 1000 milliseconds).
  3. Tell it what method to call when it goes off (the event handler Kaboom).

Somewhere in your executable code you do that by typing the following.

Timer ticker= new Timer();
ticker.Interval = 1000;
ticker.Tick += new EventHandler(Kaboom);

In the same class (or, if you're confident how to do it, somewhere where you can reach the code) you also create the handler for the fired event of ticking, so that the promise you made about a method to be called when the timer goes off is kept.

private void Kaboom(Object sender, EventArgs eventArgs)
{
  // Execute the tickability code
  MaskedTextBox1.Text = DateTime.Now.ToShortTimeString();
}

Also, don't forget to actually start your ticker when you feel that you're ready.

MyTimer.Start();

Tada!

EDIT:

For the sake of completeness, I'm also going to paste in a part of the reply of @CuaonLe (a higher threshold of competence and requirement for .NET 3.5 or newer).

Timer timer = new Timer { Interval = 1000 };
timer.Tick += (obj, args) 
  => MaskedTextBox1.Text = DateTime.Now.ToLongTimeString();
timer.Start();
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • You might want to paste in @CuongLe 's reply as well. If somebody searches for an answer to a similar question, they'll get the best of the two worlds. –  Nov 04 '12 at 10:03