473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class NewsFeederControl : UserControl
  13.     {
  14.         string[] newsTF;
  15.         Label[] labelarray;
  16.         int jump = 0;
  17.         string newsTextFeed;
  18.         Color TextColor;
  19.         public NewsFeederControl()
  20.         {
  21.             InitializeComponent();
  22.             labelarray = new Label[5];
  23.             for (int i = 1; i < 5; i++)
  24.             {
  25.                 //intialize new label
  26.                 labelarray[i] = new Label();
  27.                 labelarray[i].Size = new System.Drawing.Size(33, 33);
  28.                 labelarray[i].Location = new System.Drawing.Point(i, i);
  29.                 labelarray[i].Text = i.ToString();
  30.                 this.Controls.AddRange(new System.Windows.Forms.Control[] {labelarray[i]});
  31.             }
  32.             TextColor = new Color();
  33.         }
  34.  
  35.         private void NewsFeederControl_Load(object sender, EventArgs e)
  36.         {
  37.  
  38.         }
  39.  
  40.         private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
  41.         {
  42.             startCustom();
  43.  
  44.         }
  45.  
  46.         public void newsTextFeeds(string newsText)
  47.         {
  48.             newsTextFeed = newsText;
  49.         }
  50.  
  51.         public void newsTextColor(Color color)
  52.         {
  53.             TextColor = color;
  54.         }
  55.  
  56.         private void timer1_Tick(object sender, EventArgs e)
  57.         {
  58.             label1.Location = new Point(label1.Location.X, label1.Location.Y - jump);
  59.         }
  60.  
  61.         public void startCustom()
  62.         {
  63.             label1.Text = newsTextFeed;
  64.             label1.ForeColor = TextColor;
  65.             timer1.Interval = 50;
  66.             jump = 1;
  67.             timer1.Start();
  68.  
  69.         }
  70.  
  71.         private void label1_LocationChanged(object sender, EventArgs e)
  72.         {
  73.             if (label1.Location.Y <= -20)
  74.             {
  75.                 label1.Location = new Point(label1.Location.X, this.Height + label1.Height);
  76.             }
  77.         }
  78.  
  79.  
  80.     }
  81. }
  82.  

Thanks.
Jan 13 '11 #1
21 3903
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.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 1; i < 5; i++)
  2. {
  3. labelarray[i].Location = new Point(labelarray[i].Location.X, labelarray[i].Location.Y - jump);
  4.  
  5. if (labelarray[i].Location.Y <= -20)
  6. labelarray[i].Location = new Point(labelarray[i].Loaction.X, this.Height + labelarray[i].Height);
  7. }
  8.  
And in the startCustom() event, add this:

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

Sam
Jan 13 '11 #2
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?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class NewsFeederControl : UserControl
  13.     {
  14.         string[] newsTF;
  15.         Label[] labelarray;
  16.         int jump = 0;
  17.         string newsTextFeed;
  18.         Color TextColor;
  19.         public NewsFeederControl()
  20.         {
  21.             InitializeComponent();
  22.             labelarray = new Label[5];
  23.             TextColor = new Color();
  24.         }
  25.  
  26.         private void NewsFeederControl_Load(object sender, EventArgs e)
  27.         {
  28.  
  29.         }
  30.  
  31.         private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
  32.         {
  33.             startCustom();
  34.  
  35.         }
  36.  
  37.         public void newsTextFeeds(string newsText)
  38.         {
  39.             newsTextFeed = newsText;
  40.         }
  41.  
  42.         public void newsTextColor(Color color)
  43.         {
  44.             TextColor = color;
  45.         }
  46.  
  47.         private void timer1_Tick(object sender, EventArgs e)
  48.         {
  49.             // label1.Location = new Point(label1.Location.X, label1.Location.Y - jump);
  50.             for (int i = 1; i < 5; i++)
  51.             {
  52.                 labelarray[i].Location = new Point(labelarray[i].Location.X, labelarray[i].Location.Y - jump);
  53.                 if (labelarray[i].Location.Y <= -20)
  54.                 {
  55.                     labelarray[i].Location = new Point(labelarray[i].Location.X, this.Height + labelarray[i].Height);
  56.                 }
  57.             }
  58.         }
  59.  
  60.         public void startCustom()
  61.         {
  62.             int x = 20;
  63.             int y = (int)(this.Height / 2);  //In the middle 
  64.             for (int i = 1; i < 5; i++)
  65.             {
  66.                 labelarray[i].Location = new Point(x, y + 40 * i);
  67.  
  68.                 labelarray[i].Text = newsTextFeed;
  69.                 labelarray[i].ForeColor = TextColor;
  70.             }
  71.             timer1.Interval = 50;
  72.             jump = 1;
  73.             timer1.Start();
  74.  
  75.         }
  76.  
  77.         private void label1_LocationChanged(object sender, EventArgs e)
  78.         {
  79.           /*  if (labelarray[i].Location.Y <= -20)
  80.             {
  81.                 labelarray[].Location = new Point(label1.Location.X, this.Height + label1.Height);
  82.             }*/
  83.         }
  84.  
  85.  
  86.  
  87.     }
  88. }
  89.  

Thanks.
Jan 13 '11 #3
Can you attach the file? id rather not have to spend another 40min recreating your project :D
Jan 13 '11 #4
Or could you copy out the error and tell me what it says?
Jan 13 '11 #5
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.

http://www.megaupload.com/?d=QQKRNHH4
Jan 13 '11 #6
Thorsten i tried this code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class NewsFeederControl : UserControl
  13.     {
  14.         List<Label> list;
  15.         int jump = 0;
  16.         string newsTextFeed;
  17.         Color TextColor;
  18.         public NewsFeederControl()
  19.         {
  20.             InitializeComponent();
  21.             list = new List<Label>(5);
  22.             TextColor = new Color();
  23.         }
  24.  
  25.         private void NewsFeederControl_Load(object sender, EventArgs e)
  26.         {
  27.  
  28.         }
  29.  
  30.         private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
  31.         {
  32.             startCustom();
  33.  
  34.         }
  35.  
  36.         public void newsTextFeeds(string newsText)
  37.         {
  38.             newsTextFeed = newsText;
  39.         }
  40.  
  41.         public void newsTextColor(Color color)
  42.         {
  43.             TextColor = color;
  44.         }
  45.  
  46.         private void timer1_Tick(object sender, EventArgs e)
  47.         {
  48.             for (int i = 1; i < 5; i++)
  49.             // Loop through List with for
  50.             {
  51.                 list[i].Location = new Point(list[i].Location.X, list[i].Location.Y - jump);
  52.                 if (list[i].Location.Y <= -20)
  53.                 {
  54.                     list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height);
  55.                 }
  56.             }
  57.  
  58.         }
  59.  
  60.         public void startCustom()
  61.         {
  62.             int x = 2;
  63.             int y = (int)(this.Height / 2);  //In the middle 
  64.                 for (int i = 1; i < 5; i++)
  65.                 {
  66.                     list[i].Location = new Point(x,y+40*i); 
  67.                 }
  68.                 timer1.Interval = 50;
  69.                 jump = 1;
  70.                 timer1.Start();
  71.  
  72.  
  73.         }
  74.  
  75.         private void label1_LocationChanged(object sender, EventArgs e)
  76.         {
  77.            /*   if (list[i].Location.Y <= -20) 
  78.               { 
  79.                   list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height); 
  80.               }*/
  81.         }
  82.  
  83.  
  84.  
  85.     }
  86. }
  87.  


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.
Jan 13 '11 #7
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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at WindowsFormsApplication1.NewsFeederControl.startCu stom() in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\NewsFeederControl.cs:line 69
13/01/2011 22:36:47 ==> Last Downloaded file is :C:\Users\Chocolade\AppData\Local\WindowsFormsAppl ication1\My Weather Station\satellite\satellite817.jpg
Jan 13 '11 #8
Combine these two together
Expand|Select|Wrap|Line Numbers
  1. List<Label> list;
  2. list = new List<Label>(5);
to make this
Expand|Select|Wrap|Line Numbers
  1. Label[] list = new Label[5];
Note: if you want to pragmatically change the size, change each line to this:
Expand|Select|Wrap|Line Numbers
  1. Label[] list;
  2.  
  3. //Put in a public method ie. public void changeSize(int size);
  4. list = new Label[size];
And change your for loops to this:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 14 '11 #9
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
Jan 14 '11 #10
Christian Binder
218 Expert 100+
I'd also suggest to use for-each instead of for when iterating through a collection (list, array, dictionary, ...)

Expand|Select|Wrap|Line Numbers
  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3.   foreach (Label label in list)
  4.   // Loop through List with for-each
  5.   {
  6.     label.Location = new Point(label.Location.X, label.Location.Y - jump);
  7.     if (label.Location.Y <= -20)
  8.     {
  9.       label.Location = new Point(label.Location.X, this.Height + label.Height);
  10.     }
  11.   }
  12. }
  13.  
Jan 14 '11 #11
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using DannyGeneral;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class NewsFeederControl : UserControl
  14.     {
  15.         Label[] list = new Label[5];
  16.         int jump = 0;
  17.         string newsTextFeed;
  18.         Color TextColor;
  19.         public NewsFeederControl()
  20.         {
  21.             InitializeComponent();
  22.             TextColor = new Color();
  23.         }
  24.  
  25.         private void NewsFeederControl_Load(object sender, EventArgs e)
  26.         {
  27.  
  28.         }
  29.  
  30.         private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
  31.         {
  32.             startCustom();
  33.  
  34.         }
  35.  
  36.         public void newsTextFeeds(string newsText)
  37.         {
  38.             newsTextFeed = newsText;
  39.         }
  40.  
  41.         public void newsTextColor(Color color)
  42.         {
  43.             TextColor = color;
  44.         }
  45.  
  46.         private void timer1_Tick(object sender, EventArgs e)
  47.         {
  48.             for (int i = 0; i < 5; i++)
  49.             // Loop through List with for 
  50.             {
  51.                 list[i].Location = new Point(list[i].Location.X, list[i].Location.Y - jump);
  52.                 if (list[i].Location.Y <= -20)
  53.                 {
  54.                     list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height);
  55.                 }
  56.             }
  57.  
  58.         }
  59.  
  60.         public void startCustom()
  61.         {
  62.             try
  63.             {
  64.                 int x = 2;
  65.                 int y = (int)(this.Height / 2);  //In the middle  
  66.                 for (int i = 1; i < 5; i++)
  67.                 {
  68.                     list[i].Location = new Point(x, y + 40 * i);
  69.                 }
  70.                 timer1.Interval = 50;
  71.                 jump = 1;
  72.                 timer1.Start();
  73.             }
  74.             catch (Exception err)
  75.             {
  76.                 Logger.Write("NewsFeeder Error: " + err);
  77.             }
  78.  
  79.  
  80.         }
  81.  
  82.         private void label1_LocationChanged(object sender, EventArgs e)
  83.         {
  84.             /*   if (list[i].Location.Y <= -20)  
  85.                {  
  86.                    list[i].Location = new Point(list[i].Location.X, this.Height + list[i].Height);  
  87.                }*/
  88.         }
  89.  
  90.  
  91.  
  92.     }
  93. }
  94.  
Note that im not using anymore the label1 changed event ill remove it later.


The null error is on line 65 means this line:

Expand|Select|Wrap|Line Numbers
  1. list[i].Location = new Point(x, y + 40 * i);
  2.  
wich is in the startCustom() function.

Thats the complete exception error:

14/01/2011 10:23:52 ==> NewsFeeder Error: System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsFormsApplication1.NewsFeederControl.startCu stom() in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\NewsFeederControl.cs:line 68
14/01/2011 10:23:55 ==> NewsFeeder Error: System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsFormsApplication1.NewsFeederControl.startCu stom() in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\NewsFeederControl.cs:line 68


Ill upload the file in few if you need it again.
Thanks.
Jan 14 '11 #12
Christian Binder
218 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1. list[1] = label1;
  2. list[2] = label2;
  3. ...
  4.  
or you create them if you don't use the designer.
Expand|Select|Wrap|Line Numbers
  1. for(int i = 1; i < 5; i++)
  2.   list[i] = new Label();
  3.  
Jan 14 '11 #13
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using DannyGeneral;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class NewsFeederControl : UserControl
  14.     {
  15.         Label[] list = new Label[5];
  16.         int jump = 0;
  17.         string newsTextFeed;
  18.         Color TextColor;
  19.         public NewsFeederControl()
  20.         {
  21.             InitializeComponent();
  22.             TextColor = new Color();
  23.  
  24.         }
  25.  
  26.         private void NewsFeederControl_Load(object sender, EventArgs e)
  27.         {
  28.  
  29.         }
  30.  
  31.         private void NewsFeederControl_Paint(object sender, PaintEventArgs e)
  32.         {
  33.             startCustom();
  34.  
  35.         }
  36.  
  37.         public void newsTextFeeds(string newsText)
  38.         {
  39.             newsTextFeed = newsText;
  40.         }
  41.  
  42.         public void newsTextColor(Color color)
  43.         {
  44.             TextColor = color;
  45.         }
  46.  
  47.         private void timer1_Tick(object,sender,EventArgse)
  48.         {
  49.           foreach (Label label in list)
  50.             // Loop through List with for-each 
  51.             {
  52.                 label.Location = new Point(label.Location.X, label.Location.Y - jump);
  53.                 if (label.Location.Y <= -20)
  54.                 {
  55.                     label.Location = new Point(label.Location.X, this.Height + label.Height);
  56.                 }
  57.             } 
  58.         }
  59.  
  60.         public void startCustom()
  61.         {
  62.             try
  63.             {
  64.                 for (int i = 0; i < 5; i++)
  65.                 {
  66.                     list[i] = new Label();
  67.                 }
  68.                 int x = 2;
  69.                 int y = (int)(this.Height / 2);  //In the middle   
  70.                 for (int i = 1; i < 5; i++)
  71.                 {
  72.                     list[i].Location = new Point(x, y + 40 * i);
  73.                 }
  74.                 list[1].Text = newsTextFeed;
  75.                 list[1].ForeColor = TextColor;
  76.                 timer1.Interval = 50;
  77.                 jump = 1;
  78.                 timer1.Start();
  79.             }
  80.             catch (Exception err)
  81.             {
  82.                 Logger.Write("NewsFeeder Error: " + err);
  83.             }
  84.         }
  85.     }
  86. }
  87.  
I changed it to foreach.

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

Expand|Select|Wrap|Line Numbers
  1. newsFeederControl1.newsTextFeeds("Hello hi");
  2.             newsFeederControl1.newsTextColor(Color.Red);
  3.  
But i dont see anything in to control moving.
Jan 14 '11 #14
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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace OnlineHelpScroller
  11. {
  12.     public partial class NewsFeederControl : UserControl
  13.     {
  14.         Label[] list = new Label[5];
  15.         int jump = 0;
  16.         public string newsTextFeed = null;
  17.         public Color TextColor = new Color();
  18.  
  19.         /*
  20.          *  To set text to display call:
  21.          *      *NewsFeederControl name*.newsTextFeed = "somestring";
  22.          *  To set color of text call:
  23.          *      *NewsFeederControl name*.newsTextFeed = Color.*somecolor*
  24.          *      
  25.          */
  26.  
  27.         public NewsFeederControl()
  28.         {
  29.             InitializeComponent();
  30.         }
  31.  
  32.         private void timer1_Tick(object sender, EventArgs e)
  33.         {
  34.             foreach (Label label in list)
  35.             // Loop through List with for-each 
  36.             {
  37.                 // Move label up 'jump' pixels
  38.                 label.Location = new Point(label.Location.X, label.Location.Y - jump);
  39.  
  40.                 // Check to see if out of view.
  41.                 if (label.Location.Y <= -20)
  42.                 {
  43.                     label.Location = new Point(label.Location.X, this.Height);
  44.                 }
  45.             } 
  46.         }
  47.  
  48.         public void startFeed()
  49.         {
  50.             try
  51.             {
  52.                 if (newsTextFeed != null && TextColor != Color.Empty)
  53.                 {
  54.                     // Initialise Position Variables
  55.                     int x = 2;
  56.                     int y = this.Height;  // just out of view at top.
  57.  
  58.                     // Assign each label in list the following properties
  59.                     for (int i = 0; i < 5; i++)
  60.                     {
  61.                         list[i] = new Label();
  62.                         list[i].Location = new Point(x, y + (40 * i));
  63.                         list[i].Text = newsTextFeed;
  64.                         list[i].ForeColor = TextColor;
  65.                         this.Controls.Add(list[i]); // Add control to UserControl
  66.                     }
  67.  
  68.                     // Set movement variables
  69.                     timer1.Interval = 50;
  70.                     jump = 1;
  71.  
  72.                     // Start Timer
  73.                     timer1.Start();
  74.                 }
  75.                 else if (newsTextFeed == null)
  76.                     MessageBox.Show("newsTextFeed is null");
  77.                 else if (TextColor == Color.Empty)
  78.                     MessageBox.Show("TextColor not defined");
  79.             }
  80.             catch (Exception err)
  81.             {
  82.                 MessageBox.Show("NewsFeeder Error: " + err);
  83.             }
  84.         }
  85.  
  86.         public void changeFeed()
  87.         {
  88.             try
  89.             {
  90.                 if (newsTextFeed != null && TextColor != Color.Empty)
  91.                 {
  92.                     // Assign each label in list the following properties
  93.                     for (int i = 0; i < 5; i++)
  94.                     {
  95.                         list[i].Text = newsTextFeed;
  96.                         list[i].ForeColor = TextColor;
  97.                     }
  98.                 }
  99.                 else if (newsTextFeed == null)
  100.                     MessageBox.Show("newsTextFeed is null");
  101.                 else if (TextColor == Color.Empty)
  102.                     MessageBox.Show("TextColor not defined");
  103.             }
  104.             catch (Exception err)
  105.             {
  106.                 MessageBox.Show("NewsFeeder Error: " + err);
  107.             }
  108.         }
  109.     }
  110. }
  111.  
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.)
Expand|Select|Wrap|Line Numbers
  1.             newsFeederControl1.newsTextFeed = "somestring";
  2.             newsFeederControl1.TextColor = Color.Red;
  3.             newsFeederControl1.startFeed();
To change the settings, use this code
Expand|Select|Wrap|Line Numbers
  1.             newsFeederControl1.newsTextFeed = "otherstring";
  2.             newsFeederControl1.TextColor = Color.Blue;
  3.             newsFeederControl1.changeFeed();
My suggestion is to now make newsTextFeed and TextColor arrays so each item has its own settings.
Jan 15 '11 #15
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!
Jan 15 '11 #16
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.
Jan 15 '11 #17
Underneath the 'int jump = 0' line add this
Expand|Select|Wrap|Line Numbers
  1. int space = 40
Change that number to change the space, currently in my code i have it set to 40 in this line:
Expand|Select|Wrap|Line Numbers
  1. list[i].Location = new Point(x, y + (40 * i));
Change it to:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 16 '11 #18
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class NewsFeederControl : UserControl
  13.     {
  14.         Label[] list = new Label[5];
  15.         int jump = 0;
  16.         public string[] newsTextFeed = null;
  17.         public Color[] TextColor;
  18.         /* 
  19.          *  To set text to display call: 
  20.          *      *NewsFeederControl name*.newsTextFeed = "somestring"; 
  21.          *  To set color of text call: 
  22.          *      *NewsFeederControl name*.newsTextFeed = Color.*somecolor* 
  23.          *       
  24.          */ 
  25.  
  26.         public NewsFeederControl()
  27.         {
  28.             InitializeComponent();
  29.  
  30.         }
  31.  
  32.         private void NewsFeederControl_Load(object sender, EventArgs e)
  33.         {
  34.  
  35.         }
  36.  
  37.         private void timer1_Tick(object sender, EventArgs e)
  38.         {
  39.             if (list.Length > 4 && newsTextFeed != null)
  40.             {
  41.                 //get the label at this position in the list
  42.                 //and assign some text
  43.                 if (list[0] != null && this.newsTextFeed[0] != null && newsTextFeed.Length > 0)
  44.                     list[0].Text = this.newsTextFeed[0];
  45.  
  46.                 if (list[1] != null && this.newsTextFeed[1] != null && newsTextFeed.Length > 1)
  47.                     list[1].Text = this.newsTextFeed[1];
  48.  
  49.                 if (list[2] != null && this.newsTextFeed[2] != null && newsTextFeed.Length > 2)
  50.                     list[2].Text = this.newsTextFeed[2];
  51.  
  52.                 if (list[3] != null && this.newsTextFeed[3] != null && newsTextFeed.Length > 3)
  53.                     list[3].Text = this.newsTextFeed[3];
  54.  
  55.                 if (list[4] != null && this.newsTextFeed[4] != null && newsTextFeed.Length > 4)
  56.                     list[4].Text = this.newsTextFeed[4];
  57.             }
  58.  
  59.             if (list.Length > 4 && TextColor != null)
  60.             {
  61.                 if (list[0] != null && TextColor.Length > 0 && !TextColor[0].Equals(Color.Transparent))
  62.                     list[0].ForeColor = TextColor[0];
  63.  
  64.                 if (list[1] != null && TextColor.Length > 1 && !TextColor[1].Equals(Color.Transparent))
  65.                     list[1].ForeColor = TextColor[1];
  66.  
  67.                 if (list[2] != null && TextColor.Length > 2 && !TextColor[2].Equals(Color.Transparent))
  68.                     list[2].ForeColor = TextColor[2];
  69.  
  70.                 if (list[3] != null && TextColor.Length > 3 && !TextColor[3].Equals(Color.Transparent))
  71.                     list[3].ForeColor = TextColor[3];
  72.  
  73.                 if (list[4] != null && TextColor.Length > 4 && !TextColor[4].Equals(Color.Transparent))
  74.                     list[4].ForeColor = TextColor[4];
  75.             }
  76.  
  77.  
  78.             foreach (Label label in list)
  79.             // Loop through List with for-each  
  80.             {
  81.                 // Move label up 'jump' pixels
  82.                 label.Location = new Point(label.Location.X, label.Location.Y - jump);
  83.  
  84.                 // Check to see if out of view. 
  85.                 if (label.Location.Y <= -20)
  86.                 {
  87.                     label.Location = new Point(label.Location.X, this.Height);
  88.                 }
  89.             }  
  90.  
  91.         }
  92.  
  93.         public void startFeed()
  94.         {
  95.             try
  96.             {
  97.                     // Initialise Position Variables 
  98.                     int x = 1;
  99.                     int y = this.Height;  // just out of view at top. 
  100.  
  101.                     // Assign each label in list the following properties 
  102.                     for (int i = 0; i < 5; i++)
  103.                     {
  104.                         list[i] = new Label();
  105.                         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 \\
  106.                         this.Controls.Add(list[i]); // Add control to UserControl 
  107.                     }
  108.  
  109.                     // Set movement variables 
  110.                     timer1.Interval = 50;
  111.                     jump = 1;
  112.  
  113.                     // Start Timer 
  114.                     timer1.Start();
  115.              }
  116.             catch (Exception err)
  117.             {
  118.                 MessageBox.Show("NewsFeeder Error: " + err);
  119.             }
  120.         }
  121.     }
  122. }
  123.  
