How to use array of labels colors and text?

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

    #16
    Samuel working great!
    Could you show me how to make the text and the color array? And how to call them then in form1?

    Now its moving same text same color wich is good.
    But lets say in the labels array i have 5 labels so i want to see in the control when im running the program eahc label with different text and different color.

    Thank you very much!

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #17
      Samuel another thing i saw now that between the first label and the other 4 there is a space. All the other 4 labels same spaces the first one come before the else.
      Did you mean to do it like that?
      And where in the code do i change/play with the spaces?

      Thanks.

      Comment

      • Samuel Jones
        New Member
        • Jan 2011
        • 48

        #18
        Underneath the 'int jump = 0' line add this
        Code:
        int space = 40
        Change that number to change the space, currently in my code i have it set to 40 in this line:
        Code:
        list[i].Location = new Point(x, y + (40 * i));
        Change it to:
        Code:
        list[i].Location = new Point(x, y + (space * i));
        That should do it.

        Try not to make space smaller than about 20, or the labels will overlap.

        Comment

        • Chocolade
          New Member
          • Aug 2010
          • 69

          #19
          Samuel this is what i did now to get the text from the Form1 to the labels and i have some questions please:

          First the code in the control i changed:

          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
              {
                  Label[] list = new Label[5];
                  int jump = 0;
                  public string[] newsTextFeed = null;
                  public Color[] TextColor;
                  /* 
                   *  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 NewsFeederControl_Load(object sender, EventArgs e)
                  {
          
                  }
          
                  private void timer1_Tick(object sender, EventArgs e)
                  {
                      if (list.Length > 4 && newsTextFeed != null)
                      {
                          //get the label at this position in the list
                          //and assign some text
                          if (list[0] != null && this.newsTextFeed[0] != null && newsTextFeed.Length > 0)
                              list[0].Text = this.newsTextFeed[0];
          
                          if (list[1] != null && this.newsTextFeed[1] != null && newsTextFeed.Length > 1)
                              list[1].Text = this.newsTextFeed[1];
          
                          if (list[2] != null && this.newsTextFeed[2] != null && newsTextFeed.Length > 2)
                              list[2].Text = this.newsTextFeed[2];
          
                          if (list[3] != null && this.newsTextFeed[3] != null && newsTextFeed.Length > 3)
                              list[3].Text = this.newsTextFeed[3];
          
                          if (list[4] != null && this.newsTextFeed[4] != null && newsTextFeed.Length > 4)
                              list[4].Text = this.newsTextFeed[4];
                      }
          
                      if (list.Length > 4 && TextColor != null)
                      {
                          if (list[0] != null && TextColor.Length > 0 && !TextColor[0].Equals(Color.Transparent))
                              list[0].ForeColor = TextColor[0];
          
                          if (list[1] != null && TextColor.Length > 1 && !TextColor[1].Equals(Color.Transparent))
                              list[1].ForeColor = TextColor[1];
          
                          if (list[2] != null && TextColor.Length > 2 && !TextColor[2].Equals(Color.Transparent))
                              list[2].ForeColor = TextColor[2];
          
                          if (list[3] != null && TextColor.Length > 3 && !TextColor[3].Equals(Color.Transparent))
                              list[3].ForeColor = TextColor[3];
          
                          if (list[4] != null && TextColor.Length > 4 && !TextColor[4].Equals(Color.Transparent))
                              list[4].ForeColor = TextColor[4];
                      }
          
          
                      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
                      {
                              // Initialise Position Variables 
                              int x = 1;
                              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 + (30 * i));  // The math is the UserControl size for example 150x150 and then 150/the number of labels for example 150/5=30 so it will be 30 * i \\
                                  this.Controls.Add(list[i]); // Add control to UserControl 
                              }
          
                              // Set movement variables 
                              timer1.Interval = 50;
                              jump = 1;
          
                              // Start Timer 
                              timer1.Start();
                       }
                      catch (Exception err)
                      {
                          MessageBox.Show("NewsFeeder Error: " + err);
                      }
                  }
              }
          }
          Then in form1 i created two new functions:

          Code:
          private void SetupColors(Color[] colors)
                  {
                      if (this.newsFeederControl1.TextColor.Length > 0 && colors.Length > 0)
                          this.newsFeederControl1.TextColor[0] = colors[0];
                      if (this.newsFeederControl1.TextColor.Length > 1 && colors.Length > 1)
                          this.newsFeederControl1.TextColor[1] = colors[1];
                      if (this.newsFeederControl1.TextColor.Length > 2 && colors.Length > 2)
                          this.newsFeederControl1.TextColor[2] = colors[2];
                      if (this.newsFeederControl1.TextColor.Length > 3 && colors.Length > 3)
                          this.newsFeederControl1.TextColor[3] = colors[3];
                      if (this.newsFeederControl1.TextColor.Length > 4 && colors.Length > 4)
                          this.newsFeederControl1.TextColor[4] = colors[4];
                  }
          
                  private void SetupText(string[] textToDisplay)
                  {
                      if (this.newsFeederControl1.newsTextFeed.Length > 0 && textToDisplay.Length > 0)
                          this.newsFeederControl1.newsTextFeed[0] = textToDisplay[0];
                      if (this.newsFeederControl1.newsTextFeed.Length > 1 && textToDisplay.Length > 1)
                          this.newsFeederControl1.newsTextFeed[1] = textToDisplay[1];
                      if (this.newsFeederControl1.newsTextFeed.Length > 2 && textToDisplay.Length > 2)
                          this.newsFeederControl1.newsTextFeed[2] = textToDisplay[2];
                      if (this.newsFeederControl1.newsTextFeed.Length > 3 && textToDisplay.Length > 3)
                          this.newsFeederControl1.newsTextFeed[3] = textToDisplay[3];
                      if (this.newsFeederControl1.newsTextFeed.Length > 4 && textToDisplay.Length > 4)
                          this.newsFeederControl1.newsTextFeed[4] = textToDisplay[4];
                  }
          And in the form1 form1_load event i did:

          Code:
          private void Form1_Load(object sender, EventArgs e)
                  {
                      this.newsFeederControl1.newsTextFeed = new string[5];
                      this.newsFeederControl1.TextColor = new Color[5];
                      SetupText(new string[] { "Hello This is the dfdf for today the dffd will be cloudy and ok", "From", "Daniels", "Latest", "Control" });
                      SetupColors(new Color[] { Color.Blue, Color.Red, Color.Green, Color.Pink, Color.Brown });
                      this.newsFeederControl1.startFeed();
          
                  }
          And its working great.

          Now i have two questions:

          Why when im typing some text for example into label[1] : SetupText(new string[] {"Text1", "Text2", "3 Text 3", "Text 4 -> 4", "Text Nr. 5"});
          Instead "Text1" lets say i typed "hello world this is a test for long text what do you think?" So why i dont see all the text only the first 17-18 chars.

          I want if its out of the label range or something so the text will automatic drop down toa news line without a spce. like:

          " hello this is my first label

          and now its a new line ok"

          just without the space between the lines so this is for example lable[1]



          Something like in www.ynet.co.il on the right. How the text goes up.



          Thanks.



          Now another thing i wonder is how to change the space between each of the labels! so first label coming up and lets say only when he touch the top of the control the top of the label control with the top of the control then the next label coming up and so on. something like that.



          How to make this thing where to change it in the code explain to me please so i can wriote it down and understand.

          Thanks a lot.

          Comment

          • Samuel Jones
            New Member
            • Jan 2011
            • 48

            #20
            Okay, lets tackle the first two questions.

            The reason you can't see all the text is because the label isn't wide enough, just make it wider by setting its AutoSize property to true.

            This will cause the label to exceed the boundaries of the control. so implement the following code in the for loop.

            Code:
            list[i].AutoSize = true;
            list[i].Text = genString(newsTextFeed[i])
            
            //Instead of 'list[i].Text = newsTextFeed;' line
            somewhere near the bottom of your code, we are going to make a method that measures the length of the string and if too big, make it go to a new line. Add this method:
            Code:
                    private string genString(string line)
                    {
                        string splitline = "";
                        string templine = null;
                        Graphics graphics = this.CreateGraphics();
            
                        string[] words = line.Split(' ');  // Splits string by spaces
            
                        foreach (string word in words)
                        {
                            templine = splitline;
                            splitline = splitline + word + " ";
            
                            // Measures length of string in pixels
                            SizeF textSize = graphics.MeasureString(splitline, this.Font);
            
                            // If the width is too large, put word
                            // on a new line.
                            if (textSize.Width >= this.Width)
                                splitline = templine + "\r\n" + word;
                        }
            
                        return splitline;
                    }
            That should fix the first two questions.

            In relation to the one about as soon as it leaves the top, send a new message, we will go back to the single label code from the other thread you made. But we are going to modify the code that brings it back to the bottom.

            Code:
            public string[] newsTextFeed = new string[5];
            int index = 0;
            
            private void label1_LocationChanged(object sender, EventArgs e)
                    {
                        if (label1.Location.Y <= -20)
                        {
                            index++; // adds 1 to the current value
                            if (index >= 5)
                                index = 0;
            
                            label1.Text = newsTextFeed[index];
                            label1.Location = new Point(label1.Location.X, this.Height + label1.Height);
                        }
                    }
            Hope this does the trick.

            Samuel.

            Comment

            • Chocolade
              New Member
              • Aug 2010
              • 69

              #21
              Samuel for the first two questions thisi s the code of the control after changed it as you wrote but it dosent work the text in the first label is dosent show and dosent go to the next line its getting hidden to the right.

              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
                  {
                      Label[] list = new Label[5];
                      int jump = 0;
                      public string[] newsTextFeed = null;
                      public Color[] TextColor;
                      /* 
                       *  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 NewsFeederControl_Load(object sender, EventArgs e)
                      {
              
                      }
              
                      private void timer1_Tick(object sender, EventArgs e)
                      {
                          if (list.Length > 4 && newsTextFeed != null)
                          {
                              //get the label at this position in the list
                              //and assign some text
                              if (list[0] != null && this.newsTextFeed[0] != null && newsTextFeed.Length > 0)
                                  list[0].Text = this.newsTextFeed[0];
              
                              if (list[1] != null && this.newsTextFeed[1] != null && newsTextFeed.Length > 1)
                                  list[1].Text = this.newsTextFeed[1];
              
                              if (list[2] != null && this.newsTextFeed[2] != null && newsTextFeed.Length > 2)
                                  list[2].Text = this.newsTextFeed[2];
              
                              if (list[3] != null && this.newsTextFeed[3] != null && newsTextFeed.Length > 3)
                                  list[3].Text = this.newsTextFeed[3];
              
                              if (list[4] != null && this.newsTextFeed[4] != null && newsTextFeed.Length > 4)
                                  list[4].Text = this.newsTextFeed[4];
                          }
              
                          if (list.Length > 4 && TextColor != null)
                          {
                              if (list[0] != null && TextColor.Length > 0 && !TextColor[0].Equals(Color.Transparent))
                                  list[0].ForeColor = TextColor[0];
              
                              if (list[1] != null && TextColor.Length > 1 && !TextColor[1].Equals(Color.Transparent))
                                  list[1].ForeColor = TextColor[1];
              
                              if (list[2] != null && TextColor.Length > 2 && !TextColor[2].Equals(Color.Transparent))
                                  list[2].ForeColor = TextColor[2];
              
                              if (list[3] != null && TextColor.Length > 3 && !TextColor[3].Equals(Color.Transparent))
                                  list[3].ForeColor = TextColor[3];
              
                              if (list[4] != null && TextColor.Length > 4 && !TextColor[4].Equals(Color.Transparent))
                                  list[4].ForeColor = TextColor[4];
                          }
              
              
                          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 <= -150)
                              {
                                  label.Location = new Point(label.Location.X, this.Height);
                              }
                          }  
              
                      }
              
                      public void startFeed()
                      {
                          try
                          {
                                  // Initialise Position Variables 
                                  int x = 1;
                                  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 + (30 * i));  // The math is the UserControl size for example 150x150 and then 150/the number of labels for example 150/5=30 so it will be 30 * i \\
                                      // Changing the autosize property to true will make the labels widers so more text can be written into it \\
                                      list[i].AutoSize = true;
                                      list[i].Text = genString(newsTextFeed[i]);
                                      this.Controls.Add(list[i]); // Add control to UserControl 
                                  }
              
                                  // Set movement variables 
                                  timer1.Interval = 50;
                                  jump = 1;
              
                                  // Start Timer 
                                  timer1.Start();
                           }
                          catch (Exception err)
                          {
                              MessageBox.Show("NewsFeeder Error: " + err);
                          }
                      }
              
                      // a method that measures the length of the string and if too big, make it go to a new line \\
                      private string genString(string line)
                      {
                          string splitline = "";
                          string templine = null;
                          Graphics graphics = this.CreateGraphics();
              
                          string[] words = line.Split(' ');  // Splits string by spaces 
              
                          foreach (string word in words)
                          {
                              templine = splitline;
                              splitline = splitline + word + " ";
              
                              // Measures length of string in pixels 
                              SizeF textSize = graphics.MeasureString(splitline, this.Font);
              
                              // If the width is too large, put word 
                              // on a new line. 
                              if (textSize.Width >= this.Width)
                                  splitline = templine + "\r\n" + word;
                          }
              
                          return splitline;
                      } 
              
                  }
              }

              Thanks for helping.

              Comment

              • Samuel Jones
                New Member
                • Jan 2011
                • 48

                #22
                Its because your block of code as follows:

                Code:
                if (list.Length > 4 && newsTextFeed != null)
                            {
                                //get the label at this position in the list
                                //and assign some text
                                if (list[0] != null && this.newsTextFeed[0] != null && newsTextFeed.Length > 0)
                                    list[0].Text = this.newsTextFeed[0];
                 
                                if (list[1] != null && this.newsTextFeed[1] != null && newsTextFeed.Length > 1)
                                    list[1].Text = this.newsTextFeed[1];
                 
                                if (list[2] != null && this.newsTextFeed[2] != null && newsTextFeed.Length > 2)
                                    list[2].Text = this.newsTextFeed[2];
                 
                                if (list[3] != null && this.newsTextFeed[3] != null && newsTextFeed.Length > 3)
                                    list[3].Text = this.newsTextFeed[3];
                 
                                if (list[4] != null && this.newsTextFeed[4] != null && newsTextFeed.Length > 4)
                                    list[4].Text = this.newsTextFeed[4];
                            }
                You reassign the OLD string of text to it every 'tick' not the modified multi-line string.

                We will change this line to suit your code:
                Code:
                list[i].Text = genString(newsTextFeed[i]);
                to:
                Code:
                newsTextFeed[i] = genString(newsTextFeed[i]);
                list[i].Text = newsTextFeed[i];
                That should work.

                Sam

                Comment

                Working...