简体   繁体   English

VB.NET拖动文本,设置焦点并输入关键问题

[英]VB.NET Drag text, set focus and enter key issue

I have an app that allows for text to be dragged-dropped into a text box. 我有一个应用程序,允许将文本拖放到文本框中。 I also have a check box that allows the app to always be on top of all windows. 我还有一个复选框,允许该应用程序始终位于所有窗口的顶部。 My issues is when I drag text into the text box and I hit the enter key, it does not run the function unless I have the window in actual focus (by clicking on it). 我的问题是,当我将文本拖动到文本框中并按Enter键时,除非我将窗口置于实际焦点(单击它),否则它将无法运行该功能。

My question is, how can I make sure when I drag text into the text box, it will make the window be the focus so when I hit the enter key, it will run my function? 我的问题是,如何确保将文本拖到文本框中时,它将使窗口成为焦点,所以当我按Enter键时,它将运行我的功能?

Here is what I am trying with no luck: 这是我没有运气的尝试:

Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles Me.DragEnter
        Me.Focus()
    End Sub
    Private Sub Form1_DragOver(sender As Object, e As DragEventArgs) Handles Me.DragOver
        Me.Focus()
    End Sub

尝试在DragEnter中使用Me.Activate()代替它应该足够了

I am posting the code as it works fine for me, try it on new project: 我正在发布代码,因为它对我来说很好用,请在新项目上尝试:

Public Class Form1
    Private Sub TextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter

        ' Check the format of the data being dropped. 
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            ' Display the copy cursor. 
            e.Effect = DragDropEffects.Copy
        Else
            ' Display the no-drop cursor. 
            e.Effect = DragDropEffects.None
        End If

    End Sub

    Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
        ' Drop text and move cursor to end of drag-dropped text 
        TextBox1.Text = e.Data.GetData(DataFormats.Text)
        TextBox1.SelectionStart = TextBox1.Text.Length + 1
        TextBox1.Focus()
        Me.Activate()
    End Sub

End Class

and designer: 和设计师:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TextBox1 = New System.Windows.Forms.TextBox()
    Me.SuspendLayout()
    '
    'TextBox1
    '
    Me.TextBox1.AllowDrop = True
    Me.TextBox1.Location = New System.Drawing.Point(30, 54)
    Me.TextBox1.Multiline = True
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(216, 125)
    Me.TextBox1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(284, 262)
    Me.Controls.Add(Me.TextBox1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.TopMost = True
    Me.ResumeLayout(False)
    Me.PerformLayout()

End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM