Copy/Paste

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
hi,

I haven't looked into this extensively...but I'm trying to implement copy/paste in the main menu. So in the menu handler I need to know what the current selection is. I figured I could just do something like My.Clipboard.SetText(ActiveControl.SelectedText), assuming that the active control is a textbox. The problem is that in my particular app, the text that is selected is not always in the active control. I am wondering if this has anything to do with the fact that all of my textboxes are readonly. Any ideas are welcome and thanks!
 
No, you don't need to know what the current selection is. The TextBox class has Cut, Copy and Paste methods. You simply call the Copy method of the appropriate TextBox.
 
Why would it not? Clicking a menu item doesn't take focus from a control. If the ActiveControl is not the TextBox of interest then you must have shifted focus yourself, so it makes complete sense that you can't copy. The only time it would make sense to be able to copy from anything other than the ActiveControl is if there's only one control on the form you want to copy from and you can refer to that control specifically, whether it has focus or not. If there are multiple controls then of course the control to copy from must have focus.
 
OK I'll try my test again.

But what I was telling you was, when the TextBox is *readonly* it appears that I can select text in that text box without actually giving the textbox focus.

I can understand that Cut and Paste don't make sense for a readonly textbox, but Copy does make sense.

Anyway I put aside my copy/paste stuff a week ago because I was annoyed. I'll get back to it today after work and hopefully it works just like you say.
 
OK I'll try my test again.

But what I was telling you was, when the TextBox is *readonly* it appears that I can select text in that text box without actually giving the textbox focus.
OK, I see what you're saying now, but I still don't find that to be the case. In my tests, if I select text in a read-only TextBox then I have to click it to do so, which will give it focus. The only way I can see that you could select text without the TextBox taking focus would be if you did so in code, but if you're selecting the text in code then you can focus it in code too.
 
Ok its more complicated than I first let on. I have an MDI app. And the child form has a SplitContainer that has a listview in the bottom panel and a series of labels & readonly textboxes in the top panel. So I select text in one of the textboxes in the top panel. And now in my copy handler I have something like this:

VB.NET:
        Dim ctl As Form = Me.ActiveMdiChild
        If ctl Is Nothing Then Exit Sub

        If TypeOf ctl.ActiveControl Is TextBox Then
            Dim tb As TextBox = CType(ctl.ActiveControl, TextBox)
            tb.Copy()
        End If

And what I'm finding is that the Typeof ctl.ActiveControl = SplitContainer. So there you have it. My problem is that i have to check for split container and if it's a split container I have to check for *it's* active control.
 
VB.NET:
Dim f As Form = Me.ActiveMdiChild

If f IsNot Nothing Then
    Dim tb As TextBox
    Dim sc As SplitContainer = TryCast(f.ActiveControl, SplitContainer)

    If sc Is Nothing Then
        tb = TryCast(f.ActiveControl, TextBox)
    Else
        tb = TryCast(sc.ActiveControl, TextBox)
    End If

    If tb IsNot Nothing Then
        tb.Copy()
    End If
End If
 
ok now that I solved that problem...I have another question. Still related to copy/paste. When the edit menu is clicked I can handle that event to determine if I should enable or disable cut/copy/paste. What event should I be handling to decide if the toolstrip cut/copy/paste buttons should be enabled or disabled?
 
ok now that I solved that problem...I have another question. Still related to copy/paste. When the edit menu is clicked I can handle that event to determine if I should enable or disable cut/copy/paste. What event should I be handling to decide if the toolstrip cut/copy/paste buttons should be enabled or disabled?
You'd handle the Enter, and possibly Leave, events of your controls. You will at least have to handle the Enter events of all controls that support clipboard operations and enable your tool bar buttons. You can then either handle the Leave events of the same controls or else handle the Enter events of all the other controls and disable the tool bar buttons.
 
Back
Top