CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2008
    Posts
    52

    Counting Lines in RichBox with labels

    Well i im trying to make a panel add a label with a that's text is +1 of the previous (starting from zero) each time enter is pressed in a rich Text Box, using the keyUp event but for some reason it only works in the first time I press enter and after that nothing happens, here the code i got so far:

    Code:
            private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                {
                    int number = int.Parse(labelK.Text);
                    if (number != 0)
                    {
                        number++;
                        int Ypoint = number * 13;
                        int Y = 13 + Ypoint;
                        Label label = new Label();
                        label.Name = "label" + number.ToString();
                        label.Location = new Point(3, Y);
                        label.Text = number.ToString();
                        panel1.Controls.Add(label);
                    }
                    else
                    {
                        number++;
                        Label label = new Label();
                        label.Name = "label" + number.ToString();
                        label.Location = new Point(3, 13);
                        label.Text = number.ToString();
                        panel1.Controls.Add(label);
                    }
                }
            }
    Any ideas on how to many it work are appreciated.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Counting Lines in RichBox with labels

    I guess your Y is not increasing.

  3. #3
    Join Date
    Jan 2007
    Posts
    491

    Re: Counting Lines in RichBox with labels

    Your second if-else statement is totally pointless. Just move the label to point (3, 13*(number+1).
    But the "number" variable (the current y?) stays the same anyway 'cuz you never change LabelK.Text, so I can't see why it should work.

  4. #4
    Join Date
    Jun 2008
    Posts
    52

    Re: Counting Lines in RichBox with labels

    well i really can't come up with a way to do it, i did this:

    Code:
            public Label label = new Label();
    
            private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                {
                    int number = int.Parse(label.Text);
                    int text = number + 1;
                    label.Text = text.ToString();
                    label.Location = new Point(3, 13 * text);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                label.Name = "label";
                label.Text = "0";
                label.Location = new Point(3, 0);
                panel1.Controls.Add(label);
            }
    but that only moves the label down and doesn't create a new one, anyone have an idea on how to do it?

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Counting Lines in RichBox with labels

    Quote Originally Posted by Korupt
    well i really can't come up with a way to do it, i did this:

    Code:
            public Label label = new Label();
    
            private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                {
                    int number = int.Parse(label.Text);
                    int text = number + 1;
                    label.Text = text.ToString();
                    label.Location = new Point(3, 13 * text);
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                label.Name = "label";
                label.Text = "0";
                label.Location = new Point(3, 0);
                panel1.Controls.Add(label);
            }
    but that only moves the label down and doesn't create a new one, anyone have an idea on how to do it?
    you forgot multiple lines of code in the KeyUp event

    Code:
    Label label = new Label();
    and

    Code:
    panel1.Controls.Add(label);
    also... you could use this code to knock out some lines

    Code:
    label.Text = Convert.ToString((int.Parse(label.Text) + 1))
    label.Location = new Point(3, 13 * (int.Parse(label.Text) + 1));
    Last edited by eclipsed4utoo; September 22nd, 2008 at 02:31 PM.

  6. #6
    Join Date
    Jun 2008
    Posts
    52

    Re: Counting Lines in RichBox with labels

    i did forget the panel.Controls.Add(label) but even with that it still just moves it down, if I add this to the KeyUp event: Label label = new Label(); i get a runtime error saying: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

    Additional information: Input string was not in a correct format.

  7. #7
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Counting Lines in RichBox with labels

    Quote Originally Posted by Korupt
    i did forget the panel.Controls.Add(label) but even with that it still just moves it down, if I add this to the KeyUp event: Label label = new Label(); i get a runtime error saying: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

    Additional information: Input string was not in a correct format.
    you are probably getting that error because you haven't given a name to the label.

    Code:
    Label label = new Label();
    label.Name = "label" + Convert.ToString(int.Parse(label.Text) + 1);

  8. #8
    Join Date
    Jun 2008
    Posts
    52

    Re: Counting Lines in RichBox with labels

    Quote Originally Posted by eclipsed4utoo
    you are probably getting that error because you haven't given a name to the label.

    Code:
    Label label = new Label();
    label.Name = "label" + Convert.ToString(int.Parse(label.Text) + 1);
    nope, same error

  9. #9
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Counting Lines in RichBox with labels

    what number are you putting in the textbox?

  10. #10
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Counting Lines in RichBox with labels

    this works fine for me...

    Code:
            private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    Label label = new Label();
                    label.Name = "label" + textBox1.Text;
                    label.Location = new Point(10,10);
                    label.Size = new Size(30, 30);
                    label.Text = textBox1.Text;
                    panel1.Controls.Add(label);
                }
            }

  11. #11
    Join Date
    Jun 2008
    Posts
    52

    Re: Counting Lines in RichBox with labels

    I don't think you understand what I'm trying to achieve here. There's not supposed to be anything in the text box just whenever you press enter in it it adds a label that +1 bigger then the previews one. Like the line counter in dreamweaver:

    http://img27.picoodle.com/data/img27...4m_4ae7a16.png

  12. #12
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Counting Lines in RichBox with labels

    ok....now I see.

    I believe you problem is that you are always using the same number from the first label(which is probably "1"). So you are creating a second label each time, but it has the same location, size, and text as the label you created when you hit enter the first time.

    maybe add something like this to your code:

    Code:
            int numberOfLabelsCreated = 1;
    
            private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.Enter)
                {
                    int number = numberOfLabelsCreated + 1;
                                       
                        int Ypoint = number * 13;
                        int Y = 13 + Ypoint;
                        Label label = new Label();
                        label.Name = "label" + number.ToString();
                        label.Location = new Point(3, Y);
                        label.Text = number.ToString();
                        panel1.Controls.Add(label);
                   
                    numberOfLabelsCreated++;
                }
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured