Click to See Complete Forum and Search --> : Counting Lines in RichBox with labels


Korupt
September 21st, 2008, 09:46 PM
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:


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.

MNovy
September 22nd, 2008, 12:24 AM
I guess your Y is not increasing.

Talikag
September 22nd, 2008, 03:11 AM
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.

Korupt
September 22nd, 2008, 01:53 PM
well i really can't come up with a way to do it, i did this:

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?

eclipsed4utoo
September 22nd, 2008, 02:28 PM
well i really can't come up with a way to do it, i did this:

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


Label label = new Label();


and


panel1.Controls.Add(label);


also... you could use this code to knock out some lines


label.Text = Convert.ToString((int.Parse(label.Text) + 1))
label.Location = new Point(3, 13 * (int.Parse(label.Text) + 1));

Korupt
September 22nd, 2008, 02:38 PM
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.

eclipsed4utoo
September 22nd, 2008, 02:57 PM
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.


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

Korupt
September 22nd, 2008, 03:28 PM
you are probably getting that error because you haven't given a name to the label.


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


nope, same error

eclipsed4utoo
September 22nd, 2008, 08:35 PM
what number are you putting in the textbox?

eclipsed4utoo
September 22nd, 2008, 09:03 PM
this works fine for me...


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);
}
}

Korupt
September 22nd, 2008, 09:30 PM
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/3/9/22/f_Untitled4m_4ae7a16.png

eclipsed4utoo
September 22nd, 2008, 10:02 PM
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:


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++;
}
}