简体   繁体   中英

How do I add a new line to richTextBox?

I have a mouse down event and I'm writing text to richTextBox:

private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
        {
            richTextBox1.Text += "Rectangle Location: " + e.Location + Environment.NewLine;
            Bitmap bmp;
            Point pnt;

            if (e.Button == MouseButtons.Right)
            {
                cm.Show(pictureBoxSnap, e.Location);

                return;
            }

            if (e.Button == MouseButtons.Left)
            {
                if (checkError(listBox1.SelectedIndex, "pictureBoxSnap_MouseDown") == true) { return; } //if listBox1 empty

                if (isCroppedList[listBox1.SelectedIndex] == true) { return; }

                mouseDown = e.Location;

                if (canDrawNormallyList[listBox1.SelectedIndex] == false)
                {
                    bmp = new Bitmap(pictureBoxSnap.Width - 4, pictureBoxSnap.Height - 4);

                    pnt = PointToScreen(pictureBoxSnap.Location);

                    g = Graphics.FromImage(bmp);

                    g.CopyFromScreen(pnt.X + 2, pnt.Y + 2, 0, 0, new Size(bmp.Width, bmp.Height));

                    g.Dispose();

                    windowsBitmaps[listBox1.SelectedIndex] = bmp;
                }
                else
                {
                    if (thumb != IntPtr.Zero)
                    {
                        MessageBoxButtons buttons = MessageBoxButtons.OK;
                        MessageBoxIcon icon = MessageBoxIcon.Error;

                        MessageBox.Show("ERROR in pictureBoxSnap_MouseDown: thumb != IntPtr.Zero", "ERROR", buttons, icon);

                        this.Close();
                    }
                }
            }
        }

In the mouse down event I'm writing the location of the rectangle.

Then in the DrawRectangle method I want to display in the richTextBox1 in real time the size of the rectangle:

private void DrawRectangle(Point pnt)
        {
            g = Graphics.FromImage(img);

            g.Clear(Color.FromKnownColor(KnownColor.Control));

            if (pnt.X == mouseDown.X || pnt.Y == mouseDown.Y)
            {
                g.DrawLine(Pens.Firebrick, mouseDown.X, mouseDown.Y, pnt.X, pnt.Y);
            }
            else
            {
                g.DrawRectangle(Pens.Firebrick, Math.Min(mouseDown.X, pnt.X), Math.Min(mouseDown.Y, pnt.Y),
                            Math.Abs(mouseDown.X - pnt.X), Math.Abs(mouseDown.Y - pnt.Y));
                richTextBox1.Text = "Rectangle Size: " + Math.Abs(mouseDown.X - pnt.X) +
                    Math.Abs(mouseDown.Y - pnt.Y);
            }

            g.Dispose();

            g = frm.CreateGraphics();

            g.DrawImage(img, 0, 0, img.Width, img.Height);

            g.Dispose();
        }

The problem now is that when i make mouse down left click on mouse button i see the location but then when i move the mouse around drag it around the location line is deleted and i see the size line instead. If in the DrawRectangle method i also make += it's just adding the size line many time fill the whole richTextBox with that when i move/drag the mouse aorund.

You need to append each time. What you're doing currently is overwriting the text that is already there.

This:

richTextBox1.Text = "Rectangle Location: " + e.Location + Environment.NewLine;

..says: "The value of the richtextbox text must ONLY be what I've provided.

This however:

richTextBox1.Text += "Rectangle Location: " + e.Location + Environment.NewLine;
//               ^^^^ append

..says: "The richtextbox text must be what it currently is ... plus this extra stuff."

Have a flag variable.

int flag=0;
if(flag==0)
{
richTextBox1.Text += "Rectangle Location: " + e.Location;
flag++;
}
else
{
richTextBox1.Text += Environment.NewLine + "Rectangle Location: " + e.Location + ;
}

First of all I would like to give you some advice. When using if statement, it is unneccessary to do if (isCroppedList[listBox1.SelectedIndex] == true) when you can just use if (isCroppedList[listBox1.SelectedIndex] ). The solution that might be solve your problem could be: Check if the Multiline is set to True,or try richTextBox.AppendText(txt).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM