linklabel_VB.NET LinkLabel

linklabel

LinkLabel, new in Visual Basic .NET, is a standard control that lets you embed web-style links in a form. Like a lot of VB.NET controls, this one doesn't do anything that you couldn't do before ... but with more code and more trouble. For example, VB 6 had the Navigate (and Navigate2 when the first one proved inadequate) methods that you could use with a URL text string to call a web page.

LinkLabelVisual Basic .NET中的新增功能,它是一个标准控件,可让您将Web样式的链接嵌入到表单中。 像许多VB.NET控件一样,此控件不会执行您以前无法做的任何事情,但是会带来更多的代码和更多的麻烦。 例如,VB 6具有可以与URL文本字符串一起使用来调用网页的Navigate (和第一个证明不充分的Navigate2 )方法。

LinkLabel is much more convenient and trouble free than older techniques. But, in sync with .NET architecture, LinkLabel is designed to be used with other objects to do the whole job. You still need to use a separate command to start an email or browser for example. Example code is included below.

LinkLabel比旧技术更加方便和无故障。 但是,与.NET体系结构同步,LinkLabel旨在与其他对象一起使用以完成整个工作。 例如,您仍然需要使用单独的命令来启动电子邮件或浏览器。 示例代码如下。

The basic idea is to put the email address or web URL into the Text property of a LinkLabel component, then when the label is clicked, the LinkClicked event is triggered. There are well over a hundred methods and objects available for the LinkLabel object including properties to handle everything you might want to do with a link like changing the color, text, position, how it behaves when you click it ... whatever! You can even check mouse buttons and positions and test whether the Alt, Shift, or Ctrl keys are pressed when the link is clicked. A list is shown in the illustration below:

基本思想是将电子邮件地址或Web URL放入LinkLabel组件的Text属性中,然后在单击标签时触发LinkClicked事件。 LinkLabel对象有一百多种方法和对象,包括用于处理您可能希望通过链接进行的所有操作的属性,例如更改颜色,文本,位置,单击时的行为……等等! 您甚至可以检查鼠标按钮和位置,并测试单击链接时是否按下了AltShiftCtrl键。 下图显示了一个列表:

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

An object with a really long name is also passed to this event: LinkLabelLinkClickedEventArgs. Fortunately, this object is instantiated with the nice short name used for all event arguments, e. The Link object has more methods and properties. The illustration below shows the event code and the Link object.

具有真正长名的对象也将传递给此事件: LinkLabelLinkClickedEventArgs 。 幸运的是,此对象使用用于所有事件参数e的漂亮短名称实例化。 Link对象具有更多的方法和属性。 下图显示了事件代码和Link对象。

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

You will normally use the Text property of the Link object to get a URL or email address and then pass this value to System.Diagnostics.Process.Start.

通常,您将使用Link对象的Text属性获取URL或电子邮件地址,然后将此值传递给System.Diagnostics.Process.Start

To bring up a web page ...

调出网页...

System.Diagnostics.Process.Start("http://visualbasic.about.com")

System.Diagnostics.Process.Start(“ http://visualbasic.about.com”)

To start an email using the default email program ...

要使用默认电子邮件程序启动电子邮件...

System.Diagnostics.Process.Start("mailto:" & "visualbasic@aboutguide.com")

System.Diagnostics.Process.Start(“ mailto:”&“ visualbasic@aboutguide.com”)

But you're really limited only by your imagination in using the five overloads of the Start method. You could, for example, start the Solitaire game:

但是,实际上,使用Start方法的五个重载仅受您的想象力限制。 例如,您可以启动纸牌游戏:

System.Diagnostics.Process.Start("sol.exe")

System.Diagnostics.Process.Start(“ sol.exe”)

If you put a file in the string field, then the default processing program for that file type in Windows will kick in and process the file. This statement will display MyPicture.jpg (if it's in the root of drive C:).

如果将文件放在字符串字段中,则Windows中该文件类型的默认处理程序将启动并处理该文件。 该语句将显示MyPicture.jpg(如果位于驱动器C:的根目录中)。

System.Diagnostics.Process.Start("C:MyPicture.jpg")

System.Diagnostics.Process.Start(“ C:MyPicture.jpg”)

You can use the LinkLabel almost like a button by simply putting any code you like in the LinkClicked event instead of the Start method.

您只需将所需的任何代码放入LinkClicked事件而不是Start方法中,就可以像使用按钮一样使用LinkLabel。

The investigation of the hundred or so other possibilities is wa-a-a-y beyond the scope of this article, but here are a few examples to get you started.

对数百种其他可能性的研究超出了本文的范围,但是这里有一些示例可以帮助您入门。

One new concept used in LinkLabel is the idea that there can be multiple links in a LinkLabel and they're all stored in a LinkCollection type. The first element, Links(0), in the collection is created automatically although you can control what it is using the LinkArea property of LinkLabel. In the example below, the Text property of LinkLabel1 is set to "FirstLink SecondLink ThirdLink" but only the first 9 characters are specified as a link. The Links collection has a Count of 1 because this link was added automatically.

LinkLabel中使用的一个新概念是,LinkLabel中可以有多个链接,并且所有链接都存储在LinkCollection类型中。 尽管可以使用LinkLabel的LinkArea属性来控制集合中的第一个元素Links(0) ,它是自动创建的。 在下面的示例中,LinkLabel1的Text属性设置为“ FirstLink SecondLink ThirdLink”,但是仅将前9个字符指定为链接。 Links集合的计数为1,因为此链接是自动添加的。

To add more elements to the Links collection, just use the Add method. The example also shows how ThirdLink can be added as an active part of the link.

要将更多元素添加到Links集合,只需使用Add方法。 该示例还显示了如何将ThirdLink添加为链接的活动部分。

--------Click Here to display the illustrationClick the Back button on your browser to return--------

--------单击此处以显示插图单击浏览器上的“后退”按钮以返回--------

It's easy to associate different targets with the different parts of the Link Text. Just set the LinkData property. To make FirstLink target the About Visual Basic web page and ThirdLink target the main About.Com web page, simply add this code to the initialization (the first two statements are repeated from the illustration above for clarity):

将不同的目标与链接文本的不同部分关联起来很容易。 只需设置LinkData属性。 要使FirstLink以About Visual Basic网页为目标,而ThirdLink以About.Com主网页为目标,只需将此代码添加到初始化中(为清楚起见,在上图中重复了前两个语句 ):

LinkLabel1.LinkArea = New LinkArea(0, 9)LinkLabel1.Links.Add(21, 9)LinkLabel1.Links(0).LinkData = "http://visualbasic.about.com"LinkLabel1.Links(1).LinkData = "http://www.about.com"

LinkLabel1.LinkArea =新的LinkArea(0,9)LinkLabel1.Links.Add(21,9)LinkLabel1.Links(0).LinkData =“ http://visualbasic.about.com” LinkLabel1.Links(1).LinkData = “ http://www.about.com”

You might want to do something like this to customize links for different users. You could use code to make one group of users go to a different target than another group.

您可能需要执行类似的操作来为不同用户自定义链接。 您可以使用代码使一组用户转到另一组用户。

Microsoft "saw the light" about hyperlinks with VB.NET and included everything you might want to do with them.

Microsoft对VB.NET的超链接“一见倾心”,并包括了您可能想对它们进行的所有操作。

翻译自: https://www.thoughtco.com/the-vbnet-linklabel-3424428

linklabel

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值