Then in form1 i created two new functions:

Expand|Select|Wrap|Line Numbers
  1. private void SetupColors(Color[] colors)
  2.         {
  3.             if (this.newsFeederControl1.TextColor.Length > 0 && colors.Length > 0)
  4.                 this.newsFeederControl1.TextColor[0] = colors[0];
  5.             if (this.newsFeederControl1.TextColor.Length > 1 && colors.Length > 1)
  6.                 this.newsFeederControl1.TextColor[1] = colors[1];
  7.             if (this.newsFeederControl1.TextColor.Length > 2 && colors.Length > 2)
  8.                 this.newsFeederControl1.TextColor[2] = colors[2];
  9.             if (this.newsFeederControl1.TextColor.Length > 3 && colors.Length > 3)
  10.                 this.newsFeederControl1.TextColor[3] = colors[3];
  11.             if (this.newsFeederControl1.TextColor.Length > 4 && colors.Length > 4)
  12.                 this.newsFeederControl1.TextColor[4] = colors[4];
  13.         }
  14.  
  15.         private void SetupText(string[] textToDisplay)
  16.         {
  17.             if (this.newsFeederControl1.newsTextFeed.Length > 0 && textToDisplay.Length > 0)
  18.                 this.newsFeederControl1.newsTextFeed[0] = textToDisplay[0];
  19.             if (this.newsFeederControl1.newsTextFeed.Length > 1 && textToDisplay.Length > 1)
  20.                 this.newsFeederControl1.newsTextFeed[1] = textToDisplay[1];
  21.             if (this.newsFeederControl1.newsTextFeed.Length > 2 && textToDisplay.Length > 2)
  22.                 this.newsFeederControl1.newsTextFeed[2] = textToDisplay[2];
  23.             if (this.newsFeederControl1.newsTextFeed.Length > 3 && textToDisplay.Length > 3)
  24.                 this.newsFeederControl1.newsTextFeed[3] = textToDisplay[3];
  25.             if (this.newsFeederControl1.newsTextFeed.Length > 4 && textToDisplay.Length > 4)
  26.                 this.newsFeederControl1.newsTextFeed[4] = textToDisplay[4];
  27.         }
  28.  
And in the form1 form1_load event i did:

Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             this.newsFeederControl1.newsTextFeed = new string[5];
  4.             this.newsFeederControl1.TextColor = new Color[5];
  5.             SetupText(new string[] { "Hello This is the dfdf for today the dffd will be cloudy and ok", "From", "Daniels", "Latest", "Control" });
  6.             SetupColors(new Color[] { Color.Blue, Color.Red, Color.Green, Color.Pink, Color.Brown });
  7.             this.newsFeederControl1.startFeed();
  8.  
  9.         }
  10.  
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.
Jan 17 '11 #19
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.

Expand|Select|Wrap|Line Numbers
  1. list[i].AutoSize = true;
  2. list[i].Text = genString(newsTextFeed[i])
  3.  
  4. //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:
Expand|Select|Wrap|Line Numbers
  1.         private string genString(string line)
  2.         {
  3.             string splitline = "";
  4.             string templine = null;
  5.             Graphics graphics = this.CreateGraphics();
  6.  
  7.             string[] words = line.Split(' ');  // Splits string by spaces
  8.  
  9.             foreach (string word in words)
  10.             {
  11.                 templine = splitline;
  12.                 splitline = splitline + word + " ";
  13.  
  14.                 // Measures length of string in pixels
  15.                 SizeF textSize = graphics.MeasureString(splitline, this.Font);
  16.  
  17.                 // If the width is too large, put word
  18.                 // on a new line.
  19.                 if (textSize.Width >= this.Width)
  20.                     splitline = templine + "\r\n" + word;
  21.             }
  22.  
  23.             return splitline;
  24.         }
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.

