Results 1 to 18 of 18

Thread: Adding text box to rich text box

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Adding text box to rich text box

    I'm developing a urdu editor software. I want to add text box to rich text box by clicking at a menu item. I have coded the rich text box for urdu language. The problem is how to use the same coding for the text box?

    Thnx

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Adding text box to rich text box

    A standard textbox doesn't support that.

    If you need two textboxs for some reason, why aren't you using two richtextboxs?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    I am using a rich text box and i need to place a textbox on it at any position by clicking athe menu entry

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding text box to rich text box

    you can try like this, it does not put the textbox in the richtextbox but over it
    vb Code:
    1. Text1.Move RichTextBox1.Left, RichTextBox1.Top
    2. Text1.ZOrder 0
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    thnx i will try that

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    I have tried that but its not working. I have added following code to the click event of "Add Text" menu entry
    Code:
    Dim Text1 As TextBox
    With Text1
       ZOrder 0
    End With
    When i run it nothing happens. Any ideas.....

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding text box to rich text box

    if you are creating a textbox at run time you need to add to the controls collection

    if you are working with a textbox created at design time, do not declare it as a variable
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Adding text box to rich text box

    How about the following code

    Code:
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    
    Private Sub Command1_Click()
        SetParent Text1.hWnd, RichTextBox1.hWnd
    End Sub
    It will put the textbox inside the richtextbox, is that what you want?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    Its giving the runtime error # 91
    Object variable or with block variable not set

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding text box to rich text box

    i tested dee-u code in a new project, works fine
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Adding text box to rich text box

    Quote Originally Posted by yuleball
    Its giving the runtime error # 91
    Object variable or with block variable not set
    You need to have a RichTextBox control in your form named RichTextBox1, and a Textbox named Text1. How are you doing it?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    I have done that. thank you. Now problem is that i want to place this text box at the position of cursor e-g if the cursor is at line no 3 then this text box should be added at line no 3. Also when i have finished entering text in this text box, i want that cursor is placed below this text box.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding text box to rich text box

    i don't believe you can do like that, i think the text in the richtext box will continue under /behind the textbox

    looks like you should consider making a usercontrol
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    How can I do that?

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding text box to rich text box

    i had a thought about this, if you place an image (blank, no picture) of appropriate size (or resize to suit) in the richtextbox, then place your textbox over the image, the text should appear to wrap around the textbox

    i have never tried to do this, and it might take some trial and error to make the textbox and image match size and position
    in any case if you save or print the richtext using methods of the richtextbox the textbox will not be included
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Adding text box to rich text box

    Quote Originally Posted by yuleball
    Now problem is that i want to place this text box at the position of cursor e-g if the cursor is at line no 3 then this text box should be added at line no 3.
    Heres a really-really simple example to position a textbox on the line the cursor is at, doesn't take into account mixed fonts/sizes, moving the vertical scrollbar while textbox is shown or anything like that though.

    Code:
    Option Explicit
    ' for scroll bar info
    Private Declare Function GetScrollInfo Lib "user32.dll" (ByVal hwnd As Long, ByVal n As Long, ByRef lpScrollInfo As SCROLLINFO) As Long
    Private Const SB_VERT As Long = 1
    Private Const SIF_RANGE As Long = &H1
    Private Const SIF_PAGE As Long = &H2
    Private Const SIF_POS As Long = &H4
    Private Type SCROLLINFO
        cbSize As Long
        fMask As Long
        nMin As Long
        nMax As Long
        nPage As Long
        nPos As Long
        nTrackPos As Long
    End Type
    'for GetCurrentLine
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
     ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const EM_LINEFROMCHAR = &HC9
    ' for get height of text
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" _
    (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
    ' var to hold height of text
    Dim TxtHeight As Long
    
    Private Sub ShowTextBox()
        Dim TxtTop As Long, CurLine As Long
        Dim sINFO As SCROLLINFO
        ' find pos of vert scrollbar
        sINFO.cbSize = Len(sINFO)
        sINFO.fMask = SIF_RANGE Or SIF_PAGE Or SIF_POS
        GetScrollInfo RichTextBox1.hwnd, SB_VERT, sINFO
        ' what line is cursor on
        CurLine = GetCurrentLine(RichTextBox1)
        If CurLine > 0 Then TxtTop = (CurLine * TxtHeight)
        ' show Textbox
        Text1.Visible = True
        Text1.ZOrder 0
        Text1.Move 0, TxtTop - sINFO.nPos
        Text1.SetFocus
    End Sub
    
    Private Function GetCurrentLine(RTB As RichTextBox) As Long
        GetCurrentLine = SendMessage(RTB.hwnd, EM_LINEFROMCHAR, ByVal RTB.SelStart, ByVal 0& + 1)
    End Function
    
    Private Sub Command1_Click()
        ShowTextBox ' show text box
    End Sub
    
    Private Sub Form_Load()
        Dim i As Integer
        Dim TextSize As POINTAPI
        Text1.Visible = False
        Me.ScaleMode = vbPixels
        Me.Font = "Courier New"
        Me.FontSize = 12
        GetTextExtentPoint32 Me.hdc, "8W", 2, TextSize ' get size of font
        TxtHeight = TextSize.Y ' save text height
        ' set RTB font/size same as form so text height matches.
        RichTextBox1.Font = Me.Font
        RichTextBox1.Font.Size = Me.FontSize
        ' add some test text to the RTB control
        RichTextBox1.Text = ""
        For i = 1 To 20
            RichTextBox1.Text = RichTextBox1.Text & "Line Number " & CStr(i) & vbCrLf
        Next i    
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then
            KeyAscii = 0
            Text1.Visible = False
        End If
    End Sub

  17. #17

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    13

    Re: Adding text box to rich text box

    I have tried it but its not working. plz help

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding text box to rich text box

    what do you mean by
    I have tried it but its not working
    ?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width