Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have 42 RichTextBoxes (RTBs) and I would like to add my ContextMenuStrip to each of them (which I have already done), but the problem I am having is when the application is running and the ContextMenuItem "Copy" for example is clicked, I can only make it copy the context of one of the RTBs, when I would like it to copy the contents of any individual one.
So as I mentioned, I have 42 RTBs, but I am having trouble getting the contents of the focused RTB. The below code is just copying the 1 RTB and when I Copy on another RTB, it copies the contents of RTB1.
I originally started out with like so, but is just for the one RTB and when I add more than one in the code below it's even more of a disaster..
VB
Private Sub Copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy.Click
    RichTextBox1.Copy()
End Sub

I tried using this, but I was not sure how to add the copy function into it:
VB
Private Sub Copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy.Click
    Try
        For Each itm As Control In EnumerateAllControls(Me)
            For Each c As Control In itm.Controls
                If TypeOf c Is RichTextBox Then
                    ****NOT SURE WHAT CODE TO PUT HERE..****
                End If
            Next
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

So If anyone can help my Copy ContextMenuItem to focus on the user focused RTB, I would greatly appreciate it! :)
Posted

1 solution

You would do something like this

VB
if TypeOf c is RichTextBox then
  c.Copy
end if


But this only copies it to the clipboard and will be overwritten with the data from the other Richtextboxs.
 
Share this answer
 
Comments
NY Andrew 10-Aug-11 19:16pm    
Thanks for your reply Simon, but I have already tried that and it did not work like it should.
Simon_Whale 10-Aug-11 19:18pm    
What is it your after it doing?
NY Andrew 10-Aug-11 19:21pm    
Ok, so I have 42 RTBs and when the user focuses on say RTB3, I want the ContextMenu to pop (which it does), and then I want the COPY menu to copy highlighted text, but I cannot get it to work because I have so many RTBs..
Simon_Whale 10-Aug-11 19:29pm    
Have a look at the contextmenu.sourcecontrol this will tell you what control fire the event.

so I would use

dim rch as RichTextbox = ContextMenu1.Sourcecontrol
rch.Copy


NY Andrew 10-Aug-11 19:37pm    
OH WOW!! It actually worked! THANK YOU SO MUCH Simon_Whale! I NEVER would have thought of that!
May I ask, how this sourcecontrol works? I never even heard of it before.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900