Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi
i want draw a line when press Enter key

like this


Hello World
___________________________
i am jack
___________________________
H r u jack?
___________________________


i cant :(

please help me
Posted
Updated 19-Nov-19 17:47pm

Hi, this code works for me:

VB
Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
    If e.KeyCode = Keys.Return Then
        RichTextBox1.AppendText("_______________")
        RichTextBox1.AppendText(Environment.NewLine)
    End If
End Sub


Have a nice day.
 
Share this answer
 
Comments
maxvel 20-Sep-13 7:41am    
HIno no

no append text :S

Draw the Line!
You can also do that in javascript detect enter key and then append the line and new line.
Can you give me link to the demo of that rich text editor.
 
Share this answer
 
If you want to draw in a control use :

http://msdn.microsoft.com/fr-fr/library/system.windows.forms.control.creategraphics.aspx[^]

http://msdn.microsoft.com/fr-fr/library/system.drawing.graphics.aspx[^]


Exemple :

VB
Dim gdi As Graphics = richTextBox1.CreateGraphics()
gdi.DrawLine(Pens.Black, new Point(0, (richTextBox1.Lines.Length + 1) * 10), new Point(50, (richTextBox1.Lines.Length + 1) * 10))
gdi.Dispose()
 
Share this answer
 
v3
 
Share this answer
 
Private Sub RichTextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
      ' Get current line number in RichTextBox
      Dim currentLine As Int32 = 0
      Dim P As Point = New Point(0)
      GetCaretPos(P)
      Dim Pos As Integer = Me.RichTextBox1.GetCharIndexFromPosition(P)
      currentLine = Me.RichTextBox1.GetLineFromCharIndex(Pos)

      ' Presume you manage to get line height and line space
      Dim lineHeight As Double
      Dim lineSpace As Double

      ' You can get current Y coordinate in RichTextBox
      Dim Y As Int32 = Math.Floor((lineHeight + lineSpace) * currentLine)

      If e.KeyCode = Keys.Enter Then

          Dim myPen As New System.Drawing.Pen(Color.Red)
          Dim formGraphics As System.Drawing.Graphics
          formGraphics = RichTextBox1.CreateGraphics()
          formGraphics.DrawLine(myPen, 0, Y, Me.RichTextBox1.Width, Y)
          myPen.Dispose()
          formGraphics.Dispose()
      End If

  End Sub



this code draw line on top of rich text box! i want draw line Bottom of each line!



and this code draw a line on bottom of rich text box but not Fixed


Dim currentLine As Int32 = 0

       Dim P As Point = New Point(0)

       GetCaretPos(P)

       Dim Pos As Integer = Me.RichTextBox1.GetCharIndexFromPosition(P)

       currentLine = Me.RichTextBox1.GetLineFromCharIndex(Pos)



       'MessageBox.Show(currentLine)

       ' Presume you manage to get line height and line space

       If currentLine > 0 Then

           Dim lineHeight As Double

           Dim lineSpace As Double



           ' You can get current Y coordinate in RichTextBox

           Dim Y As Int32 = Math.Floor((lineHeight + lineSpace) * currentLine)



           If e.KeyCode = Keys.Enter And RichTextBox1.Lines(currentLine - 1) = "" Then

               Dim myP As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)

               Dim myPen As New System.Drawing.Pen(Color.Black)

               Dim formGraphics As System.Drawing.Graphics



               formGraphics = RichTextBox1.CreateGraphics() 'the control where you want to paint the line

               formGraphics.DrawLine(myPen, myP.X, myP.Y, Me.RichTextBox1.Width, myP.Y)

               myPen.Dispose()

               formGraphics.Dispose()

               'TextBox1.Text = myP.Y & " " & myP.X

           End If

       End If


and this code was not true

VB
RichTextBox1.AppendText("HI")

       Dim gdi As Graphics = RichTextBox1.CreateGraphics()
       gdi.DrawLine(Pens.Black, New Point(0, (RichTextBox1.Lines.Length + 1) * 10), New Point(50, (RichTextBox1.Lines.Length + 1) * 10))
       gdi.Dispose()

       RichTextBox1.AppendText(vbNewLine)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900