我有一个应用程序,允许将文本拖放到文本框中。我还有一个复选框,允许应用程序始终位于所有窗口的顶部。我的问题是当我将文本拖到文本框中并按下回车键时,除非我将窗口置于实际焦点(通过单击它),否则它不会运行该功能。
我的问题是,如何确保当我将文本拖到文本框中时,它会使窗口成为焦点,所以当我按下回车键时,它会运行我的功能?
这是我没有运气的尝试:
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
答案 0 :(得分:0)
尝试在DragEnter中使用Me.Activate()代替它
答案 1 :(得分:0)
我发布代码,因为它适用于我,在新项目上尝试:
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
和设计师:
<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