Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Go Back  Xtreme Visual Basic Talk > > > Copy/Paste from more then one RichTextBox


Reply
 
Thread Tools Display Modes
  #1  
Old 06-17-2010, 11:31 AM
wendy wendy is offline
Junior Contributor
 
Join Date: Jan 2002
Location: germany, frankfurt
Posts: 267
Default Copy/Paste from more then one RichTextBox


Hello,

in VB6 the Copy/Cut/Paste etc. feature could be setup with "AutoVerbMenu".
In .net 2008 this feauture is not available as far as I know, thus I configured the functions manually.
Everything work so far as expected for my first RichtTextBox.

Code:
    Private Sub popCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popCopy.Click

        If rtbTemplate.SelectedText <> "" Then
            rtbTemplate.Copy()
        End If

    End Sub

    Private Sub popPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popPaste.Click

        If Clipboard.ContainsText Then
            rtbTemplate.Paste()
        End If

    End Sub
I'm using several TextBoxes in my application where I want to use Copy/Paste feature.
until now I have defined only 1 "ContextMenuStrip_CopyPaste".
I have assigned this ContextMenuStrip to each Textbox ContextMenuStrip Property.
If I use mouse right click, the popup is shown properly.

However, the code for this single ContextMenuStrip does include only the textbox name "rtbTemplate", but not "rtbTemplate2", etc.
Thus, each time I want to paste something in rtbTemplate2 anything, it is paste to rtbTemplate

I wondering how to setup this copy/paste feature as a unique routine for all textboxes.
I tried to use "sender As System.Object" or "e As System.EventArgs", but could not find a way how to distinguish from which Textbox should be used for copy/paste

Do I have to define 5xContextMenuStrip Controls for 5x Textboxes???
I don't believe this, but I do not find a way to solve the problem.

Many thanks for a hint :-)
Reply With Quote
  #2  
Old 06-17-2010, 02:15 PM
shekoasinger shekoasinger is offline
Freshman
 
Join Date: May 2010
Location: The Netherlands
Posts: 46
Cool Seriously

Try to write more commands in the paste function, something like this:

Code:
Private Sub popPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popPaste.Click

        If Clipboard.ContainsText Then
            rtbTemplate.Paste()
            rtbTemplate2.Paste()
        End If

    End Sub
Try this? Dont know if this is what you mean, reply maybe I can help you.
Reply With Quote
  #3  
Old 06-18-2010, 08:54 AM
wendy wendy is offline
Junior Contributor
 
Join Date: Jan 2002
Location: germany, frankfurt
Posts: 267
Default

thanks for you suggestion, but this is not an alternative unfortunately.
With this code I would paste the clipboard always to all textboxes, but I need to have the actions performed (copy/paste, etc.) only on the form the PopUp Menu event was raised

Any suggestions welcome
Reply With Quote
  #4  
Old 06-18-2010, 11:51 AM
shekoasinger shekoasinger is offline
Freshman
 
Join Date: May 2010
Location: The Netherlands
Posts: 46
Default

Seriously I can help you so easily just answer the following questions:

What needs to be copied? What is the location(on what form etc.) Where does it have to be pasted. Does it all have to be written in 1 button ?
Reply With Quote
  #5  
Old 06-22-2010, 09:31 AM
wendy wendy is offline
Junior Contributor
 
Join Date: Jan 2002
Location: germany, frankfurt
Posts: 267
Default

Hello,
as mentioned, I do have several RichTextBoxes in my project.
To all of them I assigned the same ContextMenu for Copy/Paste...., as I do not want to define a new ContexMenu with the same items for each RTB - doesn't make sense.

The only question is how to distinguish from with RichTextBox the ContextMenu was opened, so I can paste or copy only from or to this form.

I tried
Code:
Private Sub popCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popCopy.Click

Dim rtb As RichTextBox = CType(sender.SourceControl, RichTextBox)
....
....
but this is not working with Option strike as late binding is not allowed and I'm getting an error with "sender.SourceControl"
Reply With Quote
  #6  
Old 06-22-2010, 09:43 AM
AtmaWeapon's Avatar
AtmaWeaponCopy/Paste from more then one RichTextBox AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You're not thinking the problem through, or you're copy/pasting code without thinking about it.

Why are you using CType() to convert SourceControl from Control to RichTextBox? You are doing so because Option Strict is on and late binding is not allowed. You have to explicitly cast before you can treat the object as a RichTextBox.

