Vb.net 如何以编程方式在RichTextBox中添加按钮控件?

Vb.net 如何以编程方式在RichTextBox中添加按钮控件?,vb.net,winforms,button,Vb.net,Winforms,Button,如何(通过代码)在WinFormTextBox或RichTextBox中添加一个或多个按钮?具体来说,我想在每个文本行的末尾,在VbCrLf字符之前添加按钮。它将是这样的: Friend WithEvents Button1 As Button Private Sub RichTextBox1_Click(sender As Object, e As EventArgs) Handles RichTextBox1.Click Button1 = New Button '

如何(通过代码)在
WinForm
TextBox
RichTextBox
中添加一个或多个
按钮?具体来说,我想在每个文本行的末尾,在
VbCrLf
字符之前添加按钮。

它将是这样的:

Friend WithEvents Button1 As Button
    Private Sub RichTextBox1_Click(sender As Object, e As EventArgs) Handles RichTextBox1.Click
        Button1 = New Button ' Create new instance
        Me.Button1.Size = New System.Drawing.Size(75, 23) ' give the button a size
        Button1.Text = "My button" ' set the button text
        Me.Button1.UseVisualStyleBackColor = True ' make it look windows like


        Dim pos As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)  'determine the button position
        RichTextBox1.Controls.Add(Button1) ' get it inside the rich text box
        Button1.Location = New Point(RichTextBox1.Right - Button1.Width, pos.Y + RichTextBox1.Top) ' set the button position
    End Sub
当然,它不需要出现在Click事件中,您也许可以使用KeyPress事件来确定用户是否按了enter,然后在该点插入它。还可以使用位置(POS变量)更改按钮位置


Nandostyle将是这样的:

Friend WithEvents Button1 As Button
    Private Sub RichTextBox1_Click(sender As Object, e As EventArgs) Handles RichTextBox1.Click
        Button1 = New Button ' Create new instance
        Me.Button1.Size = New System.Drawing.Size(75, 23) ' give the button a size
        Button1.Text = "My button" ' set the button text
        Me.Button1.UseVisualStyleBackColor = True ' make it look windows like


        Dim pos As Point = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.SelectionStart)  'determine the button position
        RichTextBox1.Controls.Add(Button1) ' get it inside the rich text box
        Button1.Location = New Point(RichTextBox1.Right - Button1.Width, pos.Y + RichTextBox1.Top) ' set the button position
    End Sub
当然,它不需要出现在Click事件中,您也许可以使用KeyPress事件来确定用户是否按了enter,然后在该点插入它。还可以使用位置(POS变量)更改按钮位置


Nandostyle

谢谢。但是如何向下滚动richtextbox内容和MyButton?(当然,我可以基于文本高度实现此功能并在下一个位置重新绘制按钮,但这不是一种更简单的方法?谢谢。但是如何向下滚动richtextbox内容和MyButton?(当然,我可以基于文本高度实现此功能并在下一个位置重新绘制按钮,但这不是一种更简单的方法?