Cut, Copy, Paste [Text Only]

Posted on 2005-04-06
Last Modified: 2010-04-23
OK, This one has been bugging me for awhile. I have a top-level form that has a toolbar w/ Cut, Copy, Paste buttons. Inside this form I have a series of custom controls that also could potentially have other forms inside (loaded from a .dll via reflection), ultimately ending at a textbox that might or might not have focus. I'm just trying to get this clipboard functionality working universally, regardless of what form this textbox is a control of. I've already tried doing me.form.activeform.activecontrol & this just returns a reference to the top-level custom control.

Does anyone have some code that will drill through the focus forms/controls until it ultimately ends up at the focus textbox? As ugly as it sounds, I'm ready to just make these buttons emulate Ctrl-C, Ctrl-X, and Ctrl-V !! Please help. Thanks in advance.
Question by:AgentSmith007
    LVL 96

    Accepted Solution

    Find control recursively:

      Public Shared Function FindControlByNameAndType(ByVal findType As Type, ByVal findName As String, ByVal controls As Control.ControlCollection) As Control

        For Each ctl As Control In controls

          If ctl.Controls.Count > 0 Then
            ' Search recursively in container controls.
            Return FindControlByNameAndType(findType, findName, ctl.Controls)
          End If

          If ctl.GetType.Equals(findType) AndAlso ctl.Name.Equals(findName) Then
            Return ctl
          End If

        Next ctl

      End Function

    Dim control As Control = FindControlByNameAndType(GetType(TextBox), "TextBox1", Me.Controls)

    LVL 9

    Author Comment


    This helps greatly. I am not, however, looking based on name and type. I guess my criteria for returning the valid control is if it passes the type check AND it has focus. Is there a property of the textbox or shared control property that lets me check if the control currently has focus?

    LVL 9

    Author Comment

    Don't worry about re-writing your code to reflect the type only needed, I've done that, I just need to know how to check if a control has focus.
    LVL 96

    Expert Comment

    by:Bob Learned
    This isn't easily apparent, but there is the Control.Focused property.  The reason it's not apparent is that the auto-complete doesn't show this property.  You just have to know it's there.  

    LVL 9

    Author Comment

    Hmm, well focused didn't work but either way I think I can work it out from here. Thanks for your help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now