Results 1 to 13 of 13

Thread: Make textbox appear after a button is clicked

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    57

    Make textbox appear after a button is clicked

    Hi
    How do I make a textbox appear inside rich text box by clicking on a button. I want the new textbox to appear where the cursor is

    Thanks

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Make textbox appear after a button is clicked

    Perhaps by explaining to us what you are trying to do, we can provide a better solution.

    For now, as far as I know, you can't place a TextBox inside of a RichTextBox because it is not a container.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Make textbox appear after a button is clicked

    I very much doubt that you actually want a TextBox to appear inside a RichTextBox. Presumably you want the text that is in the TextBox to be added to the text that is in the RichTextBox. For any control derived from TextBoxBase (TextBox, RichTextBox, MaskedTextBox, etc.) you simply set the SelectedText property to insert text at the current caret position. If text is currently selected it will be replaced. E.g.
    vb.net Code:
    1. myRichTextBox.SelectedText = myTextBox.Text

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    57

    Re: Make textbox appear after a button is clicked

    Quote Originally Posted by weirddemon View Post
    Perhaps by explaining to us what you are trying to do, we can provide a better solution.

    For now, as far as I know, you can't place a TextBox inside of a RichTextBox because it is not a container.
    I want to make a program that allows to use maths symbols etc easily. For example for now it has 3 buttons, one which allows to enter sqrt symbol, one with powers, and one with fractions. So I want two textboxes to appear (one on top of the other) once the fraction button is clicked so that its easy to type in the numbers

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Make textbox appear after a button is clicked

    Well, that just goes to show that a complete explanation is required, because my assumption was incorrect.

    I just did this and it worked OK:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            With Me.TextBox1
                .Clear()
                .Location = Me.PointToClient(Me.RichTextBox1.PointToScreen(Me.RichTextBox1.GetPositionFromCharIndex(Me.RichTextBox1.SelectionStart)))
                .Show()
                .BringToFront()
                .Select()
            End With
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            Me.RichTextBox1.SelectedText = Me.TextBox1.Text
            Me.TextBox1.Hide()
        End Sub
    
    End Class
    When the Button is clicked the TextBox appears and when it loses focus the text it contains is inserted into the RTB.

    EDIT: Note that I added the TextBox to the form normally and set its Visible property to False. I would also suggest setting the Tab order such that the RTB follows the TextBox. That way, when you're entering text into the TextBox you can just hit the Tab key and the TextBox will disappear and the RTB will have focus.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    57

    Re: Make textbox appear after a button is clicked

    Quote Originally Posted by jmcilhinney View Post
    Well, that just goes to show that a complete explanation is required, because my assumption was incorrect.

    I just did this and it worked OK:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            With Me.TextBox1
                .Clear()
                .Location = Me.PointToClient(Me.RichTextBox1.PointToScreen(Me.RichTextBox1.GetPositionFromCharIndex(Me.RichTextBox1.SelectionStart)))
                .Show()
                .BringToFront()
                .Select()
            End With
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            Me.RichTextBox1.SelectedText = Me.TextBox1.Text
            Me.TextBox1.Hide()
        End Sub
    
    End Class
    When the Button is clicked the TextBox appears and when it loses focus the text it contains is inserted into the RTB.

    EDIT: Note that I added the TextBox to the form normally and set its Visible property to False. I would also suggest setting the Tab order such that the RTB follows the TextBox. That way, when you're entering text into the TextBox you can just hit the Tab key and the TextBox will disappear and the RTB will have focus.
    Thanks for the long code, but it doesnt work. It just clears all the text in the RTB

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Make textbox appear after a button is clicked

    Quote Originally Posted by oyeioyei View Post
    Thanks for the long code, but it doesnt work. It just clears all the text in the RTB
    I tested it myself before I posted it and it works in VS 2008. Either you've done something wrong when implementing it for yourself or else you have selected all the text in the RTB first and then you haven't entered anything into the TextBox. Setting the SelectedText will replace the current selection. If nothing is selected then the new text is inserted but if something is already selected then it will be replaced. If the entire contents of the RTB is selected when the TextBox is shown and then no text is entered before the TextBox is hidden, the entire contents of the RTB will be replaced with an empty string. Feel free to adjust the code as you see fit but, as is, it does exactly what it's supposed to do.

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    57

    Re: Make textbox appear after a button is clicked

    Quote Originally Posted by jmcilhinney View Post
    I tested it myself before I posted it and it works in VS 2008. Either you've done something wrong when implementing it for yourself or else you have selected all the text in the RTB first and then you haven't entered anything into the TextBox. Setting the SelectedText will replace the current selection. If nothing is selected then the new text is inserted but if something is already selected then it will be replaced. If the entire contents of the RTB is selected when the TextBox is shown and then no text is entered before the TextBox is hidden, the entire contents of the RTB will be replaced with an empty string. Feel free to adjust the code as you see fit but, as is, it does exactly what it's supposed to do.

    That is the thing, I have never used this kind of code before and when I was implementing it I just did it the way I found best even though I have no idea what it is. Dont understand this code at all. As i said i am new to this so please BEAR with me.

    The code...
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    With Me.txt1
    .Clear()
    .Location = Me.PointToClient(Me.txt1.PointToScreen(Me.txt1.GetPositionFromCharIndex(Me.txt1.SelectionStart)))
    .Show()
    .BringToFront()
    .Select()
    End With
    TextBox1.Visible = True

    End Sub

    Private Sub TextBox1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Me.txt1.SelectedText = Me.txt1.Text
    Me.txt1.Hide()

    End Sub
    End Class
    Last edited by oyeioyei; Nov 14th, 2009 at 08:30 PM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Make textbox appear after a button is clicked

    1. Create a new WinForms project and add a Button, a TextBox and a RichTextBox, in that order.
    2. Set the Visible property of the TextBox to False.
    3. Open the code window and replace the entire contents with the following, which is a commented version of what I posted earlier:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            With Me.TextBox1
                'Clear the current contents of the TextBox.
                .Clear()
    
                'Position the TextBox where the caret is in the RTB.
                .Location = Me.PointToClient(Me.RichTextBox1.PointToScreen(Me.RichTextBox1.GetPositionFromCharIndex(Me.RichTextBox1.SelectionStart)))
    
                'Display the TextBox.
                .Show()
    
                'Make sure the TextBox is in front of the RTB.
                .BringToFront()
    
                'Set focus to the TextBox.
                .Select()
            End With
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            'Replace the current selection in the RTB with the contents of the TextBox.
            Me.RichTextBox1.SelectedText = Me.TextBox1.Text
    
            'Hide the TextBox.
            Me.TextBox1.Hide()
        End Sub
    
    End Class
    4. Run the project.
    5. Click the RTB and start typing.
    6. Click the Button and notice the TextBox appear.
    7. Continue typing and notice the text appear in the TextBox.
    8. Hit the Tab key and notice the TextBox disappear and it's contents appear in the RTB where the caret was.
    9. Click in the RTB somewhere within the text.
    10. Click the Button and type into the TextBox again.
    11. Hit Tab again and notice that the new text is inserted into the RTB where the caret was positioned.
    12. Click and drah in the RTB to select some text.
    13. Click the Button and type into the TextBox again.
    14. Hit Tab again and notice that the new text has replaced what was selected.

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    57

    Re: Make textbox appear after a button is clicked

    Quote Originally Posted by jmcilhinney View Post
    1. Create a new WinForms project and add a Button, a TextBox and a RichTextBox, in that order.
    2. Set the Visible property of the TextBox to False.
    3. Open the code window and replace the entire contents with the following, which is a commented version of what I posted earlier:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            With Me.TextBox1
                'Clear the current contents of the TextBox.
                .Clear()
    
                'Position the TextBox where the caret is in the RTB.
                .Location = Me.PointToClient(Me.RichTextBox1.PointToScreen(Me.RichTextBox1.GetPositionFromCharIndex(Me.RichTextBox1.SelectionStart)))
    
                'Display the TextBox.
                .Show()
    
                'Make sure the TextBox is in front of the RTB.
                .BringToFront()
    
                'Set focus to the TextBox.
                .Select()
            End With
        End Sub
    
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            'Replace the current selection in the RTB with the contents of the TextBox.
            Me.RichTextBox1.SelectedText = Me.TextBox1.Text
    
            'Hide the TextBox.
            Me.TextBox1.Hide()
        End Sub
    
    End Class
    4. Run the project.
    5. Click the RTB and start typing.
    6. Click the Button and notice the TextBox appear.
    7. Continue typing and notice the text appear in the TextBox.
    8. Hit the Tab key and notice the TextBox disappear and it's contents appear in the RTB where the caret was.
    9. Click in the RTB somewhere within the text.
    10. Click the Button and type into the TextBox again.
    11. Hit Tab again and notice that the new text is inserted into the RTB where the caret was positioned.
    12. Click and drah in the RTB to select some text.
    13. Click the Button and type into the TextBox again.
    14. Hit Tab again and notice that the new text has replaced what was selected.
    Thank you very much that way it works but i dont know why its not woking on my one. Is there a way to type in a fraction like in word eqn so on top and bottom there is a text box, divided by a line to make fraction that is waht I wanted to achieve by using this textbox method
    Last edited by oyeioyei; Nov 14th, 2009 at 08:56 PM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Make textbox appear after a button is clicked

    You can only do in a RichTextBox what is supported by the RTF specification. It basically just supports text, but allows you to add markup to format the text with different fonts, colours, etc. What you're talkingabout goes beyond what RTF, and therefore the RichTextBox control, supports. If you want to display fractions like that then you'd have to use GDI+ to draw them yourself because they cannot be represented that way in RTF.

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    57

    Re: Make textbox appear after a button is clicked

    Quote Originally Posted by jmcilhinney View Post
    You can only do in a RichTextBox what is supported by the RTF specification. It basically just supports text, but allows you to add markup to format the text with different fonts, colours, etc. What you're talkingabout goes beyond what RTF, and therefore the RichTextBox control, supports. If you want to display fractions like that then you'd have to use GDI+ to draw them yourself because they cannot be represented that way in RTF.
    What about the method of using the textbox, the one u sent me code of. Any ideas why its not working on my current program, on new it works

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Make textbox appear after a button is clicked

    No idea at all.

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