Expand|Select|Wrap|Line Numbers
  1. public string[] newsTextFeed = new string[5];
  2. int index = 0;
  3.  
  4. private void label1_LocationChanged(object sender, EventArgs e)
  5.         {
  6.             if (label1.Location.Y <= -20)
  7.             {
  8.                 index++; // adds 1 to the current value
  9.                 if (index >= 5)
  10.                     index = 0;
  11.  
  12.                 label1.Text = newsTextFeed[index];
  13.                 label1.Location = new Point(label1.Location.X, this.Height + label1.Height);
  14.             }
  15.         }
Hope this does the trick.

Samuel.
Jan 17 '11 #20
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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class NewsFeederControl : UserControl
  13.     {
  14.         Label[] list = new Label[5];
  15.         int jump = 0;
  16.         public string[] newsTextFeed = null;
  17.         public Color[] TextColor;
  18.         /* 
  19.          *  To set text to display call: 
  20.          *      *NewsFeederControl name*.newsTextFeed = "somestring"; 
  21.          *  To set color of text call: 
  22.          *      *NewsFeederControl name*.newsTextFeed = Color.*somecolor* 
  23.          *       
  24.          */ 
  25.  
  26.         public NewsFeederControl()
  27.         {
  28.             InitializeComponent();
  29.  
  30.         }
  31.  
  32.         private void NewsFeederControl_Load(object sender, EventArgs e)
  33.         {
  34.  
  35.         }
  36.  
  37.         private void timer1_Tick(object sender, EventArgs e)
  38.         {
  39.             if (list.Length > 4 && newsTextFeed != null)
  40.             {
  41.                 //get the label at this position in the list
  42.                 //and assign some text
  43.                 if (list[0] != null && this.newsTextFeed[0] != null && newsTextFeed.Length > 0)
  44.                     list[0].Text = this.newsTextFeed[0];
  45.  
  46.                 if (list[1] != null && this.newsTextFeed[1] != null && newsTextFeed.Length > 1)
  47.                     list[1].Text = this.newsTextFeed[1];
  48.  
  49.                 if (list[2] != null && this.newsTextFeed[2] != null && newsTextFeed.Length > 2)
  50.                     list[2].Text = this.newsTextFeed[2];
  51.  
  52.                 if (list[3] != null && this.newsTextFeed[3] != null && newsTextFeed.Length > 3)
  53.                     list[3].Text = this.newsTextFeed[3];
  54.  
  55.                 if (list[4] != null && this.newsTextFeed[4] != null && newsTextFeed.Length > 4)
  56.                     list[4].Text = this.newsTextFeed[4];
  57.             }
  58.  
  59.             if (list.Length > 4 && TextColor != null)
  60.             {
  61.                 if (list[0] != null && TextColor.Length > 0 && !TextColor[0].Equals(Color.Transparent))
  62.                     list[0].ForeColor = TextColor[0];
  63.  
  64.                 if (list[1] != null && TextColor.Length > 1 && !TextColor[1].Equals(Color.Transparent))
  65.                     list[1].ForeColor = TextColor[1];
  66.  
  67.                 if (list[2] != null && TextColor.Length > 2 && !TextColor[2].Equals(Color.Transparent))
  68.                     list[2].ForeColor = TextColor[2];
  69.  
  70.                 if (list[3] != null && TextColor.Length > 3 && !TextColor[3].Equals(Color.Transparent))
  71.                     list[3].ForeColor = TextColor[3];
  72.  
  73.                 if (list[4] != null && TextColor.Length > 4 && !TextColor[4].Equals(Color.Transparent))
  74.                     list[4].ForeColor = TextColor[4];
  75.             }
  76.  
  77.  
  78.             foreach (Label label in list)
  79.             // Loop through List with for-each  
  80.             {
  81.                 // Move label up 'jump' pixels
  82.                 label.Location = new Point(label.Location.X, label.Location.Y - jump);
  83.  
  84.                 // Check to see if out of view. 
  85.                 if (label.Location.Y <= -150)
  86.                 {
  87.                     label.Location = new Point(label.Location.X, this.Height);
  88.                 }
  89.             }  
  90.  
  91.         }
  92.  
  93.         public void startFeed()
  94.         {
  95.             try
  96.             {
  97.                     // Initialise Position Variables 
  98.                     int x = 1;
  99.                     int y = this.Height;  // just out of view at top. 
  100.  
  101.                     // Assign each label in list the following properties 
  102.                     for (int i = 0; i < 5; i++)
  103.                     {
  104.                         list[i] = new Label();
  105.                         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 \\
  106.                         // Changing the autosize property to true will make the labels widers so more text can be written into it \\
  107.                         list[i].AutoSize = true;
  108.                         list[i].Text = genString(newsTextFeed[i]);
  109.                         this.Controls.Add(list[i]); // Add control to UserControl 
  110.                     }
  111.  
  112.                     // Set movement variables 
  113.                     timer1.Interval = 50;
  114.                     jump = 1;
  115.  
  116.                     // Start Timer 
  117.                     timer1.Start();
  118.              }
  119.             catch (Exception err)
  120.             {
  121.                 MessageBox.Show("NewsFeeder Error: " + err);
  122.             }
  123.         }
  124.  
  125.         // a method that measures the length of the string and if too big, make it go to a new line \\
  126.         private string genString(string line)
  127.         {
  128.             string splitline = "";
  129.             string templine = null;
  130.             Graphics graphics = this.CreateGraphics();
  131.  
  132.             string[] words = line.Split(' ');  // Splits string by spaces 
  133.  
  134.             foreach (string word in words)
  135.             {
  136.                 templine = splitline;
  137.                 splitline = splitline + word + " ";
  138.  
  139.                 // Measures length of string in pixels 
  140.                 SizeF textSize = graphics.MeasureString(splitline, this.Font);
  141.  
  142.                 // If the width is too large, put word 
  143.                 // on a new line. 
  144.                 if (textSize.Width >= this.Width)
  145.                     splitline = templine + "\r\n" + word;
  146.             }
  147.  
  148.             return splitline;
  149.         } 
  150.  
  151.     }
  152. }
  153.  

