Which Control am I working on?

  • Thread starter Thread starter TheGanjaMan
  • Start date Start date
T

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.
 
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.
 
TheGanjaMan said:
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.
 
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
 
Hi Randy...

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

Thanks for your help...
 
Back
Top