简体   繁体   English

VB.NET 中的 RichTextBox

[英]RichTextBox in VB.NET

I want set a line of text link in rich text box of vb.net.我想在 vb.net 的富文本框中设置一行文本链接。 Like for example: I want to know you比如:我想认识你

The word want, I want to set a link word.想要这个词,我想设置一个链接词。

Can I do that?我可以这样做吗?

This is how I would do it.这就是我要做的。

Dim linkLa As New LinkLabel
linkLa.LinkColor = Color.Red

Dim link As LinkLabel.Link = linkLa.Links.Add(0, 13, "http://www.stackoverflow.com")
linkLa.Text = "Stackoverflow"
AddHandler linkLa.LinkClicked, AddressOf Link_Clicked

richTextBox1.Controls.Add(linkLa)

Private Sub Link_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    MessageBox.Show("clicked")
End Sub 

I have an answer for you.我有一个答案给你。 This will allow you to show the link target address as a tooltip.这将允许您将链接目标地址显示为工具提示。 (little pop up bubble.) Other than that, it's similar to Stan R.'s answer. (弹出的小气泡。)除此之外,它类似于 Stan R. 的回答。

  1. Put this code under the "add link" button (or whatever you're calling it) in your program将此代码放在程序中的“添加链接”按钮(或任何您调用的按钮)下

Note: I put comments before each line, so it's easier to follow!注意:我在每一行之前都加了注释,这样更容易理解!


'define the text and link targets
Dim linktext As String = LinkTextbox.Text 'LinkTextbox is just the textbox where the user inputs the text of the link
Dim linktarget As String = LinkTargetTextbox.Text 'LinkTargetTextBox is just the textbox where the user inputs the target URL of the link

'Define the LinkLabel
Dim lnk As New LinkLabel
'if you want, you can set the different properties, like font or linkcolor, programmatically after defining the linklabel, for instance:
lnk.LinkColor = Color.Blue
'set tooltip
lnk.Tooltip = linktarget
'set the link target
Dim lk As LinkLabel.Link = lnk.Links.Add(0, 13, linktarget)
'set the link text
lnk.Text = linktext
'EventHandler
AddHandler lnk.LinkClicked, AddressOf LinkClicked
'Add the control to the richtextbox
RichTextBox1.Controls.Add(lnk)
'This is the Subroutine that the label will run when clicked (Make sure to put your "End Sub" before this, because it's not part of the button's subroutine)
Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
    'send link to the browser
    Process.Start(linktarget)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM