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
Printable View
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
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 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:
myRichTextBox.SelectedText = myTextBox.Text
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
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:When the Button is clicked the TextBox appears and when it loses focus the text it contains is inserted into the RTB.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
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.
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
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:4. Run the project.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
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:thumb: 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
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.
No idea at all.