Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting textbox inside rtb

Status
Not open for further replies.

kulfanumber

Programmer
Jun 24, 2009
21
0
0
PK
I am developing an MDI editor and i want to add the functionality to allow user to insert as many textboxes into richtextbox as he wishes using mouse. I have added following code to accompish this

Form1 has one richtextbox named RichTextBox1


Public Class Form1
Private i As Integer
Private start As New Point
Private text1 As New TextBox

Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
Me.start = e.Location
If MDIParent1.AddTextBox Then
If e.Button = Windows.Forms.MouseButtons.Left Then
i = 0
End If
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'maximizes the form
Me.WindowState = FormWindowState.Maximized
'maximize rich text box
RichTextBox1.Dock = DockStyle.Fill
End Sub

Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
Dim [end] As Point = e.Location
If MDIParent1.AddTextBox Then
'changes the mouse to arrow while drawing textbox
RichTextBox1.Cursor = Cursors.Arrow
If e.Button = Windows.Forms.MouseButtons.Left Then
If i = 0 Then
'adds only one textbox with mouse move event
text1 = New TextBox
End If
'prevents selecting of text with mouse move
RichTextBox1.SelectionStart = RichTextBox1.TextLength
RichTextBox1.SelectionLength = 0
'sets the location and size of textbox
text1.Location = Me.start
text1.Height = Math.Abs(Me.start.Y - [end].Y)
text1.Width = Math.Abs(Me.start.X - [end].X)
RichTextBox1.Controls.Add(text1)
i += 1
End If
End If
End Sub

Private Sub RichTextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseUp
MDIParent1.AddTextBox = False
RichTextBox1.Cursor = Cursors.IBeam
End Sub
End Class
'MDIParent has button1 to draw textbox
Public Class MDIParent1
Public AddTextBox As Boolean

Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
AddTextBox = True
End Sub
End Class

It works fine as far as adding the textbox is concernedbut I hav few problems like

a)I cant move the textbox with movement of scrollbars of rtb
b)I cant resize the textbox when minimizing form
c)I want to disable the area behind textbox so that user cant write there.
d)Is there any way to detect that user is working in which textbox if i add more than one textbox

Looking forward to some help



 
A) Nope, a rtb isn't a true container. You can add the TextBox because it is an inherited behavior, but it doesn't know what to do with it because it wasn't designed for that.

B) I'm not quite sure what you are asking/saying here. When you minimize the window you still see the text box? If the rtb is minimized then it should as well. It could be a strange side effect since it isn't designed to work like that.

C) You can't disable an area of an rtb. Best you could do is keep track of where the tbs are and add tabs to the text to "skip" that area.

D) Either place something in the mouse enter/leave events for the text box or iterate through the controls of the rtb and find the one that has focus.

Really my best suggestion would be to inheriting from a true container control like a panel and building your own behavior. Someone else might be able to tell you how to create a container class or add said functionality to the rtb (if that is even possible), but that is something I've never tried before.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Ok. thnx for ur reply. Yeah i think its better to create my own rtb control. I'll post it in a separate thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top