Home All Groups Group Topic Archive Search About

[VB2005EE] accessing control-specific functions via form's ActiveControl

Author
11 May 2006 1:06 PM
Aphazel
I got serveral textboxes in a form and want to copy/cut/paste text to
clipboard from currently selected (focused) textbox.

It's simple to cut/copy/paste when using specified control directly ie
Me.TextBox1.Cut()

But there's no .Cut() function when i try to use Me.ActiveControl. Is the a
way to access textbox'es .Cut() via ActiveControl knowing that it's a
textbox object?

I also tried an other way:
Clipboard.SetText(Me.ActiveControl.Text)
and
Clipoboard.SetText(Me.ActiveControl.Text.ToString())
but it produces errors.

Does anyone knows a solution?
Thx, Aph

Author
11 May 2006 2:25 PM
Dragon
Hi,

Form.ActiveControl is of type Control, so you need to cast it manually:

~
        If TypeOf Me.ActiveControl Is TextBox Then
            DirectCast(Me.ActiveControl, TextBox).Cut()
        End If
~

Roman
Author
11 May 2006 9:42 PM
Aphazel
Sup :)
This is exaclty what I wanted. THX, I owe you man :)))
I used to use similar stuff in Delphi and BCB, just couldn't find easy
enough what's the syntax in VB.Net so I've dediced to ask here.

Aph

Show quoteHide quote
> ~
>        If TypeOf Me.ActiveControl Is TextBox Then
>            DirectCast(Me.ActiveControl, TextBox).Cut()
>        End If
> ~
Author
11 May 2006 2:54 PM
Kerry Moorman
Aph,

Here is one way:

        Dim tbox As TextBox

        tbox = CType(Me.ActiveControl, TextBox)
        tbox.SelectAll()
        tbox.Copy()

Kerry Moorman


Show quoteHide quote
"Aphazel" wrote:

> I got serveral textboxes in a form and want to copy/cut/paste text to
> clipboard from currently selected (focused) textbox.
>
> It's simple to cut/copy/paste when using specified control directly ie
> Me.TextBox1.Cut()
>
> But there's no .Cut() function when i try to use Me.ActiveControl. Is the a
> way to access textbox'es .Cut() via ActiveControl knowing that it's a
> textbox object?
>
> I also tried an other way:
> Clipboard.SetText(Me.ActiveControl.Text)
> and
> Clipoboard.SetText(Me.ActiveControl.Text.ToString())
> but it produces errors.
>
> Does anyone knows a solution?
> Thx, Aph
>
>
>
>
Author
11 May 2006 9:42 PM
Aphazel
Thx for your advice Kerry :) however I've always liked this style more:
> DirectCast(Me.ActiveControl, TextBox).Cut()
It's just my personal preference but I don't like to declare additionas dims
for such actions.
Which way is better? I'm not the one to judge here, not enough knowledge ;)
It's just IMHO code looks better this way. Neat, I'd say. :D

Aph

Show quoteHide quote
> Here is one way:
>
>        Dim tbox As TextBox
>
>        tbox = CType(Me.ActiveControl, TextBox)
>        tbox.SelectAll()
>        tbox.Copy()
>
> Kerry Moorman
>