Why can't you use SourceControl? Because sender is an Object and not a ContextMenuStrip so you need an explicit cast.
Code:
Dim menu As ContextMenuStrip = CType(sender, ContextMenuStrip)
Dim rtb As RichTextBox = CType(menu.SourceControl, RichTextBox)
(Note you may be using ContextMenu instead of ContextMenuStrip; the change should be obvious.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 06-22-2010, 10:14 AM
wendy wendy is offline
Junior Contributor
 
Join Date: Jan 2002
Location: germany, frankfurt
Posts: 267
Default

Many thanks for your swift reply

When using this syntax I'm getting an error "Unable to cast Object of Type.." now.

I tried using ContextMenu and ContextMenuStrip.

Not sure I'm still doing something wrong
Unfortunately I do not have much experience with VB .net.
Reply With Quote
  #8  
Old 06-22-2010, 11:23 AM
AtmaWeapon's Avatar
AtmaWeaponCopy/Paste from more then one RichTextBox AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You listed the least important part of the error message. The rest of the message reveals the real type of the object and the type you tried to cast it to. It'd also help if you'd post the code you are actually using instead of just saying "I tried that". Sometimes there are tiny differences.

*edit*
I'll make a preemptive guess: I bet the rest of the message says something like "'ToolStripMenuItem" to type 'ContextMenuStrip'". It's possible the menu item itself raises the event rather than the menu. In this case, you need to see if you can get to the rich text box from there. If there's not a direct way, you can probably get from the menu item to its menu, which will get you to the rich text box. Think about it a bit. Don't just give up when you get an error message; figure out why there's an error and how you can solve it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 06-22-2010 at 11:38 AM.
Reply With Quote
  #9  
Old 06-22-2010, 11:34 AM
wendy wendy is offline
Junior Contributor
 
Join Date: Jan 2002
Location: germany, frankfurt
Posts: 267
Default

Quote:
Originally Posted by wendy View Post
Many thanks for your swift reply

When using this syntax I'm getting an error "Unable to cast Object of Type.." now.

I tried using ContextMenu and ContextMenuStrip.

Not sure I'm still doing something wrong
Unfortunately I do not have much experience with VB .net.
Your are right - with more information it is much easier to troubleshoot this
Below are the errors I get when using the code

Code:
A first chance exception of type 'System.InvalidCastException' occurred in MyProject.NET.exe
System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.ContextMenuStrip'.
   at MyProject.NET.frmMain.popCopy_Click(Object sender, EventArgs e) in E:\-- VB .net\MyProject\MyProject.NET\MyProject.NET\frmMain.vb:line 618

   
   A first chance exception of type 'System.InvalidCastException' occurred in MyProject.NET.exe
System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.ContextMenu'.
   at MyProject.NET.frmMain.popCopy_Click(Object sender, EventArgs e)

Code:
    Private Sub popCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popCopy.Click

        Try

            Dim menu As ContextMenu = CType(sender, ContextMenu)
            Dim rtb As RichTextBox = CType(menu.SourceControl, RichTextBox)

            If rtbTemplate.SelectedText <> "" Then
                rtbTemplate.Copy()
            End If

        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try

    End Sub
Hope this helps.
Please let me know if you need further details.

many thanks!!!
Reply With Quote
  #10  
Old 06-22-2010, 11:53 AM
PlausiblyDamp's Avatar
PlausiblyDampCopy/Paste from more then one RichTextBox PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Given the error states
Quote:
Unable to cast object of type 'System.Windows.Forms.ToolStripMenuItem' to type 'System.Windows.Forms.ContextMenuStrip'
it isn't a great leap of faith to assume that sender is of type ToolStripMenuItem and not ContextMenuStrip and that trying to convert isn't going to work.

Have you tried just using popCopy.SourceControl instead?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #11  
Old 06-22-2010, 12:24 PM
wendy wendy is offline
Junior Contributor
 
Join Date: Jan 2002
Location: germany, frankfurt
Posts: 267
Smile

Quote:
Originally Posted by PlausiblyDamp View Post
Given the error states

it isn't a great leap of faith to assume that sender is of type ToolStripMenuItem and not ContextMenuStrip and that trying to convert isn't going to work.

Have you tried just using popCopy.SourceControl instead?
I do not understand why it is showing a type "ToolStripMenuItem".-
I just added the "ContextMenuStrip" from Toolbox menu.
Then I edit the text and double click the the menu designer. The "PopCopy" event was created automatically

But the I got it working with your hint to directly use popCopy.SourceControl.
With the control name the copy/paste feature is working as I expected - this hint was perfect - MANY THANKS!!!

Code:
    Private Sub popCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popCopy.Click

        Try

            Dim rtb As RichTextBox = CType(ContextMenuStrip_CopyPaste.SourceControl, RichTextBox)

            If rtb.SelectedText <> "" Then
                rtb.Copy()
            End If

        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try

    End Sub
Attached Images
File Type: png ContextMenuStrip.png (4.0 KB, 1 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
 
Copy/Paste from more then one RichTextBox
Copy/Paste from more then one RichTextBox
 
-->