0

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.

Mary
  • 14,926
  • 3
  • 18
  • 27
  • You mention an Exception and generalize it as "X is not working", please include details (Exception message and stacktrace) instead (by editting your question). – Fixation Jan 21 '20 at 10:59
  • Instead of saving the name of the control, save the control. You can get the type of the control without using any magic property of its name: [Determining Object Type](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-binding/determining-object-type). – Andrew Morton Jan 21 '20 at 11:09
  • 2
    Also, you should use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) to avoid errors like `If MsgBoxResult.Yes Then`. – Andrew Morton Jan 21 '20 at 11:10
  • @Fixation `System.NullReferenceException: Object reference not set to an instance of an object. at NSForms.Form1.pasteit(Object sender, EventArgs e) in E:\Nexsales\NSForms\Form1.vb:line 95` – Sameer Chaturvedi Jan 21 '20 at 14:03
  • @AndrewMorton I tried with 'typeof' prior to posting this here. but got the same issue. i am able to paste text easily in Richtextbox/Textbox that is on the form, but not in the Richtextbox/Textbox placed in a groupbox. thanks for your suggestion to use Option Strict, i'll incorporating it in my code, as soon as this issue is sorted. – Sameer Chaturvedi Jan 21 '20 at 14:15
  • 1
    @SameerChaturvedi Note that `Me.Controls` only looks in `Me`, it does not look further into controls in `Me`. You can write a recursive function instead. Or, just use the actual control's reference instead of its name. I recommend switching on Option Strict first, as it may solve other issues that you haven't discovered yet. – Andrew Morton Jan 21 '20 at 14:35
  • I did switch on Option Strict, and it has given me lot to work with. One thing that i still do not understand, is that the mousehover is correctly returning the name of the control i'm about to click. and i also have tried with removing `Me` from the code but still no progress – Sameer Chaturvedi Jan 22 '20 at 09:25
  • Resolved. @AndrewMorton Thanks, instead of `Me.Controls` I used `Groupbox.controls` Works – Sameer Chaturvedi Jan 23 '20 at 13:14

0 Answers0