Thanks for helping.
Jan 17 '11 #21
Its because your block of code as follows:

Expand|Select|Wrap|Line Numbers
  1. if (list.Length > 4 && newsTextFeed != null)
  2.             {
  3.                 //get the label at this position in the list
  4.                 //and assign some text
  5.                 if (list[0] != null && this.newsTextFeed[0] != null && newsTextFeed.Length > 0)
  6.                     list[0].Text = this.newsTextFeed[0];
  7.  
  8.                 if (list[1] != null && this.newsTextFeed[1] != null && newsTextFeed.Length > 1)
  9.                     list[1].Text = this.newsTextFeed[1];
  10.  
  11.                 if (list[2] != null && this.newsTextFeed[2] != null && newsTextFeed.Length > 2)
  12.                     list[2].Text = this.newsTextFeed[2];
  13.  
  14.                 if (list[3] != null && this.newsTextFeed[3] != null && newsTextFeed.Length > 3)
  15.                     list[3].Text = this.newsTextFeed[3];
  16.  
  17.                 if (list[4] != null && this.newsTextFeed[4] != null && newsTextFeed.Length > 4)
  18.                     list[4].Text = this.newsTextFeed[4];
  19.             }
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:
Expand|Select|Wrap|Line Numbers
  1. list[i].Text = genString(newsTextFeed[i]);
to:
Expand|Select|Wrap|Line Numbers
  1. newsTextFeed[i] = genString(newsTextFeed[i]);
  2. list[i].Text = newsTextFeed[i];
That should work.

Sam
Jan 18 '11 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: avashisht | last post by:
Hi, I am veeeery new to perl.. i have a script where i am outputting the Environment variable to a HASH. either i want to output hash values into a text file, Or get hash into an array and then...
3
by: Geoff Hague | last post by:
I've got a small little problem with my 'website-in-progress': http://www.captainsoftheworld.com/modernrepublic/strict/index.php...
1
by: Buddy | last post by:
Hello, Does anyone know how to stop labels from wrapping text. If the text is to long for the label then I want it to hide the text just like out the TextBox control works. Thanks,
2
by: Woody Splawn | last post by:
When placing labels and text boxes on winforms they are coming up by default in a size that is just a little too small. How do I change this so that in the future they will come up with a bigger...
5
by: B | last post by:
Hello, I am trying to migrate a vb6 app to vb.net. In one piece of the vb6 app I have a label where the label backcolor changes every 5 seconds on a timer. So I created an array of colors as...
10
by: tnspc | last post by:
I have a function in my program to write an array to a text file, but every way I've tried results in the same result: when I open the file to check what's been written to it, all it displays is the...
3
by: OliveOyl3471 | last post by:
In Visual Basic.NET 2003, how do you populate a two dimensional array from a text file? I know how to check file existence, open the file for read, etc. I just can't get it to read more than one...
11
by: deeptismuley | last post by:
Hello, i want to write an array to a text file using visual basic. the output should look like this for a 5X5 sized array. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0...
3
by: tivicoo | last post by:
I have a 16 bit array called ADData, and i want to save this array to a text file using C#. i tried to use different codes that i located online but when i open the saved data on notepad the data...
1
anfetienne
by: anfetienne | last post by:
i have this code below that i made....it loads vars from txt file splits it then puts it into an array....once in an array it the brings the pics in from the array to create thumbnails and a larger...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.