By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Which Control am I working on?

P: n/a
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.SelectedTex t,
TextDataFormat.Text)
End If
If TextBox2.SelectionLength > 0 Then
My.Computer.Clipboard.SetText(TextBox1.SelectedTex t,
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.

Mar 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.SelectedTex t,_
TextDataFormat.Text)
End If
If TextBox2.SelectionLength > 0 Then
My.Computer.Clipboard.SetText(TextBox2.SelectedTex t,_
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.
Mar 20 '06 #2

P: n/a
TheGanjaMan wrote:
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.SelectedTex t,_
TextDataFormat.Text)
End If
If TextBox2.SelectionLength > 0 Then
My.Computer.Clipboard.SetText(TextBox2.SelectedTex t,_
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
Mar 20 '06 #3

P: n/a
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:
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.SelectedTex t,_
TextDataFormat.Text)
End If
If TextBox2.SelectionLength > 0 Then
My.Computer.Clipboard.SetText(TextBox2.SelectedTex t,_
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.

Mar 20 '06 #4

P: n/a
Hi Randy...

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

Thanks for your help...

"R. MacDonald" <sc****@NO-SP-AMcips.ca> wrote in
news:44***********************@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

Mar 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.