Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Cut, Copy, Paste [Text Only]

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.
0
AgentSmith007
Asked:
AgentSmith007
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
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


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


Bob
0
 
AgentSmith007Author Commented:
Bob,

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?

Scott
0
 
AgentSmith007Author Commented:
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.
0
 
Bob LearnedCommented:
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.  

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now