
正文
vb.net选择文本,vb选择文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
VB.NET中怎样用API获得文本框中的选中文本
Public Declare Auto Function GetWindowText Lib "user32" Alias "GetWindowText" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Public Declare Auto Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLength" (ByVal hwnd As Integer) As Integer
Public Function GetText(ByVal hwnd As Integer) As String
Dim nLen As Integer
nLen = GetWindowTextLength(hwnd)
GetText = Space(nLen)
GetWindowText(hwnd, GetText, nLen)
End Function
VS2008测试通过, 函数GetText传入的就是对应文本框的句柄.
相关问答
Q1: vb.net怎么实现读取指定WORD文档中的内容
添加spire.doc.dll为引用,在vb.net中读取指定word文档的内容到 txt文件,代码示例如下:
'加载Word文档
Dim doc As Document = New Documentdocument.LoadFromFile("测试文档.docx")
'使用GetText方法获取文档中的所有文本
Dim s As String = doc.GetText
File.WriteAllText("文本1.txt", s.ToString)
Q2: vb.net textbox1选中的文本,拖放到textbox2?
很久没有上这里了,今天看到了这个问题,尝试做了一个;
本例以源文本框TextBox1全部文字作为拖放文字为例,实现拖放
1、向一个窗体中添加两个文本框,分别名为TextBox1,TextBox2。注意:把TextBox2控件的AllowDrop属性设置成True,这点不要遗漏。
2、完整的代码如下:
Public Class Form1
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
'设置一个标志以显示鼠标已按下。
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
'开始拖动(将TextBox1的文本内容作为拖放内容)。
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
'检查正在被拖放的数据的格式。
If (e.Data.GetDataPresent(DataFormats.Text)) Then
'显示复制光标(表示是拖放行为)。
e.Effect = DragDropEffects.Copy
Else
'显示不放置光标(表示不是拖放行为)。
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
'粘贴文本(将拖放内容作为TextBox2的文本内容)。
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
End Class






