scrolling text in C#

Hey Can you heip me out??
I am making an application where I want to display scrolling text something like marquee.How do I do it??It has to be done in C#

Comments

  • : Hey Can you heip me out??
    : I am making an application where I want to display scrolling text something like marquee.How do I do it??It has to be done in C#
    :

    Create a user control and use drawstring to draw the string. You can use a timer and expose the interval to set a scroll rate.
  • : Hey Can you heip me out??
    : I am making an application where I want to display scrolling text something like marquee.How do I do it??It has to be done in C#
    :

    Here is what I came up with:

    This is the code for the control.
    [code]
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;

    namespace Marquee
    {
    ///
    /// Summary description for UserControl1.
    ///
    public class Marquee : System.Windows.Forms.UserControl
    {
    private System.ComponentModel.IContainer components;
    private System.Windows.Forms.Timer timer1;

    private int currentPos = 0;
    private bool mBorder;
    private string mText;

    public string MarqueeText{
    get{return mText; }
    set{mText = value; }
    }

    public bool Border{
    get{ return mBorder; }
    set{ mBorder = value; }
    }

    public int Interval{
    get{ return timer1.Interval*10; }
    set{ timer1.Interval = value/10; }
    }


    public Marquee()
    {
    // This call is required by the Windows.Forms Form Designer.
    InitializeComponent();

    // TODO: Add any initialization after the InitForm call
    this.Size = new Size(this.Width, this.Font.Height);
    }

    ///
    /// Clean up any resources being used.
    ///
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if( components != null )
    components.Dispose();
    }
    base.Dispose( disposing );
    }

    #region Component Designer generated code
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    //
    // timer1
    //
    this.timer1.Enabled = true;
    this.timer1.Interval = 1000;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    //
    // Marquee
    //
    this.Name = "Marquee";
    this.Size = new System.Drawing.Size(150, 136);
    this.Resize += new System.EventHandler(this.Marquee_Resize);

    }
    #endregion

    private void Marquee_Resize(object sender, System.EventArgs e) {
    this.Height = this.Font.Height;
    }

    private void timer1_Tick(object sender, System.EventArgs e) {
    this.Invalidate();
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {

    if(mBorder){
    e.Graphics.DrawRectangle(new Pen(this.ForeColor),0,0,this.Width-1, this.Height-1);
    }

    e.Graphics.DrawString(mText, this.Font, new SolidBrush(this.ForeColor),
    currentPos, 0);
    e.Graphics.DrawString(mText, this.Font, new SolidBrush(this.ForeColor),
    this.Width+currentPos, 0);

    currentPos--;

    if((currentPos<0)&&(Math.Abs(currentPos)>= e.Graphics.MeasureString(mText,this.Font).Width))
    currentPos = this.Width +currentPos;
    }
    }
    }
    [/code]

    This is the code for the test project.
    [code]
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace MarqueeTest
    {
    ///
    /// Summary description for Form1.
    ///
    public class Form1 : System.Windows.Forms.Form
    {
    private Marquee.Marquee marquee1;
    ///
    /// Required designer variable.
    ///
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }

    ///
    /// Clean up any resources being used.
    ///
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.marquee1 = new Marquee.Marquee();
    this.SuspendLayout();
    //
    // marquee1
    //
    this.marquee1.Border = true;
    this.marquee1.Interval = 500;
    this.marquee1.Location = new System.Drawing.Point(24, 64);
    this.marquee1.MarqueeText = "This is what I want to scroll";
    this.marquee1.Name = "marquee1";
    this.marquee1.Size = new System.Drawing.Size(184, 13);
    this.marquee1.TabIndex = 0;
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.marquee1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);

    }
    #endregion

    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e) {
    marquee1.Text = "This is the text";
    marquee1.Interval = 200;
    marquee1.Border = true;
    }
    }
    }
    [/code]

    Just create a new project of type windows control library and add the code from the first snippet that is not there. Than add a new windows application project, set it as the startup project and add the code from the second snippet.

    Voila! a scrolling control!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion