473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Which Control am I working on?

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
4 1233
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: AK | last post by:
Hi, I recently read an advice here that one should try to use make and version control system even if you're the only one working on the program. Is that a good advice? How many of you do that? ...
2
by: Tom | last post by:
hi guys I have a problem a event bubble that isn't working. isn't working= I've opened up a handle in my user control but when I try to use it ie. ok I am trying to assign an event handler from...
1
by: Gilles T. | last post by:
I have calendar control in Javascript including in the Head of mu page: <script charset="iso-8859-1" language="JavaScript" src="popcalendar.js"></script> I call this calendar popup with a image...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: Sevu | last post by:
I am working with ASP.NET.I am using ReportViwer Control to show my report.I like to add dropdownlist with in the reportviewer control. ( Not top to the control some thing like that).I need to...
15
by: mc | last post by:
I'm writing an app for managing Task Lists, I'm trying to add some controls to a form that I can use to link tasks, my original intention was to: - Add two list boxes, one listing "all Tasks"...
2
by: monomaniac21 | last post by:
Hi all we are thinking of using a version control system at work. I was wondering what are some of the issues we should take into consideration when deciding upon which one to use? and which...
13
by: Dog Ears | last post by:
I've got a windows form that monitors the status of employees it polls a database every 2 seconds to fetch updates then update a underlying dataset bound to a grid using Invoke to marshal to the...
2
by: Steve Swift | last post by:
I don't have a particular webpage to point you to; I thought about this whilst walking my dog this morning and don't have an example page. That said, this question doesn't really require a...
4
by: =?Utf-8?B?Qkw=?= | last post by:
I am in a problem to access user control(.ascx) on a .aspx page the following is my scenario I have following directory structure - Autoboom - workshop -- WSFirst -showroom
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.