TextBox doesn't show characters

Page 1 of 1

14 Replies - 2394 Views - Last Post: 12 November 2012 - 04:18 AM Rate Topic: -----

#1 Zigon   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

TextBox doesn't show characters

Posted 25 January 2012 - 09:54 AM

Hi there!
I'm new to this forum and C# itself.
While trying to learn some basic stuff, I stubled into a problem I just do not understand. I am trying to to output some text to a textBox char by char (using the method described in the snippets section). The problem is, that when I redirect the output to the Debug window, everything works just fine, but when I'm directing it to the textBox control, nothing happens until the loop is finished - then the desired text appears.

Here is the code I'm using:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace Typewriter_effect
    {
    public partial class Form1 : Form
        {
        public Form1()
            {
            InitializeComponent();
            }

        private void button1_Click(object sender, EventArgs e)
            {
            string message = "HELLO THERE!";
            foreach (char x in message.ToCharArray())
                {
                //Debug.Write(x);
                textBox1.Text += Convert.ToString(x);
                
                System.Threading.Thread.Sleep(300);
                }

            }
        }
    }



What am I doing wrong?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: TextBox doesn't show characters

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:08 AM

Thread sleep is evil and should not be used casually.

It does NOT mean "do everything else you're supposed to like updating the GUI, but sit here and wait 300 ms"

It DOES mean "Do absolutely nothing on this thread. Not a fraking thing. Don't respond to events. Don't update the GUI."

So what's happening is you're sleeping the thread without giving the GUI a change to update.

If you want an active delay try something more like this

void Wait(int DelayInMS)
{
   DateTime timeout = DateTime.Now.AddMilliseconds(DelayInMS);
   while (timeout > DateTime.Now) Application.DoEvents();
}


Then call that from any of your methods.
Was This Post Helpful? 2
  • +
  • -

#3 Toadill   User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 434
  • Joined: 08-January 12

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:21 AM

Sleep is bugged
Was This Post Helpful? 0
  • +
  • -

#4 Zigon   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:36 AM

View PosttlhIn`toq, on 25 January 2012 - 10:08 AM, said:

Thread sleep is evil and should not be used casually.

It does NOT mean "do everything else you're supposed to like updating the GUI, but sit here and wait 300 ms"

It DOES mean "Do absolutely nothing on this thread. Not a fraking thing. Don't respond to events. Don't update the GUI."

So what's happening is you're sleeping the thread without giving the GUI a change to update.

If you want an active delay try something more like this

void Wait(int DelayInMS)
{
   DateTime timeout = DateTime.Now.AddMilliseconds(DelayInMS);
   while (timeout > DateTime.Now) Application.DoEvents();
}


Then call that from any of your methods.

Thanks a ton! It works great!
Would it be safe to use this method any time I want my App to wait for a specific time? What I mean is would it be usefull to put this in some common library of my own for later re-use or is there any bulit-in funcion available (stability, memory leaks,...)?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:39 AM

View PostToadill, on 25 January 2012 - 11:21 AM, said:

Sleep is bugged


Not sure what that is supposed to mean or how it is helpful or how you feel it is buggy. It does exactly what it says it does: It sleeps the thread.

Its just that most rookies don't know what that really means. If one does not understand their GUI is on the same thread as their computations then it might seem buggy.

So its not that sleep is buggy, its that someone's understanding is.

This is why we so often say that the GUI should be on its own thread. It should then react to events on work threads.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:52 AM

View PostZigon, on 25 January 2012 - 11:36 AM, said:

Thanks a ton! It works great!
Would it be safe to use this method any time I want my App to wait for a specific time? What I mean is would it be usefull to put this in some common library of my own for later re-use or is there any bulit-in funcion available (stability, memory leaks,...)?


Is it safe-ish. Yes.

Should you continue to code like this? No. Your GUI should be on its own thread, separate from work/computations/processing.

Think of it this way... The GUI is your dashboard. Its only real purpose should be to interact with the user just like your car dashboard only REPORTS what is happening but it really doesn't make anything happen. Your application GUI should be the same way. It should raise events to tell your other threads what to do, and listen for events from those threads so it can update the information it is reporting to you.

Should you be building a library? No. Let's be fair and say you are just learning. You really don't know what you need in a library, possibly the best way to organize such a thing and so on. Or if you are going to go ahead with a library, just realize you are going to re-create it 10 times in the next year.

Should a function like this go in a library? Eh? I suppose it could. It would make for a single place of change if you later find a better way to do a wait. That's good code isolation practice. On the other hand, its the kind of thing I can type faster than I can drill through a library to locate again. "Did I put that under myFunctions.Common.Threading, or did I put it under myFunctions.Forms.Common, or is it under myFunctions.Timing.Wait ?"

If you'd like to look at some examples on separating GUI threads, raising events and so on these should help:
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events


For more on threading take a look at FAQ 12

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 2
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5105
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: TextBox doesn't show characters

Posted 25 January 2012 - 11:49 AM

View PostToadill, on 25 January 2012 - 11:21 AM, said:

Sleep is bugged


It's not bugged at all. It does exactly what it's supposed to do.

Also, for the record, Thread.Sleep(x) also doesn't guarantee that the sleep time will be exactly x. It'll be at least x.

Also, I prefer the Timer method to using Application.DoEvents, just because it fits the event-driven model a bit better. Idling in a while loop is really procedural. Subscribing to an event that'll be fired later is event-driven (though behind the scenes it's doing an idle loop anyway).
Was This Post Helpful? 1
  • +
  • -

#8 Zigon   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:19 PM

I'm sorry for the delay, but I had to go to work.
Thanks for all replies, they were all helpfull!

I have one question left: Suppose I have some functions stored in in some library of my own, that I would use in allmost every project I make (let it call MyFunctinsIUseOnDalyBases). So the big Q is, is there a way to include that lib in the startup template of the VS? What I mean is when I start new project, that lib would be automaticly added to the "USES" section of the project?

Thatns again, guys!
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5105
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:30 PM

Well, you can edit the template files. I'm not sure off the top of my head which folder those reside in, but you should be able to find it by searching for vs project template files or something in that vein.

However, just adding a using directive isn't going to do you any good. Using directives are for namespaces, not for assemblies. You add a reference to an assembly, which allows it to be used in the project. Then you add a using directive to a code file to tell the compiler that you won't be typing the fully qualified names of the components that belong to a particular namespace (that exists in one or more of your references). VS adds references to several libraries by default. You'll need to edit the project template itself to include a reference to one of your own libraries, I think. I've never tried.
Was This Post Helpful? 0
  • +
  • -

#10 Zigon   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: TextBox doesn't show characters

Posted 25 January 2012 - 10:57 PM

Oh, I see...

Well, how would I go about doing something like this:
Say I have a procedure, that checkes for the net conectivity (just for the cause of this Q). If I got it right, I would normaly put it in a class lib (compile it into a DLL - or am I wrong again?). Let's say, I want that procedure (or DLL) in each and every project I create. How would I do that?

Or am I on the wrong bank of the river? Probably I am missing something here (I'm still grasping my way around the whole thing, as my background is legacy VB, but not been coding for at least ten years... - shame on me!)

As you can see, this whole idea of OOP is quite new to me (well, not new all together, but I have to think in a much diferent way than in the "old days").

Well, I'm trying to figure out just the basics like "why isn't this working - oh, I must include something in my project! OK! But what? Where is that function defined? How do I find that? And so on... If you know what I mean...

So, I am to be expected to ask some quite silly questions in the near future, so bear with me.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: TextBox doesn't show characters

Posted 26 January 2012 - 06:05 AM

Try this....

Make your new solution such as "mySpecialGame"
From the Solution pallet, add... existing project...
Now browse to your library project such as "myCommonStuff"

Add a reference to 'myCommonStuff' inside "mySpecialGame".

Now you can use all your common stuff in you game.


Keep doing this through your next 20 projects because it will allow you to grow your common library easily. When it has grown to the point where you haven't added to it for 6 months you might consider it 'done' enough to make a dll out of it.
Was This Post Helpful? 0
  • +
  • -

#12 Zigon   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: TextBox doesn't show characters

Posted 26 January 2012 - 07:02 AM

Good idea! I'll try that...
Just one question, if I may: What is the scope (visibility) of this approach? Would procedures be visible/ accesible throuhg my entire project/ solution or do I have to make then somehow Public/ Global?
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: TextBox doesn't show characters

Posted 26 January 2012 - 07:43 AM

Accessibility is going to be what you make of it for each method.

If you make something public, then it is public.

But basically everything in your library has the potential to be available to all the projects within the solution. I do this all the time. I wouldn't have suggested it if it didn't work for me every day.

From here I would suggest you just do it. Trial and error is the greatest teacher. You will learn more if you build a solution and do it than by trying to visualize it in the abstract.

Some of these concepts are in these two tutorials of mine if you want to see it in practice:

Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 0
  • +
  • -

#14 Zigon   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: TextBox doesn't show characters

Posted 26 January 2012 - 08:04 AM

OK, thanks! I'll give it a go.
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades   User is online

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,015
  • Joined: 23-August 08

Re: TextBox doesn't show characters

Posted 12 November 2012 - 04:18 AM

Closing this topic as it appears to have become a spam magnet for an unscrupulous software company.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1