用vb.net实现写字板程序报告(三)

类别:.NET开发 点击:0 评论:0 推荐:

所有源代码均在这里下载:

http://www.up2e.com/resource.php

用vb.net实现写字板程序报告(三)---by zigz(LuHai)
[email protected]

3)            关于查找替换功能
这里我用了一个Panel面板控件来放查找,查找下一个,替换按钮和文本框。

4 查找替换

我为什么不用一个窗体呢?因为我正好想练练拖动控件的代码,可以实现让这个面板在主窗体范围内拖动,由于看到了一个老外的教程,就顺便翻译了来练习练习。首先说说这个简单的查找替换功能,就是用户在第一个文本框中输入希望查找的字,然后点击查找按钮,程序就会在RichTextBox中查找相匹配的字,找到之后,将其高亮显示,点击下一个按钮,就会找到下一个匹配的字。。如此反复,直到结束,而替换就是把所有在RichTextBox中第一个文本框中的内容用第二个文本框的内容替换。是不是有些绕口令?还是亲自尝试一下就知道了。下面是查找替换相关代码(主要是创建一个FindText函数):

'下面是关于实现查找功能

    Dim MyPos As Integer '先声明一个全局变量

    Private Sub FindText(ByVal start As Integer) '创建findtext函数

        Dim pos As Integer

        Dim target As String

        '获取用户输入的要查找的字符串

        target = txtbox.Text

        pos = InStr(start, rtbox.Text, target)

        If pos > 0 Then  '找到了匹配字符串

            MyPos = pos

            rtbox.SelectionStart = MyPos - 1 '高亮显示

            rtbox.SelectionLength = Len(txtbox.Text)

            rtbox.Focus()

        Else

            MsgBox("没找到!")

        End If

    End Sub

find按钮,findNext按钮

Private Sub find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find.Click

        FindText(1)

End Sub

Private Sub findnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles findnext.Click

        FindText(MyPos + 1)

    End Sub

拖动控件的代码:

'下面这段程序,用作拖拽“查找面板”使用

    Dim dragging As Boolean

    Dim mousex As Integer

    Dim mousey As Integer

    Private Sub panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown

        If e.Button = MouseButtons.Left Then

            dragging = True

            mousex = -e.X

            mousey = -e.Y

            Dim clipleft As Integer = Me.PointToClient(MousePosition).X - Panel1.Location.X

            Dim cliptop As Integer = Me.PointToClient(MousePosition).Y - Panel1.Location.Y

            Dim clipwidth As Integer = Me.ClientSize.Width - (Panel1.Width - clipleft)

            Dim clipheight As Integer = Me.ClientSize.Height - (Panel1.Height - cliptop)

            Cursor.Clip = Me.RectangleToScreen(New Rectangle(clipleft, cliptop, clipwidth, clipheight))

            Panel1.Invalidate()

        End If

    End Sub

 

    Private Sub panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove

        If dragging Then

            '移动控件到新的位置

            Dim MPosition As New Point()

            MPosition = Me.PointToClient(MousePosition)

            MPosition.Offset(mousex, mousey)

            '确实控件不能离开主窗口

            Panel1.Location = MPosition

        End If

    End Sub

 

    Private Sub panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp

        If dragging Then

            '结束拖拽

            dragging = False

            Cursor.Clip = Nothing

            Panel1.Invalidate()

        End If

    End Sub

 

未完待续......



 

 

本文地址:http://com.8s8s.com/it/it45490.htm