vb.net 教程 3-4 窗体编程 公共控件8 RichTextBox

前端之家收集整理的这篇文章主要介绍了vb.net 教程 3-4 窗体编程 公共控件8 RichTextBox前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
RichTextBox:富文本框
常用属性
DetectUrls:指示当输入一个URL地址时,RichTextBox 是否自动设置为 URL 的链接
ScrollBars:显示的滚动条类型。以下设置:
Both 在需要时同时显示水平滚动条和垂直滚动条
ForcedBoth 始终同时显示水平滚动条和垂直滚动条
ForcedHorizontal 始终显示水平滚动条
ForcedVertical 始终显示垂直滚动条
Horizontal 仅在文本比控件的宽度长时显示水平滚动条
None 不显示滚动条
Vertical 仅在文本比控件的高度长时显示垂直滚动条
SelectionColor
SelectionFont
SelectionIndent:所选内容开始行的左边缩进距离(像素)
SelectionRightIndent:所选内容开始行的右边缩进距离(像素)

常用方法
CanPaste():可以粘贴指定数据格式的剪贴板信息
Public Function CanPaste (clipFormat As DataFormats.Format) As Boolean
参数clipFormat 是DataFormats 类字段,包括Bitmap、Text、Text、WaveAudio等(请参见MSDN DataFormats 类)。
Paste():在光标位置粘贴剪贴板内容
SaveFile():将RichTextBox内容保存到文件
LoadFile():载入文件

事件:
LinkClicked:单击了富文本框。

一个简单的例子:

Public Class Form2
    Dim fontsize As Integer
    Dim fontname As String

    Private Sub Form2_Load(sender As Object,e As EventArgs) Handles MyBase.Load
        fontsize = 10
        fontname = "宋体"
    End Sub

    '设置字体为宋体
    Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
        fontname = "宋体"
        RichTextBox1.SelectionFont = New Font(fontname,fontsize)
    End Sub
    '设置字体为楷体
    Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click
        fontname = "楷体"
        RichTextBox1.SelectionFont = New Font(fontname,fontsize)
    End Sub

    '设置文字颜色为红色
    Private Sub Button3_Click(sender As Object,e As EventArgs) Handles Button3.Click
        RichTextBox1.SelectionColor = Color.Red
    End Sub
    '设置文字颜色为蓝色
    Private Sub Button4_Click(sender As Object,e As EventArgs) Handles Button4.Click
        RichTextBox1.SelectionColor = Color.Blue
    End Sub

    '设置文字段落左缩进为10
    Private Sub Button5_Click(sender As Object,e As EventArgs) Handles Button5.Click
        RichTextBox1.SelectionIndent = 10
    End Sub
    '设置文字段落左缩进为20
    Private Sub Button6_Click(sender As Object,e As EventArgs) Handles Button6.Click
        RichTextBox1.SelectionIndent = 20
    End Sub

    '设置文字段落右缩进为10
    Private Sub Button7_Click(sender As Object,e As EventArgs) Handles Button7.Click
        RichTextBox1.SelectionRightIndent = 10
    End Sub
    '设置文字段落右缩进为20
    Private Sub Button8_Click(sender As Object,e As EventArgs) Handles Button8.Click
        RichTextBox1.SelectionRightIndent = 20
    End Sub

    '设置文字大小为10磅
    Private Sub Button9_Click(sender As Object,e As EventArgs) Handles Button9.Click
        fontsize = 10
        RichTextBox1.SelectionFont = New Font(fontname,fontsize)
    End Sub
    '设置文字大小为20磅
    Private Sub Button10_Click(sender As Object,e As EventArgs) Handles Button10.Click
        fontsize = 12
        RichTextBox1.SelectionFont = New Font(fontname,fontsize)
    End Sub

    '插入一张图片
    Private Sub Button11_Click(sender As Object,e As EventArgs) Handles Button11.Click
        Dim bmp As Bitmap
        bmp = Bitmap.FromFile("d:\15af.jpg")
        '将图片设置到剪贴板
        Clipboard.SetDataObject(bmp)
        '设置格式
        Dim MyFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)
        '检查是否可以粘贴指定的格式
        If RichTextBox1.CanPaste(MyFormat) Then
            'Paste重载的一个版本,在光标位置粘贴指定格式的剪贴板内容
            RichTextBox1.Paste(MyFormat)
        End If

    End Sub

    '当富文本框内的超链接被点击
    Private Sub RichTextBox1_LinkClicked(sender As Object,e As LinkClickedEventArgs) Handles RichTextBox1.LinkClicked
        Process.Start(e.LinkText)
    End Sub
End Class

运行如下:


学习更多vb.net知识,请参看 vb.net 教程 目录

原文链接:https://www.f2er.com/vb/256642.html

猜你在找的VB相关文章