pro_vb_dotnet thread: get the active control (eg textbox) on a window form for copy in
vb.net
Thank you Duncan.
I used this line of code to get the active control on my form.
Me.mainForm.ActiveControl.GetContainerControl.ActiveControl
After this, I determined the type of the active control.
Thank you.
Zude
> Hi
Sorry I misunderstood your question, I had a similar issue trying find
the active text box on a tab control, and I was forwarded this code and
it works a treat. There is a controls property on a form and on each
control. Eg. A Panel can hold 4 controls (2 lables + 2 textboxes.) How
to loop through all ......? Recursion is the answer.....
Private Sub ProcessControls(ByRef objControls As
Windows.Forms.Control.ControlCollection)
Dim objTempControl As Windows.Forms.Control
Dim objTempComboBox As Windows.Forms.ComboBox
Dim objTempRadioButton As Windows.Forms.RadioButton
Dim objTempCheckBox As Windows.Forms.CheckBox
Try
For Each objTempControl In objControls
If (TypeOf objTempControl Is Windows.Forms.TextBox) Or _
(TypeOf objTempControl Is
Windows.Forms.DateTimePicker) Or _
(TypeOf objTempControl Is
Windows.Forms.NumericUpDown) Then
-- Do what you want with the control
ElseIf TypeOf objTempControl Is Windows.Forms.ComboBox
Then
objTempComboBox = objTempControl
objTempComboBox.ContextMenu = objMenuClearComboBoxes
-- Do what you want with the control
ElseIf TypeOf objTempControl Is
Windows.Forms.RadioButton Then
objTempRadioButton = objTempControl
-- Do what you want with the control
ElseIf TypeOf objTempControl Is Windows.Forms.CheckBox
Then
objTempCheckBox = objTempControl
-- Do what you want with the control
ElseIf (TypeOf objTempControl Is Windows.Forms.Panel) Or
_
(TypeOf objTempControl Is Windows.Forms.GroupBox)
Or _
(TypeOf objTempControl Is
Windows.Forms.TabControl) Then
' Do the recursion thing.....
Me.ProcessControls(objTempControl.Controls)
End If
Next
Catch objException As Exception
Throw New Exception(objException.ToString())
End Try
End Sub
In a method elsewhere.....
Me.ProcessControls(Me.Controls)
Original code from Jannie Strydom
HTH
Duncan
-->-----Original Message-----
-->From: Zude Weng [mailto:zweng@t...]
-->Sent: 16 February 2003 19:23
-->To: pro_VB_dotnet
-->Subject: [pro_vb_dotnet] RE: get the active control (eg
-->textbox) on a window form for copy in vb.net
-->
-->
-->Duncan,
-->Thank you for the reply. My post was about how to find the
-->ActiveControl
-->on the form I described. I found the way to locate the activated
-->textbox. The ActiveControl only returns the TabControl on my form.
-->
-->However, you answer simplifies my code for the copy, cut and
-->paste. I
-->didn't realize that you can do it this simple.
-->
-->Thank you very much.
-->
-->Zude
-->>
-->Hi
-->
-->A nice feature in vb.net is that cut, copy, paste etc.. Are
-->methods of the text box now lengthy coding as before in VB6.
-->I'm using the following in an app I'm developing, though I'm
-->only using a menu and not a tool bar.
-->
-->If you have a menu item then use this
-->Private Sub mnuEditCopy_Click(ByVal sender As Object, ByVal e As
-->System.EventArgs) Handles mnuEditCopy.Click
--> Dim objTextBox As TextBox = Me.ActiveControl
-->
--> 'Copy the selected text
--> objTextBox.Copy()
-->End Sub
-->
-->
-->You would also need to add after the "Handles
-->mnuEditCopy.Click" the following ", toolbarbutton.click".
-->What this does is When you click the toolbarbutton the
-->mnuEditCopy_Click event's code will run. If you don't have a
-->menu place the above code in the click event of the button,
-->it should add the correct handler so you won't need to add it.
-->
-->Duncan
-->
-->
-->-->-----Original Message-----
-->-->From: Zude Weng [mailto:zweng@t...]
-->-->Sent: 14 February 2003 22:12
-->-->To: pro_VB_dotnet
-->-->Subject: [pro_vb_dotnet] get the active control (eg textbox)
-->-->on a window form for copy in vb.net
-->-->
-->-->
-->-->Hi, All,
-->-->
-->-->
-->-->
-->-->I have posted to vb_dotnet also. And I repost here to see
-->-->if I can get help from more pros. Sorry if you are on the
-->-->two lists and see the same message.
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->I have a form with a panel, which has a usercontrol. The
-->-->usercontrol contains a tabcontrol, which in turn as a couple
-->-->of textboxes. The form also has a toolbar with a Copy
-->-->button there. Now, if one of the textboxes is selected for
-->-->copying the text, how would this control be detected?
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->Attached is a zip file for this simple app. Thank you very
-->-->much for the help.
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->Zude
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->
-->-->---
-->-->VB.NET Handbooks Library Special Offer
-->-->
-->-->Get a massive 40% off this complete library
-->-->of books (US residents only at the moment).
-->-->Free ground shipping on this 10 book bundle!
-->-->
-->-->http://www.wroxdirect.com/proddescription.asp?Bookselect=BUN10E004
-->-->---
-->-->Change your mail options at http://p2p.wrox.com/manager.asp or
-->-->to unsubscribe send a blank email to
-->-->
-->-->
-->
-->---
-->VB.NET Handbooks Library Special Offer
-->
-->Get a massive 40% off this complete library
-->of books (US residents only at the moment).
-->Free ground shipping on this 10 book bundle!
-->
-->http://www.wroxdirect.com/proddescription.asp?Bookselect=BUN10E004
-->---
-->Change your mail options at http://p2p.wrox.com/manager.asp or
-->to unsubscribe send a blank email to
-->
-->