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.
Re: Counting Lines in RichBox with labels
I guess your Y is not increasing.
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.
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?
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));
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.
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);
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
Re: Counting Lines in RichBox with labels
what number are you putting in the textbox?
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);
}
}
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
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++;
}
}