How to use array of labels colors and text?

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

    How to use array of labels colors and text?

    I did that the label1 is scrolling moving from down bottom to up top and back again in a loop.
    So in form1 i can give any text i want to be show and any color to color the text in.
    Now instead one label i want that therw will be maximum 5labels running one after one each one with another text and another color.
    So i created array of labels but what now?
    This is the code so far using one label. I want to delete the current label1 from the control designer and instead using the array of labels i created.


    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 NewsFeederControl : UserControl
        {
            string[] newsTF;
            Label[] labelarray;
            int jump = 0;
            string newsTextFeed;
            Color TextColor;
            public NewsFeederControl()
            {
                InitializeComponent();
                labelarray = new Label[5];
                for (int i = 1; i < 5; i++)
                {
                    //intialize new label
                    labelarray[i] = new Label();
                    labelarray[i].Size = new System.Drawing.Size(33, 33);
                    labelarray[i].Location = new System.Drawing.Point(i, i);
                    labelarray[i].Text = i.ToString();
                    this.Controls.AddRange(new System.Windows.Forms.Control[] {labelarray[i]});
                }
                TextColor = new Color();
            }
    
            private void NewsFeederControl_Load(object sender, EventArgs e)
            {
    
            }
    
            private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
            {
                startCustom();
                
            }
    
            public void newsTextFeeds(string newsText)
            {
                newsTextFeed = newsText;
            }
    
            public void newsTextColor(Color color)
            {
                TextColor = color;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                label1.Location = new Point(label1.Location.X, label1.Location.Y - jump);
            }
    
            public void startCustom()
            {
                label1.Text = newsTextFeed;
                label1.ForeColor = TextColor;
                timer1.Interval = 50;
                jump = 1;
                timer1.Start();
    
            }
    
            private void label1_LocationChanged(object sender, EventArgs e)
            {
                if (label1.Location.Y <= -20)
                {
                    label1.Location = new Point(label1.Location.X, this.Height + label1.Height);
                }
            }
    
    
        }
    }

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

    #2
    firstly change all references of label1 to labelarray[0].

    Resize your control so that its height + 40 is approximately divisible by 5 and try to make it at least 200 tall.

    In the tick event write this code.

    Code:
    for (int i = 1; i < 5; i++)
    {
    labelarray[i].Location = new Point(labelarray[i].Location.X, labelarray[i].Location.Y - jump);
    
    if (labelarray[i].Location.Y <= -20)
    labelarray[i].Location = new Point(labelarray[i].Loaction.X, this.Height + labelarray[i].Height);
    }
    And in the startCustom() event, add this:

    Code:
    int x = 20
    int y = (int)(this.Height/2)  //In the middle
    for (int i = 1; i < 5; i++)
    {
    labelarray[i].Location = new Point(x,y + 40*i);
    }
    Hope this helps (and works! I just wrote it freehand :D)

    Sam

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      Samuel i tried it i got error in the designer i see box X in Red inside the control. Something i did wrong.
      Here is my code now can you please fix it and show me the complete code how it should be?

      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 NewsFeederControl : UserControl
          {
              string[] newsTF;
              Label[] labelarray;
              int jump = 0;
              string newsTextFeed;
              Color TextColor;
              public NewsFeederControl()
              {
                  InitializeComponent();
                  labelarray = new Label[5];
                  TextColor = new Color();
              }
      
              private void NewsFeederControl_Load(object sender, EventArgs e)
              {
      
              }
      
              private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
              {
                  startCustom();
                  
              }
      
              public void newsTextFeeds(string newsText)
              {
                  newsTextFeed = newsText;
              }
      
              public void newsTextColor(Color color)
              {
                  TextColor = color;
              }
      
              private void timer1_Tick(object sender, EventArgs e)
              {
                  // label1.Location = new Point(label1.Location.X, label1.Location.Y - jump);
                  for (int i = 1; i < 5; i++)
                  {
                      labelarray[i].Location = new Point(labelarray[i].Location.X, labelarray[i].Location.Y - jump);
                      if (labelarray[i].Location.Y <= -20)
                      {
                          labelarray[i].Location = new Point(labelarray[i].Location.X, this.Height + labelarray[i].Height);
                      }
                  }
              }
      
              public void startCustom()
              {
                  int x = 20;
                  int y = (int)(this.Height / 2);  //In the middle 
                  for (int i = 1; i < 5; i++)
                  {
                      labelarray[i].Location = new Point(x, y + 40 * i);
      
                      labelarray[i].Text = newsTextFeed;
                      labelarray[i].ForeColor = TextColor;
                  }
                  timer1.Interval = 50;
                  jump = 1;
                  timer1.Start();
      
              }
      
              private void label1_LocationChanged(object sender, EventArgs e)
              {
                /*  if (labelarray[i].Location.Y <= -20)
                  {
                      labelarray[].Location = new Point(label1.Location.X, this.Height + label1.Height);
                  }*/
              }
      
      
      
          }
      }

      Thanks.

      Comment

      • Samuel Jones
        New Member
        • Jan 2011
        • 48

        #4
        Can you attach the file? id rather not have to spend another 40min recreating your project :D

        Comment

        • Samuel Jones
          New Member
          • Jan 2011
          • 48

          #5
          Or could you copy out the error and tell me what it says?

          Comment

          • Chocolade
            New Member
            • Aug 2010
            • 69

            #6
            Samuel here is the file i have uploaded tried using the array and i did a mess.
            Please post the whole code again fixed.

            Thanks a lot.

            Comment

            • Chocolade
              New Member
              • Aug 2010
              • 69

              #7
              Thorsten i tried this code:

              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 NewsFeederControl : UserControl
                  {
                      List<Label> list;
                      int jump = 0;
                      string newsTextFeed;
                      Color TextColor;
                      public NewsFeederControl()
                      {
                          InitializeComponent();
                          list = new List<Label>(5);
                          TextColor = new Color();
                      }
              
                      private void NewsFeederControl_Load(object sender, EventArgs e)
                      {
              
                      }
              
                      private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
                      {
                          startCustom();
              
                      }
              
                      public void newsTextFeeds(string newsText)
                      {
                          newsTextFeed = newsText;
                      }
              
                      public void newsTextColor(Color color)
                      {
                          TextColor = color;
                      }
              
                      private void timer1_Tick(object sender, EventArgs e)
                      {
                          for (int i = 1; i < 5; i++)
                          // Loop through List with for
                          {
                              list[i].Location = new Point(list[i].Location.X, list[i].Location.Y - jump);
                              if (list[i].Location.Y <= -20)
                              {
                                  list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height);
                              }
                          }
              
                      }
              
                      public void startCustom()
                      {
                          int x = 2;
                          int y = (int)(this.Height / 2);  //In the middle 
                              for (int i = 1; i < 5; i++)
                              {
                                  list[i].Location = new Point(x,y+40*i); 
                              }
                              timer1.Interval = 50;
                              jump = 1;
                              timer1.Start();
                          
              
                      }
              
                      private void label1_LocationChanged(object sender, EventArgs e)
                      {
                         /*   if (list[i].Location.Y <= -20) 
                            { 
                                list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height); 
                            }*/
                      }
              
              
              
                  }
              }


              But when i make Built solution and then going to the form1 designer before im running the program im getting on the control inside the control in the form1 designer exception say on the control code in line 66 that the index was out of range must be none negative and less then the size of the collection paramet name : index.



              The line (66) is : list[i].Location = new Point(x,y+40*i) ;


              And in general im not sure the code is good at all.



              Thanks.

              Comment

              • Chocolade
                New Member
                • Aug 2010
                • 69

                #8
                Samuel thats the complete exception message im getting but again im sure the whole array code isnt good:

                13/01/2011 22:36:34 ==> Newsfeeder Exception: System.Argument OutOfRangeExcep tion: Index was out of range. Must be non-negative and less than the size of the collection.
                Parameter name: index
                at System.Collecti ons.Generic.Lis t`1.get_Item(In t32 index)
                at WindowsFormsApp lication1.NewsF eederControl.st artCustom() in D:\C-Sharp\Download File\Downloadin g-File-Project-Version-011\Downloading File\Downloadin g File\NewsFeeder Control.cs:line 69
                13/01/2011 22:36:47 ==> Last Downloaded file is :C:\Users\Choco lade\AppData\Lo cal\WindowsForm sApplication1\M y Weather Station\satelli te\satellite817 .jpg

                Comment

                • Samuel Jones
                  New Member
                  • Jan 2011
                  • 48

                  #9
                  Combine these two together
                  Code:
                  List<Label> list;
                  list = new List<Label>(5);
                  to make this
                  Code:
                  Label[] list = new Label[5];
                  Note: if you want to pragmatically change the size, change each line to this:
                  Code:
                  Label[] list;
                  
                  //Put in a public method ie. public void changeSize(int size);
                  list = new Label[size];
                  And change your for loops to this:
                  Code:
                  for (int i = 0; i < 5; i++)
                  note 'int i = 0' instead of 'int i = 1', this is because an arrays first reference is at 0.
                  Last edited by Samuel Jones; Jan 14 '11, 07:18 AM. Reason: Corrected code

                  Comment

                  • Samuel Jones
                    New Member
                    • Jan 2011
                    • 48

                    #10
                    Also note that this forum supports attaching files to posts

                    under post box
                    select 'Go advanced'
                    select 'Manage Attachments'
                    you can upload projects in .zip format

                    Comment

                    • Christian Binder
                      Recognized Expert New Member
                      • Jan 2008
                      • 218

                      #11
                      I'd also suggest to use for-each instead of for when iterating through a collection (list, array, dictionary, ...)

                      Code:
                      private void timer1_Tick(object sender, EventArgs e)
                      {
                        foreach (Label label in list)
                        // Loop through List with for-each
                        {
                          label.Location = new Point(label.Location.X, label.Location.Y - jump);
                          if (label.Location.Y <= -20)
                          {
                            label.Location = new Point(label.Location.X, this.Height + label.Height);
                          }
                        }
                      }

                      Comment

                      • Chocolade
                        New Member
                        • Aug 2010
                        • 69

                        #12
                        Samuel now im getting error exception null:

                        This is the code now i changed as you wrote and im getting the exception inside the startCustom() function wich is calling in the paint event in the control.

                        Here is the complete code now:

                        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;
                        using DannyGeneral;
                        
                        namespace WindowsFormsApplication1
                        {
                            public partial class NewsFeederControl : UserControl
                            {
                                Label[] list = new Label[5];
                                int jump = 0;
                                string newsTextFeed;
                                Color TextColor;
                                public NewsFeederControl()
                                {
                                    InitializeComponent();
                                    TextColor = new Color();
                                }
                        
                                private void NewsFeederControl_Load(object sender, EventArgs e)
                                {
                        
                                }
                        
                                private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
                                {
                                    startCustom();
                        
                                }
                        
                                public void newsTextFeeds(string newsText)
                                {
                                    newsTextFeed = newsText;
                                }
                        
                                public void newsTextColor(Color color)
                                {
                                    TextColor = color;
                                }
                        
                                private void timer1_Tick(object sender, EventArgs e)
                                {
                                    for (int i = 0; i < 5; i++)
                                    // Loop through List with for 
                                    {
                                        list[i].Location = new Point(list[i].Location.X, list[i].Location.Y - jump);
                                        if (list[i].Location.Y <= -20)
                                        {
                                            list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height);
                                        }
                                    }
                        
                                }
                        
                                public void startCustom()
                                {
                                    try
                                    {
                                        int x = 2;
                                        int y = (int)(this.Height / 2);  //In the middle  
                                        for (int i = 1; i < 5; i++)
                                        {
                                            list[i].Location = new Point(x, y + 40 * i);
                                        }
                                        timer1.Interval = 50;
                                        jump = 1;
                                        timer1.Start();
                                    }
                                    catch (Exception err)
                                    {
                                        Logger.Write("NewsFeeder Error: " + err);
                                    }
                        
                        
                                }
                        
                                private void label1_LocationChanged(object sender, EventArgs e)
                                {
                                    /*   if (list[i].Location.Y <= -20)  
                                       {  
                                           list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height);  
                                       }*/
                                }
                        
                        
                        
                            }
                        }
                        Note that im not using anymore the label1 changed event ill remove it later.


                        The null error is on line 65 means this line:

                        Code:
                        list[i].Location = new Point(x, y + 40 * i);
                        wich is in the startCustom() function.

                        Thats the complete exception error:

                        14/01/2011 10:23:52 ==> NewsFeeder Error: System.NullRefe renceException: Object reference not set to an instance of an object.
                        at WindowsFormsApp lication1.NewsF eederControl.st artCustom() in D:\C-Sharp\Download File\Downloadin g-File-Project-Version-011\Downloading File\Downloadin g File\NewsFeeder Control.cs:line 68
                        14/01/2011 10:23:55 ==> NewsFeeder Error: System.NullRefe renceException: Object reference not set to an instance of an object.
                        at WindowsFormsApp lication1.NewsF eederControl.st artCustom() in D:\C-Sharp\Download File\Downloadin g-File-Project-Version-011\Downloading File\Downloadin g File\NewsFeeder Control.cs:line 68


                        Ill upload the file in few if you need it again.
                        Thanks.

                        Comment

                        • Christian Binder
                          Recognized Expert New Member
                          • Jan 2008
                          • 218

                          #13
                          You have to assign your labels to the list before you access them.
                          If you create them with the designer, you have to assign them by name, e.g. in method startCustom
                          Code:
                          list[1] = label1;
                          list[2] = label2;
                          ...
                          or you create them if you don't use the designer.
                          Code:
                          for(int i = 1; i < 5; i++)
                            list[i] = new Label();

                          Comment

                          • Chocolade
                            New Member
                            • Aug 2010
                            • 69

                            #14
                            Samuel now im not getting exceptions but i dont see anything moving inside the control when im running my application.

                            This is the complete code now please review it:

                            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;
                            using DannyGeneral;
                            
                            namespace WindowsFormsApplication1
                            {
                                public partial class NewsFeederControl : UserControl
                                {
                                    Label[] list = new Label[5];
                                    int jump = 0;
                                    string newsTextFeed;
                                    Color TextColor;
                                    public NewsFeederControl()
                                    {
                                        InitializeComponent();
                                        TextColor = new Color();
                                        
                                    }
                            
                                    private void NewsFeederControl_Load(object sender, EventArgs e)
                                    {
                            
                                    }
                            
                                    private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
                                    {
                                        startCustom();
                            
                                    }
                            
                                    public void newsTextFeeds(string newsText)
                                    {
                                        newsTextFeed = newsText;
                                    }
                            
                                    public void newsTextColor(Color color)
                                    {
                                        TextColor = color;
                                    }
                            
                                    private void timer1_Tick(object,sender,EventArgse)
                                    {
                                      foreach (Label label in list)
                                        // Loop through List with for-each 
                                        {
                                            label.Location = new Point(label.Location.X, label.Location.Y - jump);
                                            if (label.Location.Y <= -20)
                                            {
                                                label.Location = new Point(label.Location.X, this.Height + label.Height);
                                            }
                                        } 
                                    }
                            
                                    public void startCustom()
                                    {
                                        try
                                        {
                                            for (int i = 0; i < 5; i++)
                                            {
                                                list[i] = new Label();
                                            }
                                            int x = 2;
                                            int y = (int)(this.Height / 2);  //In the middle   
                                            for (int i = 1; i < 5; i++)
                                            {
                                                list[i].Location = new Point(x, y + 40 * i);
                                            }
                                            list[1].Text = newsTextFeed;
                                            list[1].ForeColor = TextColor;
                                            timer1.Interval = 50;
                                            jump = 1;
                                            timer1.Start();
                                        }
                                        catch (Exception err)
                                        {
                                            Logger.Write("NewsFeeder Error: " + err);
                                        }
                                    }
                                }
                            }
                            I changed it to foreach.

                            and in the form1 im sending the text and color by:

                            Code:
                            newsFeederControl1.newsTextFeeds("Hello hi");
                                        newsFeederControl1.newsTextColor(Color.Red);
                            But i dont see anything in to control moving.

                            Comment

                            • Samuel Jones
                              New Member
                              • Jan 2011
                              • 48

                              #15
                              To start, when i recreated the control, paint was never called, so it never started startCustom();

                              To make a note as well, try to comment your code as much as possible. It helps us and yourself figure out what the code does.

                              I made the following code on an empty user control with a timer named timer1, with a size of 200 by 200.

                              Here is your code with modifications which worked on my computer.

                              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 OnlineHelpScroller
                              {
                                  public partial class NewsFeederControl : UserControl
                                  {
                                      Label[] list = new Label[5];
                                      int jump = 0;
                                      public string newsTextFeed = null;
                                      public Color TextColor = new Color();
                              
                                      /*
                                       *  To set text to display call:
                                       *      *NewsFeederControl name*.newsTextFeed = "somestring";
                                       *  To set color of text call:
                                       *      *NewsFeederControl name*.newsTextFeed = Color.*somecolor*
                                       *      
                                       */
                              
                                      public NewsFeederControl()
                                      {
                                          InitializeComponent();
                                      }
                               
                                      private void timer1_Tick(object sender, EventArgs e)
                                      {
                                          foreach (Label label in list)
                                          // Loop through List with for-each 
                                          {
                                              // Move label up 'jump' pixels
                                              label.Location = new Point(label.Location.X, label.Location.Y - jump);
                              
                                              // Check to see if out of view.
                                              if (label.Location.Y <= -20)
                                              {
                                                  label.Location = new Point(label.Location.X, this.Height);
                                              }
                                          } 
                                      }
                               
                                      public void startFeed()
                                      {
                                          try
                                          {
                                              if (newsTextFeed != null && TextColor != Color.Empty)
                                              {
                                                  // Initialise Position Variables
                                                  int x = 2;
                                                  int y = this.Height;  // just out of view at top.
                              
                                                  // Assign each label in list the following properties
                                                  for (int i = 0; i < 5; i++)
                                                  {
                                                      list[i] = new Label();
                                                      list[i].Location = new Point(x, y + (40 * i));
                                                      list[i].Text = newsTextFeed;
                                                      list[i].ForeColor = TextColor;
                                                      this.Controls.Add(list[i]); // Add control to UserControl
                                                  }
                              
                                                  // Set movement variables
                                                  timer1.Interval = 50;
                                                  jump = 1;
                              
                                                  // Start Timer
                                                  timer1.Start();
                                              }
                                              else if (newsTextFeed == null)
                                                  MessageBox.Show("newsTextFeed is null");
                                              else if (TextColor == Color.Empty)
                                                  MessageBox.Show("TextColor not defined");
                                          }
                                          catch (Exception err)
                                          {
                                              MessageBox.Show("NewsFeeder Error: " + err);
                                          }
                                      }
                              
                                      public void changeFeed()
                                      {
                                          try
                                          {
                                              if (newsTextFeed != null && TextColor != Color.Empty)
                                              {
                                                  // Assign each label in list the following properties
                                                  for (int i = 0; i < 5; i++)
                                                  {
                                                      list[i].Text = newsTextFeed;
                                                      list[i].ForeColor = TextColor;
                                                  }
                                              }
                                              else if (newsTextFeed == null)
                                                  MessageBox.Show("newsTextFeed is null");
                                              else if (TextColor == Color.Empty)
                                                  MessageBox.Show("TextColor not defined");
                                          }
                                          catch (Exception err)
                                          {
                                              MessageBox.Show("NewsFeeder Error: " + err);
                                          }
                                      }
                                  }
                              }
                              Call this code in your main form ONLY ONCE to start it.
                              (I put it on a button, but will work well within main form's load event.)
                              Code:
                                          newsFeederControl1.newsTextFeed = "somestring";
                                          newsFeederControl1.TextColor = Color.Red;
                                          newsFeederControl1.startFeed();
                              To change the settings, use this code
                              Code:
                                          newsFeederControl1.newsTextFeed = "otherstring";
                                          newsFeederControl1.TextColor = Color.Blue;
                                          newsFeederControl1.changeFeed();
                              My suggestion is to now make newsTextFeed and TextColor arrays so each item has its own settings.

                              Comment

                              Working...