I'm developing a solution for my work where user copies text and pastes in the form. the code shared below works smoothly for textbox/richtextbox placed on the form outside the group box. where as throws exception when tried to paste text in a textbox inside a groupbox.
there are three types of forms which have some common textbox(s) those are always visible and some are placed in groupbox(s), depending on the type of form selected the groupbox is visible with the relevant textbox/richtextbox.
The code below identifies the name of the control on mouse hover and on mouse click pastes the contents from the clipboard to the textbox/richtextbox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
doSet(Me)
dopaste(Me)
End Sub
Public ctr As Control
Private Sub doSet(ByVal parentCtr As Control)
For Each ctr In parentCtr.Controls
AddHandler ctr.MouseHover, AddressOf MouseHoover
doSet(ctr)
Next
End Sub
Private Sub MouseHoover(sender As Object, e As System.EventArgs)
topaste = DirectCast(sender, Control).Name
End Sub
Private Sub dopaste(ByVal parentCtr As Control)
For Each ctr1 In parentCtr.Controls
AddHandler ctr1.MouseClick, AddressOf pasteit
dopaste(ctr1)
Next
End Sub
Private Sub pasteit(sender As Object, e As System.EventArgs)
Try
If topaste.Contains("TextBox") = True Then
If Clipboard.ContainsText() And Me.Controls(topaste).Text = "" Then
Me.Controls(topaste).Text = Clipboard.GetText()
ElseIf Clipboard.ContainsText = False Then
MsgBox("Either text not copied or not in compatible format")
ElseIf Me.Controls(topaste).Text.Equals("") = False Then
MsgBox("There is text in the text box. Do you want to replace this text?", vbYesNo)
If MsgBoxResult.Yes Then
Me.Controls(topaste).Text = ""
Me.Controls(topaste).Text = Clipboard.GetText()
End If
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Please suggest what needs to be done.