Home All Groups Group Topic Archive Search About

Which Control am I working on?

Author
20 Mar 2006 12:20 PM
TheGanjaMan
Hi everyone,
I'm trying to code an event and seem to be stuck on something that might
seem pretty simple (I am a newbie...)

I have two textboxes on a form, I have a button on a form that is
supposed to copy selected text.
When I click the button I want the selected text to be copied into the
clipboard.  I can do this by saying:

Private Sub Copy_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Copy.Click
        If TextBox1.SelectionLength > 0 Then
            My.Computer.Clipboard.SetText(TextBox2.SelectedText,
TextDataFormat.Text)
        End If
        If TextBox2.SelectionLength > 0 Then
            My.Computer.Clipboard.SetText(TextBox1.SelectedText,
TextDataFormat.Text)
        End If
    End Sub

But I want the code to know which textbox has text selected in it without
me having to specify it.  Suppose I had more textboxes for example it
would be tiresome to write the if statement for all the textboxes (or any
control for that matter)...
Is there a way to do this without specifying the control name all the
time, I would love to learn how...

Any help is welcome,
Thanks in advance to anyone willing to help.

Author
20 Mar 2006 12:43 PM
TheGanjaMan
Hi everyone,
Sorry... I just saw something wrong in my code and have to update the
post...
I'm trying to code an event and seem to be stuck on something that might
seem pretty simple (I am a newbie...)

I have two textboxes on a form, I have a button on a form that is
supposed to copy selected text.
When I click the button I want the selected text to be copied into the
clipboard.  I can do this by saying:

Private Sub Copy_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Copy.Click
        If TextBox1.SelectionLength > 0 Then
            My.Computer.Clipboard.SetText(TextBox1.SelectedText,_
TextDataFormat.Text)
        End If
        If TextBox2.SelectionLength > 0 Then
            My.Computer.Clipboard.SetText(TextBox2.SelectedText,_
TextDataFormat.Text)
        End If
    End Sub

But I want the code to know which textbox has text selected in it
without me having to specify it.  Suppose I had more textboxes for
example it would be tiresome to write the if statement for all the
textboxes (or any control for that matter)...
Is there a way to do this without specifying the control name all the
time, I would love to learn how...

Any help is welcome,
Thanks in advance to anyone willing to help.
Author
20 Mar 2006 1:12 PM
C-Services Holland b.v.
TheGanjaMan wrote:

Show quoteHide quote
> Hi everyone,
> Sorry... I just saw something wrong in my code and have to update the
> post...
> I'm trying to code an event and seem to be stuck on something that might
> seem pretty simple (I am a newbie...)

> I have two textboxes on a form, I have a button on a form that is
> supposed to copy selected text.
> When I click the button I want the selected text to be copied into the
> clipboard.  I can do this by saying:

> Private Sub Copy_Click(ByVal sender As System.Object, ByVal e As_
> System.EventArgs) Handles Copy.Click
>         If TextBox1.SelectionLength > 0 Then
>             My.Computer.Clipboard.SetText(TextBox1.SelectedText,_
> TextDataFormat.Text)
>         End If
>         If TextBox2.SelectionLength > 0 Then
>             My.Computer.Clipboard.SetText(TextBox2.SelectedText,_
> TextDataFormat.Text)
>         End If
>     End Sub
>
> But I want the code to know which textbox has text selected in it
> without me having to specify it.  Suppose I had more textboxes for
> example it would be tiresome to write the if statement for all the
> textboxes (or any control for that matter)...
> Is there a way to do this without specifying the control name all the
> time, I would love to learn how...

> Any help is welcome,
> Thanks in advance to anyone willing to help.

Maybe something like using a for each loop to check every
control(textbox) to see if something is selected.


--
Rinze van Huizen
C-Services Holland b.v
Author
20 Mar 2006 2:37 PM
R. MacDonald
Hello, GanjaMan,

I don't know anything about "My.Computer.Clipboard", but I think what
you are doing here will not give you what you want.  For example, if
both TextBoxes have selected text, you will always get the text in TextBox2.

You might consider setting a module scope string to the selected text in
the validate event of the TextBox, and using that in your button click
event.

Something like:

   Private mstrSelectedText As String = ""

   Private Sub TextBox_Validated(ByVal sender As Object, _
                                 ByVal e As System.EventArgs) _
                                       Handles TextBox1.Validated, _
                                       TextBox2.Validated
         mstrSelectedText = DirectCast(sender, TextBox).SelectedText
   End Sub

and then set the clipboard text using mstrSelectedText.  If you have a
lot of TextBoxes, you can set the handler in a loop using AddHandler.

Cheers,
Randy


TheGanjaMan wrote:

Show quoteHide quote
> Hi everyone,
> Sorry... I just saw something wrong in my code and have to update the
> post...
> I'm trying to code an event and seem to be stuck on something that might
> seem pretty simple (I am a newbie...)

> I have two textboxes on a form, I have a button on a form that is
> supposed to copy selected text.
> When I click the button I want the selected text to be copied into the
> clipboard.  I can do this by saying:

> Private Sub Copy_Click(ByVal sender As System.Object, ByVal e As_
> System.EventArgs) Handles Copy.Click
>         If TextBox1.SelectionLength > 0 Then
>             My.Computer.Clipboard.SetText(TextBox1.SelectedText,_
> TextDataFormat.Text)
>         End If
>         If TextBox2.SelectionLength > 0 Then
>             My.Computer.Clipboard.SetText(TextBox2.SelectedText,_
> TextDataFormat.Text)
>         End If
>     End Sub
>
> But I want the code to know which textbox has text selected in it
> without me having to specify it.  Suppose I had more textboxes for
> example it would be tiresome to write the if statement for all the
> textboxes (or any control for that matter)...
> Is there a way to do this without specifying the control name all the
> time, I would love to learn how...

> Any help is welcome,
> Thanks in advance to anyone willing to help.
Author
20 Mar 2006 2:47 PM
TheGanjaMan
Hi Randy...

Thanks for the input, I think I'm going to get it solved with the
directcast keyword...

Thanks for your help...



Show quoteHide quote
"R. MacDonald" <sci***@NO-SP-AMcips.ca> wrote in
news:441ebe4c$0$27645$dbd4d001@news.wanadoo.nl:

> Hello, GanjaMan,
>
> I don't know anything about "My.Computer.Clipboard", but I think what
> you are doing here will not give you what you want.  For example, if
> both TextBoxes have selected text, you will always get the text in
> TextBox2.
>
> You might consider setting a module scope string to the selected text
> in the validate event of the TextBox, and using that in your button
> click event.
>
> Something like:
>
>    Private mstrSelectedText As String = ""
>
>    Private Sub TextBox_Validated(ByVal sender As Object, _
>                                  ByVal e As System.EventArgs) _
>                                        Handles TextBox1.Validated, _
>                                        TextBox2.Validated
>          mstrSelectedText = DirectCast(sender, TextBox).SelectedText
>    End Sub
>
> and then set the clipboard text using mstrSelectedText.  If you have a
> lot of TextBoxes, you can set the handler in a loop using AddHandler.
>
> Cheers,
> Randy