2014-09-02 87 views
0

我有一个鼠标按下事件,我写文字的RichTextBox:如何向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(); 
        } 
       } 
      } 
     } 

在鼠标按下事件我写了矩形的位置。

然后在DrawRectangle的方法,我想在实时richTextBox1显示矩形的大小:

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

现在的问题是,当我让鼠标按钮按下鼠标左键单击我看到位置,但随后当我移动鼠标左右拖动它时,位置线被删除,而我看到的是尺寸线。如果在DrawRectangle方法中,我也使+ =它只是添加尺寸线,很多时候填充整个richTextBox,当我移动/拖动鼠标aorund。

+0

richTextBox1.Text + =“Rectangle Location:”+ e.Location + Environment.NewLine; 使用这种语法 – Partha 2014-09-02 23:07:30

回答

0

您需要每次追加。你现在正在做的是覆盖已经存在的文本。

此:

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

..says:“在RichTextBox文本的值必须只能是我提供什么

然而,这:

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

..says :“richtextbox文本必须是它目前的内容...加上这些额外的东西。”

+0

西蒙它添加了两条线,但第一条和第一条靠近它,但在第一行。它没有把它添加到一个新的行。它看起来像例如:大小:22,22地点:233,44都在第一行。 – 2014-09-02 23:11:22

+0

确保写入'Size'的另一段代码也有'+ ='。 – 2014-09-02 23:13:07

+0

Simon我将位置线移动到鼠标按下事件,所以当我点击鼠标左键时,它会显示richTextBox中的位置。 richTextBox1.Text + =“Rectangle Location:”+ e.Location + Environment.NewLine;鼠标向下事件中的同一行。第二行是在它现在在DrawRectangle方法中的位置,当我单击鼠标左键时,我看到了位置,但是当我移动鼠标拖动鼠标时,行被删除,而是从DrawRectangle中看到尺寸线方法。 – 2014-09-02 23:16:35

0

有一个标志可变BLE。

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

首先我想给你一些建议。当使用if语句时,如果(isCroppedList [listBox1.SelectedIndex] == true)当您仅使用 if(isCroppedList [listBox1.SelectedIndex]),则无需执行if(isCroppedList [listBox1.SelectedIndex] == true)。 可能解决您的问题的解决方案可能是:检查多行设置为True,或尝试richTextBox.AppendText(txt)。