Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    1
    ReputationRep:
    Hey, so I'm trying to make a windows application with scrolling text but every time I hit a button it freezes and I can't click anything else. I have a form with a label which isn't visible until you hit 'Start', and I have 2 buttons, 'Start' and 'Stop'. Here's my code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace TextScroll
    {
         public partial class Form1 : Form
         {
              public delegate void ScrollDelegate();
              private ScrollDelegate s_del;
    
              public Form1
              {
                   InitializeComponent();
              }
    
              private void Form1_Load(object sender, EventArgs e)
              {
                   s_del = new ScrollDelegate(ScrollText);
                   btn_stop.Enabled = false;
              }
    
              private void ScrollText()
              {
              }
    
              private void btn_start_Click(object sender, EventArgs e)
              {
                   btn_start.Enabled = false;
                   btn_stop.Enabled = true;
                   System.Text.StringBuilder sb = new System.Text.StringBuilder("Test String" + " ");
                   while (true)
                   {
                        char ch = sb[sb.Length - 1];
                        sb.Remove(sb.Length-1, 1);
                        sb.Insert(0, ch);
                        label1.Text = sb.ToString();
                        label1.Refresh();
                        System.Threading.Thread.Sleep(120);
                   }
                   s_del.BeginInvoke(null, null);
              }
    
              private void btn_stop_Click(object sender, EventArgs e)
              {
                   s_del.EndInvoke(null);
                   btn_stop.Enabled = false;
                   btn_start.Enabled = true;
              }
         }
    }
    Offline

    17
    ReputationRep:
    While not knowing anything about C#, I suspect the freeze is going to be due to the infinite loop in your 'btn_start_Click' method.
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by DFranklin)
    While not knowing anything about C#, I suspect the freeze is going to be due to the infinite loop in your 'btn_start_Click' method.
    Yes, I imagine you're right, but I don't know what to put instead because that's the bit that makes it scroll...
    Offline

    17
    ReputationRep:
    I don't know if C# gives you easier ways of doing this, but typically you'd set up an event timer so you get an event every second, and then when you get the event, you scroll the text 1 position.
    • PS Helper
    Offline

    14
    PS Helper
    (Original post by Mcjazz)
    Yes, I imagine you're right, but I don't know what to put instead because that's the bit that makes it scroll...
    If you have a while loop, it executes until whatever's inside the brackets becomes false. If you have while(true), then it's independent of any variables, so what's inside the bracket is always true and cannot be modified by anything inside the loop, so it goes on forever. I don't know anything about C# either, but it looks like you need something like
    Code:
    while(btn_stop.Enabled == true) {
        //
        // Whatever code goes here
        //
    
        //
        // Something which detects if the button should become disabled
        // and changes the value of btn_stop.Enabled to "false"
        //
    }
    Offline

    9
    ReputationRep:
    (Original post by nuodai)
    If you have a while loop, it executes until whatever's inside the brackets becomes false. If you have while(true), then it's independent of any variables, so what's inside the bracket is always true and cannot be modified by anything inside the loop, so it goes on forever. I don't know anything about C# either, but it looks like you need something like
    Code:
    while(btn_stop.Enabled == true) {
        //
        // Whatever code goes here
        //
    
        //
        // Something which detects if the button should become disabled
        // and changes the value of btn_stop.Enabled to "false"
        //
    }
    this.

    Also, add Application.DoEvents() in your loop to stop it freezing, it processes all the messages sent to your form (e.g. button clicks).
    Offline

    2
    ReputationRep:
    Isn't there a .NET timer control for jobs like this?
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by Phil2008)
    this.

    Also, add Application.DoEvents() in your loop to stop it freezing, it processes all the messages sent to your form (e.g. button clicks).
    Great, the Application.DoEvents stopped it freezing

    Thanks everyone I'll take it into account
    Offline

    1
    ReputationRep:
    Yes. There is a timer and that would be the most sensible way of doing this. Application.DoEvents() and the Thread.Sleep() calls are pure abuse.
    Offline

    1
    ReputationRep:
    Using a timer would be bad practise, as would directly manipulating the only thread you're working on. It might be a bit advanced, as it's a weird concept to wrap your head around, but threading your app so that things like user input and the interface are handled on a separate thread, and the work is handled on another, is the common way to multi-task. The BackgroundWorker class/control in C# is basically an automated way of doing this, but with threads you can have an upper bound of 256 for most PCs, which is better than having 255 BackgoundWorker controls, and hence causing memory usage to go through the roof :rolleyes:
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Brussels sprouts
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.