How do i make the text inside the control to move up like news feeder?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    How do i make the text inside the control to move up like news feeder?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class NewsControl : UserControl
        {
            String drawString;
            public NewsControl()
            {
                InitializeComponent();
            }
    
            private void NewsControl_Paint(object sender, PaintEventArgs e)
            {
                // Create string to draw.
                getText(drawString);
                // Create font and brush.
                Font drawFont = new Font("Arial", 8);
                SolidBrush drawBrush = new SolidBrush(Color.Black);
                // Create point for upper-left corner of drawing.
                float x = 130.0F;
                float y = 170.0F;
                // Set format of string.
                StringFormat drawFormat = new StringFormat();
                drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                // Draw string to screen.
                e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
            }
    
            public void getText(string getText)
            {
                drawString = getText;
            }
        }
    }

    This just show any text i enter in Form1 for example in form1 constructor i did:



    newsControl1.ge tText("Hello");

    And when im running my program i see Hello.

    But now i want using For loop and timer i want that the word Hello to move inside the control up and go away up then return from down bottom again and foinf up again like a loop and each i add more text it will make new line and the seocnd line will go up after the first one and so on.

    And i want it will show the control box borders i mean now i just see a text i need to see some borders box or something so the text wont look like it hang in the air.



    You can see here on the right top as the page is loading how it should be how the text is moving up: http://www.ynet.co.il/home/0,7340,L-8,00.html



    Thanks for help.
  • Samuel Jones
    New Member
    • Jan 2011
    • 48

    #2
    How similar are you trying to make it in comparison to the webpage? are you trying the little rotation as well?

    I have managed to recreate your project but i need a little more insight.

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      Samuel it dosent have to be 100% like the it is in the webpage but the same idea.
      The webpage was just example.
      Ill make the frame around the text later and colors but the movement should be the same from down to up in a loop.

      Thanks.

      Comment

      • Samuel Jones
        New Member
        • Jan 2011
        • 48

        #4
        well if that is the case, can't you just use a moving label?

        Reasons why.
        1. Labels have borders
        2. Labels are easy to change color
        3. Labels can have multiple lines
        4. Labels can be shifted easily

        Comment

        • Chocolade
          New Member
          • Aug 2010
          • 69

          #5
          Samuel ok then with label that a good idea.

          Comment

          • Samuel Jones
            New Member
            • Jan 2011
            • 48

            #6
            To do this, create a control (150x150 in size), add a label (called label1) roughly in the middle.

            Try this for a solution:

            Code:
            public partial class customControl : UserControl
                {
                    int jump = 0;
            
                    public customControl()
                    {
                        InitializeComponent();
                    }
            
                    public void startCustom()
                    {
                        timer1.Interval = 50;  // Timer Speed
                        jump = 10;             // Gap in pixels
            
                        timer1.Start();
                    }
            
                    private void timer1_Tick(object sender, EventArgs e)
                    {
                        label1.Location = new Point(label1.Location.X, label1.Location.Y - jump);
                    }
            
                    private void label1_LocationChanged(object sender, EventArgs e)
                    {
                        if (label1.Location.Y <= -20)
                            label1.Location = new Point(label1.Location.X, this.Height + label1.Height);
                    }
                }

            To use: call startCustom();

            Comment

            • Chocolade
              New Member
              • Aug 2010
              • 69

              #7
              Samuel Working great.

              Two questions please:

              1. Not so critical now but lets say i wanted to use instead of label1 just paint event inside the control and write the text directly to the control with drawstring for example:

              Code:
              [C#] 
              public void DrawStringFloatFormat(PaintEventArgs e)
              {
              // Create string to draw.
              String drawString = "Sample Text";
              // Create font and brush.
              Font drawFont = new Font("Arial", 16);
              SolidBrush drawBrush = new SolidBrush(Color.Black);
              // Create point for upper-left corner of drawing.
              float x = 150.0F;
              float y =  50.0F;
              // Set format of string.
              StringFormat drawFormat = new StringFormat();
              drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
              // Draw string to screen.
              e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
              }
              So how can i use also timer to make this text to move like you did with the label? I mean instead the label using this.


              2. I want to make the label now o move slowly up so i cahgned this variables values:

              Code:
              timer1.Interval = 50;
              jump = 1;
              So jump = 1; is the lowest i can get and its moving good but i think there are still some flickering i mean very small small jumps. Its not that much a problem to the eye but lets say i wanted to make it much smoother in this speed?


              Thanks for helping.

              Comment

              • Samuel Jones
                New Member
                • Jan 2011
                • 48

                #8
                The problem that i could see with the original method was that you are redrawing the same object so many times per second from scratch. Which is why it flickers.

                btw... if you change the timer's interval you can make it even slower...

                But it is easy to adapt my code to your solution.

                move your "float y = " outside of that method.
                adjust the timer to read this:

                Code:
                private void timer1_Tick(object sender, EventArgs e)
                        {
                            y = y - jump;
                            if (y< = -20)
                                y = this.Height + 20;
                            Refresh();
                        }

                Comment

                Working...