kulfanumber
Programmer
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
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