开发者

Managing Cut, Copy Paste button availability .NET WinForms

开发者 https://www.devze.com 2023-01-21 21:38 出处:网络
I\'m working on a WinForms solution in VB.NET. It\'s been a while since I\'m mostly a web developper. So what I need to do is to replicate the behavior of Microsoft Office product for the Cut, Copy, P

I'm working on a WinForms solution in VB.NET. It's been a while since I'm mostly a web developper. So what I need to do is to replicate the behavior of Microsoft Office product for the Cut, Copy, Paste and Undo menus and toolbar. Which meens, I need to enable Cut and Copy when and only when there's some selected text on the Form. The Paste menu have to be enabled only when the is some text in the clipboard.

Do you have any ideay about how to accomplish that ? I probably would have to check some event in the TextBox's to check if some text is selected (MouseUp 开发者_Python百科?). Then in the Enter event, I would check if there's something in the Clipboard to enabled Paste menu...

If you have any suggestions, samples, etc. I would really appreciate it !

Thanks a lot !


The Application.Idle event is good to do this, it runs after the last input event is retrieved. You just need to check if the currently active control is capable of copy/paste. Make your form's code look similar to this, using a ToolStrip with the 3 buttons:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        AddHandler Application.Idle, AddressOf UpdateButtons
    End Sub

    Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
        RemoveHandler Application.Idle, AddressOf UpdateButtons
        MyBase.OnFormClosing(e)
    End Sub

    Private Sub UpdateButtons(ByVal sender As Object, ByVal e As EventArgs)
        Dim box = TryCast(Me.ActiveControl, TextBoxBase)
        CopyButton.Enabled = box IsNot Nothing And box.SelectionLength > 0
        CutButton.Enabled = CopyButton.Enabled
        PasteButon.Enabled = box isnot Nothing and Clipboard.ContainsText
    End Sub

    Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyButton.Click
        Dim box = TryCast(Me.ActiveControl, TextBoxBase)
        If box isnot Nothing then box.Copy()
    End Sub

    '' etc...


End Class


That should all be default behavior if you're using the standard WinForms controls. You don't need to implement that yourself unless you have a custom context menu.


First off if you are not tied to WinForms, switch to WPF as this is much easier to accomplish since commanding is built in and a much more friendlier technology IMHO.

For cutting and copying you can make use of the Cut/Copy/Paste methods respectively, these exist on the TextBoxBase class, since the .NET 3.

The more difficult piece of your puzzle is dealing with the commands on a global scale, via your toolbar. You will need to implement the command pattern to make this possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消