Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scrolling Text 1

Status
Not open for further replies.

NewFromMattel

Programmer
Jun 24, 2003
41
US
All,

I am working on a project where I need a windows executable that will put a window on the screen that stays on top, and has scrolling text.

This text is retreived from a server, and is updated at a user specified interval. I have figured out how to do this with the exception of the scrolling text. Coming from the web development world, this is odd. There is an HTML marquee tag that can accomplish this.

I have googled this problem of scrolling text, and found a couple of prospects, one of which uses a Graphics object, and draws some text in a rectangle. However the width of the drawn text area is dependent on the length of the measured string. That won't do. I need the text to scroll in from the right side of the screen, and go all the way to the left side of the screen irrespective of the length of the string.

Any ideas?

TIA
Marv
 
Try the followings;
- pad left the string with a number of spaces depending on the textbox width
- use two Timer objects to control the string display
- one timer objects to display the text by removing one char at the time
- when the end of the string is reached, stop the first timer and enable the second timer
- the second timer starts the first timer
The following code should work.
Code:
public class Form1 : System.Windows.Forms.Form
{
	private int iCounter;
	private string sMsg="Hello World!";
	private System.Windows.Forms.TextBox textBox1;
	private System.Windows.Forms.Timer timer1;
	private System.Windows.Forms.Timer timer2;
	public Form1()
	{
		InitializeComponent();
	 	PadMsg();

	}
	private void PadMsg()
	{
	iCounter=0;
		sMsg = sMsg.PadLeft((int)(this.textBox1.Width/(int)(this.textBox1.Font.SizeInPoints) * 3.25),' ');
	}
	private void InitializeComponent()
	{
		
		// 
		this.timer1.Enabled = true;
		this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
		// 
		this.timer2.Enabled = false;
		this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
		//
	}
	private void timer1_Tick(object sender, System.EventArgs e)
	{
			textBox1.Text=sMsg.Substring(iCounter,sMsg.Length-iCounter);
		    iCounter++;
		if (iCounter==sMsg.Length)
		{
			this.timer1.Enabled = false;
			this.timer2.Enabled = true;
			iCounter=0;
		}
	}

	private void timer2_Tick(object sender, System.EventArgs e)
	{
		this.timer1.Enabled = true;
		this.timer2.Enabled = false;
	}
}
obislavu
 
Don't I need to assign a value to the timers? I'm getting an error when I try to compile "'DefaultNamespace.MainForm.timer1' is never assigned to, and will always have it's default value null" Same for timer2.

Thanks

Oh, and I think I follow your method here. Looks good.
 
I was thinking you create the two instances at the // place.
So add the followings in the InitializeComponent() where you should have other things related to the form:
Code:
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();
obislavu
 
That was it! I really appreciate all your help! And thanks for your patience with thatlast inane question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top